From a2dbaa2759cd11e1b3dbb45488a478eb70536fc2 Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Mon, 22 Feb 2016 12:35:27 -0500 Subject: [PATCH] embryo: add tzdate function in the case where a user wants to get the current date/time from a specified timezone, this function allows a timezone string to be passed as a parameter @feature --- data/embryo/default.inc | 7 ++++ src/lib/embryo/embryo_time.c | 65 ++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/data/embryo/default.inc b/data/embryo/default.inc index 632b30b892..7855ac8ec7 100644 --- a/data/embryo/default.inc +++ b/data/embryo/default.inc @@ -231,3 +231,10 @@ native Float:cbrt(Float:value); native Float:exp(Float:value); native Float:exp2(Float:value); native Float:hypot(Float:valuex, Float:valuey); + +/**************************************************************************/ +/* ADDED in embryo 1.18 */ +/**************************************************************************/ +#define EMBRYO_118 118 +/* return the current date, year, time etc. in the variables provided modified by timezone tz */ +native tzdate(tz[], &year, &month, &day, &yearday, &weekday, &hr, &min, &Float:sec); diff --git a/src/lib/embryo/embryo_time.c b/src/lib/embryo/embryo_time.c index 2caab43290..e8b481e350 100644 --- a/src/lib/embryo/embryo_time.c +++ b/src/lib/embryo/embryo_time.c @@ -22,6 +22,15 @@ #include "Embryo.h" #include "embryo_private.h" +#define STRGET(ep, str, par) { \ + Embryo_Cell *___cptr; \ + str = NULL; \ + if ((___cptr = embryo_data_address_get(ep, par))) { \ + int ___l; \ + ___l = embryo_data_string_length_get(ep, ___cptr); \ + (str) = alloca(___l + 1); \ + if (str) embryo_data_string_get(ep, ___cptr, str); \ + } } /* exported time api */ static Embryo_Cell @@ -84,6 +93,61 @@ _embryo_time_date(Embryo_Program *ep, Embryo_Cell *params) return 0; } +static Embryo_Cell +_embryo_time_tzdate(Embryo_Program *ep, Embryo_Cell *params) +{ + struct timeval timev; + struct tm *tm; + time_t tt; + const char *tzenv; + char *tz, prevtz[128] = {0}; + + if (params[0] != (9 * sizeof(Embryo_Cell))) return 0; + STRGET(ep, tz, params[1]); + tzenv = getenv("TZ"); + if (tzenv) + strncpy(prevtz, tzenv, sizeof(prevtz) - 1); + if (tz) + { + setenv("TZ", tz, 1); + tzset(); + } + gettimeofday(&timev, NULL); + tt = (time_t)(timev.tv_sec); + tm = localtime(&tt); + if (prevtz[0]) + setenv("TZ", prevtz, 1); + else + unsetenv("TZ"); + tzset(); + if (tm) + { + Embryo_Cell *cptr; + double t; + float f; + + cptr = embryo_data_address_get(ep, params[2]); + if (cptr) *cptr = tm->tm_year + 1900; + cptr = embryo_data_address_get(ep, params[3]); + if (cptr) *cptr = tm->tm_mon + 1; + cptr = embryo_data_address_get(ep, params[4]); + if (cptr) *cptr = tm->tm_mday; + cptr = embryo_data_address_get(ep, params[5]); + if (cptr) *cptr = tm->tm_yday; + cptr = embryo_data_address_get(ep, params[6]); + if (cptr) *cptr = (tm->tm_wday + 6) % 7; + cptr = embryo_data_address_get(ep, params[7]); + if (cptr) *cptr = tm->tm_hour; + cptr = embryo_data_address_get(ep, params[8]); + if (cptr) *cptr = tm->tm_min; + cptr = embryo_data_address_get(ep, params[9]); + t = (double)tm->tm_sec + (((double)timev.tv_usec) / 1000000); + f = (float)t; + if (cptr) *cptr = EMBRYO_FLOAT_TO_CELL(f); + } + return 0; +} + /* functions used by the rest of embryo */ void @@ -91,5 +155,6 @@ _embryo_time_init(Embryo_Program *ep) { embryo_program_native_call_add(ep, "seconds", _embryo_time_seconds); embryo_program_native_call_add(ep, "date", _embryo_time_date); + embryo_program_native_call_add(ep, "tzdate", _embryo_time_tzdate); }