From ad2b41ff6563e6704e8a62703f751471227e18a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?agentzh=20=28=E7=AB=A0=E4=BA=A6=E6=98=A5=29?= Date: Thu, 18 Nov 2010 20:00:55 +0800 Subject: [PATCH] renamed ngx.get_today() to ngx.today(), ngx.get_now() to ngx.now(), and ngx.get_now_ts() to ngx.time(). --- README.markdown | 8 ++++---- src/ngx_http_lua_setby.c | 18 +++++++++++++++--- src/ngx_http_lua_util.c | 15 ++++++++++++--- t/{008-get_today.t => 008-today.t} | 8 ++++---- t/{012-get_now.t => 012-now.t} | 26 +++++++++++++------------- t/021-time.t | 2 +- 6 files changed, 49 insertions(+), 28 deletions(-) rename t/{008-get_today.t => 008-today.t} (72%) rename t/{012-get_now.t => 012-now.t} (56%) diff --git a/README.markdown b/README.markdown index 78fc9e10..6a54a4e9 100644 --- a/README.markdown +++ b/README.markdown @@ -503,23 +503,23 @@ Decode `str` as a base64 digest to the raw form newstr = ngx.decode_base64(str) -ngx.get_today() +ngx.today() --------------- Returns today's date (in the format `yyyy-mm-dd`) from nginx cached time (no syscall involved unlike Lua's date library). . -ngx.get_now() +ngx.now() ------------- Returns the current timestamp (in the format `yyyy-mm-dd hh:mm:ss`) of the nginx cached time (no syscall involved unlike Lua's date library). -ngx.get_now_ts() +ngx.time() ---------------- Returns the current timestamp (in seconds) of the nginx cached time (no syscall involved unlike Lua's date library). ngx.cookie_time(sec) -------------------- -Returns a formated string can be used as the cookie expiration time. The parameter `sec` is the timestamp in seconds (like those returned from `ngx.get_now_ts`). +Returns a formated string can be used as the cookie expiration time. The parameter `sec` is the timestamp in seconds (like those returned from `ngx.time`). ngx.say(ngx.cookie_time(1290079655)) # yields "Thu, 18-Nov-10 11:27:35 GMT" diff --git a/src/ngx_http_lua_setby.c b/src/ngx_http_lua_setby.c index 7ff6fc02..b309875a 100644 --- a/src/ngx_http_lua_setby.c +++ b/src/ngx_http_lua_setby.c @@ -107,13 +107,25 @@ ngx_http_lua_set_by_lua_env(lua_State *L, ngx_http_request_t *r, size_t nargs, lua_setfield(L, -2, "md5"); lua_pushcfunction(L, ngx_http_lua_ngx_get_now_ts); - lua_setfield(L, -2, "get_now_ts"); + lua_setfield(L, -2, "get_now_ts"); /* deprecated */ + + lua_pushcfunction(L, ngx_http_lua_ngx_get_now_ts); + lua_setfield(L, -2, "time"); lua_pushcfunction(L, ngx_http_lua_ngx_get_now); - lua_setfield(L, -2, "get_now"); + lua_setfield(L, -2, "get_now"); /* deprecated */ + + lua_pushcfunction(L, ngx_http_lua_ngx_get_now); + lua_setfield(L, -2, "now"); lua_pushcfunction(L, ngx_http_lua_ngx_get_today); - lua_setfield(L, -2, "get_today"); + lua_setfield(L, -2, "get_today"); /* deprecated */ + + lua_pushcfunction(L, ngx_http_lua_ngx_get_today); + lua_setfield(L, -2, "today"); + + lua_pushcfunction(L, ngx_http_lua_ngx_cookie_time); + lua_setfield(L, -2, "cookie_time"); lua_setfield(L, -2, "ngx"); /* }}} */ diff --git a/src/ngx_http_lua_util.c b/src/ngx_http_lua_util.c index ccf72d5f..8a6d71b0 100644 --- a/src/ngx_http_lua_util.c +++ b/src/ngx_http_lua_util.c @@ -504,13 +504,22 @@ init_ngx_lua_globals(lua_State *L) lua_setfield(L, -2, "md5"); lua_pushcfunction(L, ngx_http_lua_ngx_get_now_ts); - lua_setfield(L, -2, "get_now_ts"); + lua_setfield(L, -2, "get_now_ts"); /* deprecated */ + + lua_pushcfunction(L, ngx_http_lua_ngx_get_now_ts); + lua_setfield(L, -2, "time"); lua_pushcfunction(L, ngx_http_lua_ngx_get_now); - lua_setfield(L, -2, "get_now"); + lua_setfield(L, -2, "get_now"); /* deprecated */ + + lua_pushcfunction(L, ngx_http_lua_ngx_get_now); + lua_setfield(L, -2, "now"); lua_pushcfunction(L, ngx_http_lua_ngx_get_today); - lua_setfield(L, -2, "get_today"); + lua_setfield(L, -2, "get_today"); /* deprecated */ + + lua_pushcfunction(L, ngx_http_lua_ngx_get_today); + lua_setfield(L, -2, "today"); lua_pushcfunction(L, ngx_http_lua_ngx_cookie_time); lua_setfield(L, -2, "cookie_time"); diff --git a/t/008-get_today.t b/t/008-today.t similarity index 72% rename from t/008-get_today.t rename to t/008-today.t index f49ed9ab..e28dce52 100644 --- a/t/008-get_today.t +++ b/t/008-today.t @@ -16,10 +16,10 @@ run_tests(); __DATA__ -=== TEST 1: use ngx.get_today in content_by_lua +=== TEST 1: use ngx.today in content_by_lua --- config location = /today { - content_by_lua 'ngx.say(ngx.get_today())'; + content_by_lua 'ngx.say(ngx.today())'; } --- request GET /today @@ -27,10 +27,10 @@ GET /today -=== TEST 2: use ngx.get_today in set_by_lua +=== TEST 2: use ngx.today in set_by_lua --- config location = /today { - set_by_lua $a 'return ngx.get_today()'; + set_by_lua $a 'return ngx.today()'; echo $a; } --- request diff --git a/t/012-get_now.t b/t/012-now.t similarity index 56% rename from t/012-get_now.t rename to t/012-now.t index 87bf939f..79795473 100644 --- a/t/012-get_now.t +++ b/t/012-now.t @@ -1,4 +1,4 @@ -# vim:set ft=perl ts=4 sw=4 et fdm=marker: +# vim:set ft= ts=4 sw=4 et fdm=marker: use lib 'lib'; use Test::Nginx::Socket; @@ -16,10 +16,10 @@ run_tests(); __DATA__ -=== TEST 1: use ngx.get_now in content_by_lua +=== TEST 1: use ngx.now in content_by_lua --- config location = /now { - content_by_lua 'ngx.say(ngx.get_now())'; + content_by_lua 'ngx.say(ngx.now())'; } --- request GET /now @@ -27,10 +27,10 @@ GET /now -=== TEST 2: use ngx.get_now in set_by_lua +=== TEST 2: use ngx.now in set_by_lua --- config location = /now { - set_by_lua $a 'return ngx.get_now()'; + set_by_lua $a 'return ngx.now()'; echo $a; } --- request @@ -39,24 +39,24 @@ GET /now -=== TEST 3: use ngx.get_now_ts in content_by_lua +=== TEST 3: use ngx.time in content_by_lua --- config - location = /now_ts { - content_by_lua 'ngx.say(ngx.get_now_ts())'; + location = /time { + content_by_lua 'ngx.say(ngx.time())'; } --- request -GET /now_ts +GET /time --- response_body_like: ^\d{10}$ -=== TEST 4: use ngx.get_now_ts in set_by_lua +=== TEST 4: use ngx.time in set_by_lua --- config - location = /now_ts { - set_by_lua $a 'return ngx.get_now_ts()'; + location = /time { + set_by_lua $a 'return ngx.time()'; echo $a; } --- request -GET /now_ts +GET /time --- response_body_like: ^\d{10}$ diff --git a/t/021-time.t b/t/021-time.t index 4f03d616..d865ebd7 100644 --- a/t/021-time.t +++ b/t/021-time.t @@ -21,7 +21,7 @@ __DATA__ --- config location /lua { content_by_lua ' - ngx.say(ngx.cookie_time(1290079655)) + ngx.say(ngx.cookie_time(ngx.time())) '; } --- request