Skip to content

Commit b902b26

Browse files
spacewanderagentzh
authored andcommitted
feature: re-implemented the remaining time related Lua APIs with FFI (like ngx.update_time, ngx.http_time, ngx.parse_http_time, and etc.).
Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
1 parent ef0e37a commit b902b26

File tree

3 files changed

+272
-0
lines changed

3 files changed

+272
-0
lines changed

README.markdown

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ API Implemented
205205

206206
* [ngx.time](https://github.com/openresty/lua-nginx-module#ngxtime)
207207
* [ngx.now](https://github.com/openresty/lua-nginx-module#ngxnow)
208+
* [ngx.update_time](https://github.com/openresty/lua-nginx-module#ngxupdate_time)
209+
* [ngx.localtime](https://github.com/openresty/lua-nginx-module#ngxlocaltime)
210+
* [ngx.utctime](https://github.com/openresty/lua-nginx-module#ngxutctime)
211+
* [ngx.cookie_time](https://github.com/openresty/lua-nginx-module#ngxcookie_time)
212+
* [ngx.http_time](https://github.com/openresty/lua-nginx-module#ngxhttp_time)
213+
* [ngx.parse_http_time](https://github.com/openresty/lua-nginx-module#ngxparse_http_time)
208214

209215
[Back to TOC](#table-of-contents)
210216

lib/resty/core/time.lua

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,29 @@ local ffi = require 'ffi'
55
local base = require "resty.core.base"
66

77

8+
local error = error
89
local tonumber = tonumber
10+
local type = type
911
local C = ffi.C
12+
local ffi_new = ffi.new
13+
local ffi_str = ffi.string
14+
local time_val = ffi_new("long[1]")
15+
local get_string_buf = base.get_string_buf
1016
local ngx = ngx
17+
local FFI_ERROR = ngx.ERROR
1118

1219

1320
ffi.cdef[[
1421
double ngx_http_lua_ffi_now(void);
1522
long ngx_http_lua_ffi_time(void);
23+
void ngx_http_lua_ffi_today(unsigned char *buf);
24+
void ngx_http_lua_ffi_localtime(unsigned char *buf);
25+
void ngx_http_lua_ffi_utctime(unsigned char *buf);
26+
void ngx_http_lua_ffi_update_time(void);
27+
int ngx_http_lua_ffi_cookie_time(unsigned char *buf, long t);
28+
void ngx_http_lua_ffi_http_time(unsigned char *buf, long t);
29+
void ngx_http_lua_ffi_parse_http_time(const unsigned char *str, size_t len,
30+
long *time);
1631
]]
1732

1833

@@ -26,6 +41,81 @@ function ngx.time()
2641
end
2742

2843

44+
function ngx.update_time()
45+
C.ngx_http_lua_ffi_update_time()
46+
end
47+
48+
49+
function ngx.today()
50+
-- the format of today is 2010-11-19
51+
local today_buf_size = 10
52+
local buf = get_string_buf(today_buf_size)
53+
C.ngx_http_lua_ffi_today(buf)
54+
return ffi_str(buf, today_buf_size)
55+
end
56+
57+
58+
function ngx.localtime()
59+
-- the format of localtime is 2010-11-19 20:56:31
60+
local localtime_buf_size = 19
61+
local buf = get_string_buf(localtime_buf_size)
62+
C.ngx_http_lua_ffi_localtime(buf)
63+
return ffi_str(buf, localtime_buf_size)
64+
end
65+
66+
67+
function ngx.utctime()
68+
-- the format of utctime is 2010-11-19 20:56:31
69+
local utctime_buf_size = 19
70+
local buf = get_string_buf(utctime_buf_size)
71+
C.ngx_http_lua_ffi_utctime(buf)
72+
return ffi_str(buf, utctime_buf_size)
73+
end
74+
75+
76+
function ngx.cookie_time(sec)
77+
if type(sec) ~= "number" then
78+
return error("number argument only")
79+
end
80+
81+
-- the format of cookie time is Mon, 28-Sep-2038 06:00:00 GMT
82+
-- or Mon, 28-Sep-18 06:00:00 GMT
83+
local cookie_time_buf_size = 29
84+
local buf = get_string_buf(cookie_time_buf_size)
85+
local used_size = C.ngx_http_lua_ffi_cookie_time(buf, sec)
86+
return ffi_str(buf, used_size)
87+
end
88+
89+
90+
function ngx.http_time(sec)
91+
if type(sec) ~= "number" then
92+
return error("number argument only")
93+
end
94+
95+
-- the format of http time is Mon, 28 Sep 1970 06:00:00 GMT
96+
local http_time_buf_size = 29
97+
local buf = get_string_buf(http_time_buf_size)
98+
C.ngx_http_lua_ffi_http_time(buf, sec)
99+
return ffi_str(buf, http_time_buf_size)
100+
end
101+
102+
103+
function ngx.parse_http_time(time_str)
104+
if type(time_str) ~= "string" then
105+
return error("string argument only")
106+
end
107+
108+
C.ngx_http_lua_ffi_parse_http_time(time_str, #time_str, time_val)
109+
110+
local res = time_val[0]
111+
if res == FFI_ERROR then
112+
return nil
113+
end
114+
115+
return tonumber(res)
116+
end
117+
118+
29119
return {
30120
version = base.version
31121
}

t/time.t

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,179 @@ qr/\[TRACE \d+ content_by_lua\(nginx\.conf:\d+\):3 loop\]/
103103
[error]
104104
bad argument type
105105
stitch
106+
107+
108+
109+
=== TEST 3: ngx.update_time()
110+
--- http_config eval: $::HttpConfig
111+
--- config
112+
location = /t {
113+
content_by_lua_block {
114+
local start = ngx.now()
115+
for _ = 1, 1e5 do
116+
ngx.update_time()
117+
end
118+
ngx.say(ngx.now() - start > 0)
119+
}
120+
}
121+
--- request
122+
GET /t
123+
--- response_body
124+
true
125+
--- error_log eval
126+
qr/\[TRACE \d+ content_by_lua\(nginx\.conf:\d+\):3 loop\]/
127+
--- no_error_log
128+
[error]
129+
bad argument type
130+
stitch
131+
132+
133+
134+
=== TEST 4: ngx.today()
135+
--- http_config eval: $::HttpConfig
136+
--- config
137+
location = /t {
138+
content_by_lua_block {
139+
local t
140+
for i = 1, 500 do
141+
t = ngx.today()
142+
end
143+
ngx.say(t)
144+
}
145+
}
146+
--- request
147+
GET /t
148+
--- response_body_like: ^\d{4}-\d{2}-\d{2}
149+
--- error_log eval
150+
qr/\[TRACE \d+ content_by_lua\(nginx\.conf:\d+\):3 loop\]/
151+
--- no_error_log
152+
[error]
153+
bad argument type
154+
stitch
155+
156+
157+
158+
=== TEST 5: ngx.localtime()
159+
--- http_config eval: $::HttpConfig
160+
--- config
161+
location = /t {
162+
content_by_lua_block {
163+
local t
164+
for i = 1, 500 do
165+
t = ngx.localtime()
166+
end
167+
ngx.say(t)
168+
}
169+
}
170+
--- request
171+
GET /t
172+
--- response_body_like: ^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$
173+
--- error_log eval
174+
qr/\[TRACE \d+ content_by_lua\(nginx\.conf:\d+\):3 loop\]/
175+
--- no_error_log
176+
[error]
177+
bad argument type
178+
stitch
179+
180+
181+
182+
=== TEST 6: ngx.utctime()
183+
--- http_config eval: $::HttpConfig
184+
--- config
185+
location = /t {
186+
content_by_lua_block {
187+
local t
188+
for i = 1, 500 do
189+
t = ngx.utctime()
190+
end
191+
ngx.say(t)
192+
}
193+
}
194+
--- request
195+
GET /t
196+
--- response_body_like: ^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$
197+
--- error_log eval
198+
qr/\[TRACE \d+ content_by_lua\(nginx\.conf:\d+\):3 loop\]/
199+
--- no_error_log
200+
[error]
201+
bad argument type
202+
stitch
203+
204+
205+
206+
=== TEST 7: ngx.cookie_time()
207+
--- http_config eval: $::HttpConfig
208+
--- config
209+
location /t {
210+
content_by_lua_block {
211+
local t
212+
for i = 1, 500 do
213+
t = ngx.cookie_time(1290079655)
214+
end
215+
ngx.say(t)
216+
ngx.say(ngx.cookie_time(2200000000))
217+
}
218+
}
219+
--- request
220+
GET /t
221+
--- response_body
222+
Thu, 18-Nov-10 11:27:35 GMT
223+
Sun, 18-Sep-2039 23:06:40 GMT
224+
--- error_log eval
225+
qr/\[TRACE \d+ content_by_lua\(nginx\.conf:\d+\):3 loop\]/
226+
--- no_error_log
227+
[error]
228+
bad argument type
229+
stitch
230+
231+
232+
233+
=== TEST 8: ngx.http_time()
234+
--- http_config eval: $::HttpConfig
235+
--- config
236+
location /t {
237+
content_by_lua_block {
238+
local t
239+
for i = 1, 500 do
240+
t = ngx.http_time(1290079655)
241+
end
242+
ngx.say(t)
243+
}
244+
}
245+
--- request
246+
GET /t
247+
--- response_body
248+
Thu, 18 Nov 2010 11:27:35 GMT
249+
--- error_log eval
250+
qr/\[TRACE \d+ content_by_lua\(nginx\.conf:\d+\):3 loop\]/
251+
--- no_error_log
252+
[error]
253+
bad argument type
254+
stitch
255+
256+
257+
258+
=== TEST 9: ngx.parse_http_time()
259+
--- http_config eval: $::HttpConfig
260+
--- config
261+
location /t {
262+
content_by_lua_block {
263+
local t
264+
for i = 1, 500 do
265+
t = ngx.parse_http_time("Thu, 18 Nov 2010 11:27:35 GMT")
266+
end
267+
ngx.say(t)
268+
ngx.say(ngx.parse_http_time("Thu, Nov 2010"))
269+
}
270+
}
271+
--- request
272+
GET /t
273+
--- response_body
274+
1290079655
275+
nil
276+
--- error_log eval
277+
qr/\[TRACE \d+ content_by_lua\(nginx\.conf:\d+\):3 loop\]/
278+
--- no_error_log
279+
[error]
280+
bad argument type
281+
stitch

0 commit comments

Comments
 (0)