-
Notifications
You must be signed in to change notification settings - Fork 4
/
bdate.c
163 lines (137 loc) · 5.85 KB
/
bdate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <stdlib.h>
#include <math.h>
#define _XOPEN_SOURCE /* See feature_test_macros(7) */
#include <time.h>
#include "bstring.h"
#include "bobject.h"
#include "berror.h"
#include "bparser.h"
#include "bdate.h"
#define DEFAULT_FORMATTED_STRING_LENGTH 255
static TValue * primitive_Date_now(bean_State * B, TValue * thisVal UNUSED, TValue * args UNUSED, int argc UNUSED) {
assert_with_message(thisVal == G(B)->dproto, "This method just can call by prototype 'Date'.");
TValue * ret = TV_MALLOC;
time_t timer = time(NULL);
setnvalue(ret, timer);
return ret;
}
static TValue * primitive_Date_build(bean_State * B, TValue * thisVal UNUSED, TValue * args UNUSED, int argc UNUSED) {
assert_with_message(thisVal == G(B)->dproto, "This method just can call by prototype 'Date'.");
TValue * ret = TV_MALLOC;
Date * date = malloc(sizeof(Date));
time_t t = time(NULL);
date->time = t;
setdatevalue(ret, date);
return ret;
}
static TValue * primitive_Date_parse(bean_State * B, TValue * thisVal UNUSED, TValue * args UNUSED, int argc UNUSED) {
assert_with_message(thisVal == G(B)->dproto, "This method just can call by prototype 'Date'.");
TValue * ret = TV_MALLOC;
Date * date = malloc(sizeof(Date));
char * formatString = "%Y-%m-%d %H:%M:%S %z"; // default formater
assert(argc >= 1);
TValue * tvTime = tvalue_inspect(B, &args[0]);
char * timeStr = getstr(svalue(tvTime));
if (argc >= 2) {
TValue * tvFormatter = tvalue_inspect(B, &args[1]);
formatString = getstr(svalue(tvFormatter));
}
struct tm t;
memset(&t, 0, sizeof(struct tm));
strptime(timeStr, formatString, &t);
date->time = mktime(&t);
setdatevalue(ret, date);
return ret;
}
static TValue * primitive_Date_format(bean_State * B, TValue * thisVal UNUSED, TValue * args UNUSED, int argc UNUSED) {
TValue * ret = TV_MALLOC;
Date * date = datevalue(thisVal);
char * formatString = "%Y-%m-%d %H:%M:%S %z"; // default formater
char * timezone = NULL;
assert(argc >= 0);
if (argc >= 1) {
TValue * tvFormatter = tvalue_inspect(B, &args[0]);
formatString = getstr(svalue(tvFormatter));
}
if (argc >= 2) {
TValue * tvZone = tvalue_inspect(B, &args[1]);
char * string = getstr(svalue(tvZone));
timezone = malloc(strlen(string) + 2);
sprintf(timezone, ":%s", string);
}
char * tz = NULL;
if (timezone) {
tz = getenv("TZ");
setenv("TZ", timezone, 1);
}
struct tm * tm = localtime(&date->time);
char s[DEFAULT_FORMATTED_STRING_LENGTH];
strftime(s, DEFAULT_FORMATTED_STRING_LENGTH, formatString, tm);
if (timezone) {
tz ? setenv("TZ", tz, 1) : unsetenv("TZ");
}
TString * ts = beanS_newlstr(B, s, strlen(s));
setsvalue(ret, ts);
return ret;
}
#define FROM_BROKEN_DOWN_TIME(attribute) do { \
TValue * ret = TV_MALLOC; \
Date * date = datevalue(thisVal); \
time_t time = date -> time; \
struct tm * t = localtime(&time); \
setnvalue(ret, attribute); \
return ret; \
} while(0) \
static TValue * primitive_Date_getYear(bean_State * B, TValue * thisVal, TValue * args UNUSED, int argc UNUSED) {
FROM_BROKEN_DOWN_TIME(t->tm_year + 1900);
}
static TValue * primitive_Date_getMonth(bean_State * B, TValue * thisVal, TValue * args UNUSED, int argc UNUSED) {
FROM_BROKEN_DOWN_TIME(t->tm_mon);
}
static TValue * primitive_Date_getDate(bean_State * B, TValue * thisVal, TValue * args UNUSED, int argc UNUSED) {
FROM_BROKEN_DOWN_TIME(t->tm_mday);
}
static TValue * primitive_Date_getHours(bean_State * B, TValue * thisVal, TValue * args UNUSED, int argc UNUSED) {
FROM_BROKEN_DOWN_TIME(t->tm_hour);
}
static TValue * primitive_Date_getMinutes(bean_State * B, TValue * thisVal, TValue * args UNUSED, int argc UNUSED) {
FROM_BROKEN_DOWN_TIME(t->tm_min);
}
static TValue * primitive_Date_getSeconds(bean_State * B, TValue * thisVal, TValue * args UNUSED, int argc UNUSED) {
FROM_BROKEN_DOWN_TIME(t->tm_sec);
}
static TValue * primitive_Date_getWeekDay(bean_State * B, TValue * thisVal, TValue * args UNUSED, int argc UNUSED) {
FROM_BROKEN_DOWN_TIME(t->tm_wday);
}
static TValue * primitive_Date_getTimezoneOffset(bean_State * B, TValue * thisVal, TValue * args UNUSED, int argc UNUSED) {
// https://www.gnu.org/software/libc/manual/html_node/Time-Zone-Functions.html
TValue * ret = TV_MALLOC;
Date * date = datevalue(thisVal);
time_t time = date -> time;
struct tm * tm = localtime(&time); // Call the tzset to set the timezone from environment variable
setnvalue(ret, tm->tm_gmtoff);
return ret;
}
TValue * init_Date(bean_State * B) {
srand(time(0));
global_State * G = B->l_G;
TValue * proto = TV_MALLOC;
Hash * h = init_hash(B);
sethashvalue(proto, h);
set_prototype_function(B, "now", 3, primitive_Date_now, hhvalue(proto));
set_prototype_function(B, "build", 5, primitive_Date_build, hhvalue(proto));
set_prototype_function(B, "parse", 5, primitive_Date_parse, hhvalue(proto));
set_prototype_function(B, "getYear", 7, primitive_Date_getYear, hhvalue(proto));
set_prototype_function(B, "getMonth", 8, primitive_Date_getMonth, hhvalue(proto));
set_prototype_function(B, "getDate", 7, primitive_Date_getDate, hhvalue(proto));
set_prototype_function(B, "getHours", 8, primitive_Date_getHours, hhvalue(proto));
set_prototype_function(B, "getMinutes", 10, primitive_Date_getMinutes, hhvalue(proto));
set_prototype_function(B, "getSeconds", 10, primitive_Date_getSeconds, hhvalue(proto));
set_prototype_function(B, "getWeekDay", 10, primitive_Date_getWeekDay, hhvalue(proto));
set_prototype_function(B, "getTimezoneOffset", 17, primitive_Date_getTimezoneOffset, hhvalue(proto));
set_prototype_function(B, "format", 6, primitive_Date_format, hhvalue(proto));
TValue * date = TV_MALLOC;
setsvalue(date, beanS_newlstr(B, "Date", 4));
hash_set(B, G->globalScope->variables, date, proto);
return proto;
}