summaryrefslogtreecommitdiff
path: root/src/lib/embryo
diff options
context:
space:
mode:
authorMike Blumenkrantz <zmike@osg.samsung.com>2016-02-22 12:35:27 -0500
committerMike Blumenkrantz <zmike@osg.samsung.com>2016-02-22 18:29:01 -0500
commita2dbaa2759cd11e1b3dbb45488a478eb70536fc2 (patch)
tree43051abd42de6dc6fa2535d1b61afafcd49d209b /src/lib/embryo
parent3af1a8af1e76701d2204a8b3307cd80abce47efc (diff)
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
Diffstat (limited to 'src/lib/embryo')
-rw-r--r--src/lib/embryo/embryo_time.c65
1 files changed, 65 insertions, 0 deletions
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 @@
22#include "Embryo.h" 22#include "Embryo.h"
23#include "embryo_private.h" 23#include "embryo_private.h"
24 24
25#define STRGET(ep, str, par) { \
26 Embryo_Cell *___cptr; \
27 str = NULL; \
28 if ((___cptr = embryo_data_address_get(ep, par))) { \
29 int ___l; \
30 ___l = embryo_data_string_length_get(ep, ___cptr); \
31 (str) = alloca(___l + 1); \
32 if (str) embryo_data_string_get(ep, ___cptr, str); \
33 } }
25/* exported time api */ 34/* exported time api */
26 35
27static Embryo_Cell 36static Embryo_Cell
@@ -84,6 +93,61 @@ _embryo_time_date(Embryo_Program *ep, Embryo_Cell *params)
84 return 0; 93 return 0;
85} 94}
86 95
96static Embryo_Cell
97_embryo_time_tzdate(Embryo_Program *ep, Embryo_Cell *params)
98{
99 struct timeval timev;
100 struct tm *tm;
101 time_t tt;
102 const char *tzenv;
103 char *tz, prevtz[128] = {0};
104
105 if (params[0] != (9 * sizeof(Embryo_Cell))) return 0;
106 STRGET(ep, tz, params[1]);
107 tzenv = getenv("TZ");
108 if (tzenv)
109 strncpy(prevtz, tzenv, sizeof(prevtz) - 1);
110 if (tz)
111 {
112 setenv("TZ", tz, 1);
113 tzset();
114 }
115 gettimeofday(&timev, NULL);
116 tt = (time_t)(timev.tv_sec);
117 tm = localtime(&tt);
118 if (prevtz[0])
119 setenv("TZ", prevtz, 1);
120 else
121 unsetenv("TZ");
122 tzset();
123 if (tm)
124 {
125 Embryo_Cell *cptr;
126 double t;
127 float f;
128
129 cptr = embryo_data_address_get(ep, params[2]);
130 if (cptr) *cptr = tm->tm_year + 1900;
131 cptr = embryo_data_address_get(ep, params[3]);
132 if (cptr) *cptr = tm->tm_mon + 1;
133 cptr = embryo_data_address_get(ep, params[4]);
134 if (cptr) *cptr = tm->tm_mday;
135 cptr = embryo_data_address_get(ep, params[5]);
136 if (cptr) *cptr = tm->tm_yday;
137 cptr = embryo_data_address_get(ep, params[6]);
138 if (cptr) *cptr = (tm->tm_wday + 6) % 7;
139 cptr = embryo_data_address_get(ep, params[7]);
140 if (cptr) *cptr = tm->tm_hour;
141 cptr = embryo_data_address_get(ep, params[8]);
142 if (cptr) *cptr = tm->tm_min;
143 cptr = embryo_data_address_get(ep, params[9]);
144 t = (double)tm->tm_sec + (((double)timev.tv_usec) / 1000000);
145 f = (float)t;
146 if (cptr) *cptr = EMBRYO_FLOAT_TO_CELL(f);
147 }
148 return 0;
149}
150
87/* functions used by the rest of embryo */ 151/* functions used by the rest of embryo */
88 152
89void 153void
@@ -91,5 +155,6 @@ _embryo_time_init(Embryo_Program *ep)
91{ 155{
92 embryo_program_native_call_add(ep, "seconds", _embryo_time_seconds); 156 embryo_program_native_call_add(ep, "seconds", _embryo_time_seconds);
93 embryo_program_native_call_add(ep, "date", _embryo_time_date); 157 embryo_program_native_call_add(ep, "date", _embryo_time_date);
158 embryo_program_native_call_add(ep, "tzdate", _embryo_time_tzdate);
94} 159}
95 160