forked from openresty/lua-nginx-module
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathngx_http_lua_time.c
More file actions
278 lines (190 loc) · 5.83 KB
/
Copy pathngx_http_lua_time.c
File metadata and controls
278 lines (190 loc) · 5.83 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Copyright (C) Xiaozhe Wang (chaoslawful)
* Copyright (C) Yichun Zhang (agentzh)
*/
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "ngx_http_lua_time.h"
#include "ngx_http_lua_util.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_req_start_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;
}
static int
ngx_http_lua_ngx_req_start_time(lua_State *L)
{
ngx_http_request_t *r;
r = ngx_http_lua_get_req(L);
if (r == NULL) {
return luaL_error(L, "no request found");
}
lua_pushnumber(L, (lua_Number) (r->start_sec + r->start_msec / 1000.0L));
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");
}
void
ngx_http_lua_inject_req_time_api(lua_State *L)
{
lua_pushcfunction(L, ngx_http_lua_ngx_req_start_time);
lua_setfield(L, -2, "start_time");
}
#ifndef NGX_LUA_NO_FFI_API
double
ngx_http_lua_ffi_now(void)
{
ngx_time_t *tp;
tp = ngx_timeofday();
return tp->sec + tp->msec / 1000.0;
}
double
ngx_http_lua_ffi_req_start_time(ngx_http_request_t *r)
{
return r->start_sec + r->start_msec / 1000.0;
}
long
ngx_http_lua_ffi_time(void)
{
return (long) ngx_time();
}
#endif /* NGX_LUA_NO_FFI_API */
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */