diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/embryo/src/lib/embryo_time.c | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/libraries/embryo/src/lib/embryo_time.c b/libraries/embryo/src/lib/embryo_time.c new file mode 100644 index 0000000..8392ec3 --- /dev/null +++ b/libraries/embryo/src/lib/embryo_time.c | |||
@@ -0,0 +1,93 @@ | |||
1 | #ifdef HAVE_CONFIG_H | ||
2 | # include "config.h" | ||
3 | #endif | ||
4 | |||
5 | #ifndef HAVE_GETTIMEOFDAY | ||
6 | # error "Your platform isn't supported yet" | ||
7 | #endif | ||
8 | |||
9 | #include <sys/time.h> | ||
10 | #include <time.h> | ||
11 | |||
12 | #ifdef _MSC_VER | ||
13 | # include <winsock2.h> | ||
14 | #endif | ||
15 | |||
16 | #ifdef HAVE_EVIL | ||
17 | # include <Evil.h> | ||
18 | #endif | ||
19 | |||
20 | #include "Embryo.h" | ||
21 | #include "embryo_private.h" | ||
22 | |||
23 | /* exported time api */ | ||
24 | |||
25 | static Embryo_Cell | ||
26 | _embryo_time_seconds(Embryo_Program *ep __UNUSED__, Embryo_Cell *params __UNUSED__) | ||
27 | { | ||
28 | struct timeval timev; | ||
29 | double t; | ||
30 | float f; | ||
31 | |||
32 | gettimeofday(&timev, NULL); | ||
33 | t = (double)(timev.tv_sec - ((timev.tv_sec / (60 * 60 * 24)) * (60 * 60 * 24))) | ||
34 | + (((double)timev.tv_usec) / 1000000); | ||
35 | f = (float)t; | ||
36 | return EMBRYO_FLOAT_TO_CELL(f); | ||
37 | } | ||
38 | |||
39 | static Embryo_Cell | ||
40 | _embryo_time_date(Embryo_Program *ep, Embryo_Cell *params) | ||
41 | { | ||
42 | static time_t last_tzset = 0; | ||
43 | struct timeval timev; | ||
44 | struct tm *tm; | ||
45 | time_t tt; | ||
46 | |||
47 | if (params[0] != (8 * sizeof(Embryo_Cell))) return 0; | ||
48 | gettimeofday(&timev, NULL); | ||
49 | tt = (time_t)(timev.tv_sec); | ||
50 | if ((tt > (last_tzset + 1)) || | ||
51 | (tt < (last_tzset - 1))) | ||
52 | { | ||
53 | last_tzset = tt; | ||
54 | tzset(); | ||
55 | } | ||
56 | tm = localtime(&tt); | ||
57 | if (tm) | ||
58 | { | ||
59 | Embryo_Cell *cptr; | ||
60 | double t; | ||
61 | float f; | ||
62 | |||
63 | cptr = embryo_data_address_get(ep, params[1]); | ||
64 | if (cptr) *cptr = tm->tm_year + 1900; | ||
65 | cptr = embryo_data_address_get(ep, params[2]); | ||
66 | if (cptr) *cptr = tm->tm_mon + 1; | ||
67 | cptr = embryo_data_address_get(ep, params[3]); | ||
68 | if (cptr) *cptr = tm->tm_mday; | ||
69 | cptr = embryo_data_address_get(ep, params[4]); | ||
70 | if (cptr) *cptr = tm->tm_yday; | ||
71 | cptr = embryo_data_address_get(ep, params[5]); | ||
72 | if (cptr) *cptr = (tm->tm_wday + 6) % 7; | ||
73 | cptr = embryo_data_address_get(ep, params[6]); | ||
74 | if (cptr) *cptr = tm->tm_hour; | ||
75 | cptr = embryo_data_address_get(ep, params[7]); | ||
76 | if (cptr) *cptr = tm->tm_min; | ||
77 | cptr = embryo_data_address_get(ep, params[8]); | ||
78 | t = (double)tm->tm_sec + (((double)timev.tv_usec) / 1000000); | ||
79 | f = (float)t; | ||
80 | if (cptr) *cptr = EMBRYO_FLOAT_TO_CELL(f); | ||
81 | |||
82 | } | ||
83 | return 0; | ||
84 | } | ||
85 | |||
86 | /* functions used by the rest of embryo */ | ||
87 | |||
88 | void | ||
89 | _embryo_time_init(Embryo_Program *ep) | ||
90 | { | ||
91 | embryo_program_native_call_add(ep, "seconds", _embryo_time_seconds); | ||
92 | embryo_program_native_call_add(ep, "date", _embryo_time_date); | ||
93 | } | ||