-
Notifications
You must be signed in to change notification settings - Fork 2k
/
ngx_http_lua_time.c
217 lines (147 loc) · 4.77 KB
/
ngx_http_lua_time.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "ngx_http_lua_time.h"
static int ngx_http_lua_ngx_today(lua_State *L);
static int ngx_http_lua_ngx_time(lua_State *L);
static int ngx_http_lua_ngx_now(lua_State *L);
static int ngx_http_lua_ngx_localtime(lua_State *L);
static int ngx_http_lua_ngx_utctime(lua_State *L);
static int ngx_http_lua_ngx_cookie_time(lua_State *L);
static int ngx_http_lua_ngx_http_time(lua_State *L);
static int ngx_http_lua_ngx_parse_http_time(lua_State *L);
static int ngx_http_lua_ngx_update_time(lua_State *L);
static int
ngx_http_lua_ngx_today(lua_State *L)
{
time_t now;
ngx_tm_t tm;
u_char buf[sizeof("2010-11-19") - 1];
now = ngx_time();
ngx_gmtime(now + ngx_cached_time->gmtoff * 60, &tm);
ngx_sprintf(buf, "%04d-%02d-%02d", tm.ngx_tm_year, tm.ngx_tm_mon,
tm.ngx_tm_mday);
lua_pushlstring(L, (char *) buf, sizeof(buf));
return 1;
}
static int
ngx_http_lua_ngx_localtime(lua_State *L)
{
ngx_tm_t tm;
u_char buf[sizeof("2010-11-19 20:56:31") - 1];
ngx_gmtime(ngx_time() + ngx_cached_time->gmtoff * 60, &tm);
ngx_sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d", tm.ngx_tm_year,
tm.ngx_tm_mon, tm.ngx_tm_mday, tm.ngx_tm_hour, tm.ngx_tm_min,
tm.ngx_tm_sec);
lua_pushlstring(L, (char *) buf, sizeof(buf));
return 1;
}
static int
ngx_http_lua_ngx_time(lua_State *L)
{
lua_pushnumber(L, (lua_Number) ngx_time());
return 1;
}
static int
ngx_http_lua_ngx_now(lua_State *L)
{
ngx_time_t *tp;
tp = ngx_timeofday();
lua_pushnumber(L, (lua_Number) (tp->sec + tp->msec / 1000.0L));
return 1;
}
static int
ngx_http_lua_ngx_update_time(lua_State *L)
{
ngx_time_update();
return 0;
}
static int
ngx_http_lua_ngx_utctime(lua_State *L)
{
ngx_tm_t tm;
u_char buf[sizeof("2010-11-19 20:56:31") - 1];
ngx_gmtime(ngx_time(), &tm);
ngx_sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d", tm.ngx_tm_year,
tm.ngx_tm_mon, tm.ngx_tm_mday, tm.ngx_tm_hour, tm.ngx_tm_min,
tm.ngx_tm_sec);
lua_pushlstring(L, (char *) buf, sizeof(buf));
return 1;
}
static int
ngx_http_lua_ngx_cookie_time(lua_State *L)
{
time_t t;
u_char *p;
u_char buf[sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1];
if (lua_gettop(L) != 1) {
return luaL_error(L, "expecting one argument");
}
t = (time_t) luaL_checknumber(L, 1);
p = buf;
p = ngx_http_cookie_time(p, t);
lua_pushlstring(L, (char *) buf, p - buf);
return 1;
}
static int
ngx_http_lua_ngx_http_time(lua_State *L)
{
time_t t;
u_char *p;
u_char buf[sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1];
if (lua_gettop(L) != 1) {
return luaL_error(L, "expecting one argument");
}
t = (time_t) luaL_checknumber(L, 1);
p = buf;
p = ngx_http_time(p, t);
lua_pushlstring(L, (char *) buf, p - buf);
return 1;
}
static int
ngx_http_lua_ngx_parse_http_time(lua_State *L)
{
u_char *p;
size_t len;
time_t time;
if (lua_gettop(L) != 1) {
return luaL_error(L, "expecting one argument");
}
p = (u_char *) luaL_checklstring(L, 1, &len);
time = ngx_http_parse_time(p, len);
if (time == NGX_ERROR) {
lua_pushnil(L);
return 1;
}
lua_pushnumber(L, (lua_Number) time);
return 1;
}
void
ngx_http_lua_inject_time_api(lua_State *L)
{
lua_pushcfunction(L, ngx_http_lua_ngx_utctime);
lua_setfield(L, -2, "utctime");
lua_pushcfunction(L, ngx_http_lua_ngx_time);
lua_setfield(L, -2, "get_now_ts"); /* deprecated */
lua_pushcfunction(L, ngx_http_lua_ngx_localtime);
lua_setfield(L, -2, "get_now"); /* deprecated */
lua_pushcfunction(L, ngx_http_lua_ngx_localtime);
lua_setfield(L, -2, "localtime");
lua_pushcfunction(L, ngx_http_lua_ngx_time);
lua_setfield(L, -2, "time");
lua_pushcfunction(L, ngx_http_lua_ngx_now);
lua_setfield(L, -2, "now");
lua_pushcfunction(L, ngx_http_lua_ngx_update_time);
lua_setfield(L, -2, "update_time");
lua_pushcfunction(L, ngx_http_lua_ngx_today);
lua_setfield(L, -2, "get_today"); /* deprecated */
lua_pushcfunction(L, ngx_http_lua_ngx_today);
lua_setfield(L, -2, "today");
lua_pushcfunction(L, ngx_http_lua_ngx_cookie_time);
lua_setfield(L, -2, "cookie_time");
lua_pushcfunction(L, ngx_http_lua_ngx_http_time);
lua_setfield(L, -2, "http_time");
lua_pushcfunction(L, ngx_http_lua_ngx_parse_http_time);
lua_setfield(L, -2, "parse_http_time");
}