3b7b91d661bc4bde740931fd44abe9adb70dc981
[framework/uifw/embryo.git] / src / lib / embryo_time.c
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 HAVE_EVIL
13 # include <Evil.h>
14 #endif
15
16 #include "Embryo.h"
17 #include "embryo_private.h"
18
19 /* exported time api */
20
21 static Embryo_Cell
22 _embryo_time_seconds(Embryo_Program *ep __UNUSED__, Embryo_Cell *params __UNUSED__)
23 {
24    struct timeval      timev;
25    double t;
26    float  f;
27
28    gettimeofday(&timev, NULL);
29    t = (double)(timev.tv_sec - ((timev.tv_sec / (60 * 60 * 24)) * (60 * 60 * 24)))
30      + (((double)timev.tv_usec) / 1000000);
31    f = (float)t;
32    return EMBRYO_FLOAT_TO_CELL(f);
33 }
34
35 static Embryo_Cell
36 _embryo_time_date(Embryo_Program *ep, Embryo_Cell *params)
37 {
38    static time_t       last_tzset = 0;
39    struct timeval      timev;
40    struct tm          *tm;
41    time_t              tt;
42
43    if (params[0] != (8 * sizeof(Embryo_Cell))) return 0;
44    gettimeofday(&timev, NULL);
45    tt = (time_t)(timev.tv_sec);
46    if ((tt > (last_tzset + 1)) ||
47        (tt < (last_tzset - 1)))
48      {
49         last_tzset = tt;
50         tzset();
51      }
52    tm = localtime(&tt);
53    if (tm)
54      {
55         Embryo_Cell *cptr;
56         double t;
57         float  f;
58
59         cptr = embryo_data_address_get(ep, params[1]);
60         if (cptr) *cptr = tm->tm_year + 1900;
61         cptr = embryo_data_address_get(ep, params[2]);
62         if (cptr) *cptr = tm->tm_mon + 1;
63         cptr = embryo_data_address_get(ep, params[3]);
64         if (cptr) *cptr = tm->tm_mday;
65         cptr = embryo_data_address_get(ep, params[4]);
66         if (cptr) *cptr = tm->tm_yday;
67         cptr = embryo_data_address_get(ep, params[5]);
68         if (cptr) *cptr = (tm->tm_wday + 6) % 7;
69         cptr = embryo_data_address_get(ep, params[6]);
70         if (cptr) *cptr = tm->tm_hour;
71         cptr = embryo_data_address_get(ep, params[7]);
72         if (cptr) *cptr = tm->tm_min;
73         cptr = embryo_data_address_get(ep, params[8]);
74         t = (double)tm->tm_sec + (((double)timev.tv_usec) / 1000000);
75         f = (float)t;
76         if (cptr) *cptr = EMBRYO_FLOAT_TO_CELL(f);
77
78      }
79    return 0;
80 }
81
82 /* functions used by the rest of embryo */
83
84 void
85 _embryo_time_init(Embryo_Program *ep)
86 {
87    embryo_program_native_call_add(ep, "seconds", _embryo_time_seconds);
88    embryo_program_native_call_add(ep, "date",    _embryo_time_date);
89 }