LCOV - code coverage report
Current view: top level - home/build/openresty-1.13.6.1/build/ngx_lua-0.10.11/src - ngx_http_lua_time.c (source / functions) Hit Total Coverage
Test: coverage ngix Lines: 30 95 31.6 %
Date: 2020-03-03 04:25:50 Functions: 2 15 13.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : 
       2             : /*
       3             :  * Copyright (C) Xiaozhe Wang (chaoslawful)
       4             :  * Copyright (C) Yichun Zhang (agentzh)
       5             :  */
       6             : 
       7             : 
       8             : #ifndef DDEBUG
       9             : #define DDEBUG 0
      10             : #endif
      11             : #include "ddebug.h"
      12             : 
      13             : 
      14             : #include "ngx_http_lua_time.h"
      15             : #include "ngx_http_lua_util.h"
      16             : 
      17             : 
      18             : static int ngx_http_lua_ngx_today(lua_State *L);
      19             : static int ngx_http_lua_ngx_time(lua_State *L);
      20             : static int ngx_http_lua_ngx_now(lua_State *L);
      21             : static int ngx_http_lua_ngx_localtime(lua_State *L);
      22             : static int ngx_http_lua_ngx_utctime(lua_State *L);
      23             : static int ngx_http_lua_ngx_cookie_time(lua_State *L);
      24             : static int ngx_http_lua_ngx_http_time(lua_State *L);
      25             : static int ngx_http_lua_ngx_parse_http_time(lua_State *L);
      26             : static int ngx_http_lua_ngx_update_time(lua_State *L);
      27             : static int ngx_http_lua_ngx_req_start_time(lua_State *L);
      28             : 
      29             : 
      30             : static int
      31           0 : ngx_http_lua_ngx_today(lua_State *L)
      32             : {
      33             :     time_t                   now;
      34             :     ngx_tm_t                 tm;
      35             :     u_char                   buf[sizeof("2010-11-19") - 1];
      36             : 
      37           0 :     now = ngx_time();
      38           0 :     ngx_gmtime(now + ngx_cached_time->gmtoff * 60, &tm);
      39             : 
      40           0 :     ngx_sprintf(buf, "%04d-%02d-%02d", tm.ngx_tm_year, tm.ngx_tm_mon,
      41             :                 tm.ngx_tm_mday);
      42             : 
      43           0 :     lua_pushlstring(L, (char *) buf, sizeof(buf));
      44             : 
      45           0 :     return 1;
      46             : }
      47             : 
      48             : 
      49             : static int
      50           0 : ngx_http_lua_ngx_localtime(lua_State *L)
      51             : {
      52             :     ngx_tm_t                 tm;
      53             : 
      54             :     u_char buf[sizeof("2010-11-19 20:56:31") - 1];
      55             : 
      56           0 :     ngx_gmtime(ngx_time() + ngx_cached_time->gmtoff * 60, &tm);
      57             : 
      58           0 :     ngx_sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d", tm.ngx_tm_year,
      59             :                 tm.ngx_tm_mon, tm.ngx_tm_mday, tm.ngx_tm_hour, tm.ngx_tm_min,
      60             :                 tm.ngx_tm_sec);
      61             : 
      62           0 :     lua_pushlstring(L, (char *) buf, sizeof(buf));
      63             : 
      64           0 :     return 1;
      65             : }
      66             : 
      67             : 
      68             : static int
      69           0 : ngx_http_lua_ngx_time(lua_State *L)
      70             : {
      71           0 :     lua_pushnumber(L, (lua_Number) ngx_time());
      72             : 
      73           0 :     return 1;
      74             : }
      75             : 
      76             : 
      77             : static int
      78           0 : ngx_http_lua_ngx_now(lua_State *L)
      79             : {
      80             :     ngx_time_t              *tp;
      81             : 
      82           0 :     tp = ngx_timeofday();
      83             : 
      84           0 :     lua_pushnumber(L, (lua_Number) (tp->sec + tp->msec / 1000.0L));
      85             : 
      86           0 :     return 1;
      87             : }
      88             : 
      89             : 
      90             : static int
      91           0 : ngx_http_lua_ngx_update_time(lua_State *L)
      92             : {
      93           0 :     ngx_time_update();
      94           0 :     return 0;
      95             : }
      96             : 
      97             : 
      98             : static int
      99           0 : ngx_http_lua_ngx_utctime(lua_State *L)
     100             : {
     101             :     ngx_tm_t       tm;
     102             :     u_char         buf[sizeof("2010-11-19 20:56:31") - 1];
     103             : 
     104           0 :     ngx_gmtime(ngx_time(), &tm);
     105             : 
     106           0 :     ngx_sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d", tm.ngx_tm_year,
     107             :                 tm.ngx_tm_mon, tm.ngx_tm_mday, tm.ngx_tm_hour, tm.ngx_tm_min,
     108             :                 tm.ngx_tm_sec);
     109             : 
     110           0 :     lua_pushlstring(L, (char *) buf, sizeof(buf));
     111             : 
     112           0 :     return 1;
     113             : }
     114             : 
     115             : 
     116             : static int
     117           0 : ngx_http_lua_ngx_cookie_time(lua_State *L)
     118             : {
     119             :     time_t                               t;
     120             :     u_char                              *p;
     121             : 
     122             :     u_char   buf[sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1];
     123             : 
     124           0 :     if (lua_gettop(L) != 1) {
     125           0 :         return luaL_error(L, "expecting one argument");
     126             :     }
     127             : 
     128           0 :     t = (time_t) luaL_checknumber(L, 1);
     129             : 
     130           0 :     p = buf;
     131           0 :     p = ngx_http_cookie_time(p, t);
     132             : 
     133           0 :     lua_pushlstring(L, (char *) buf, p - buf);
     134             : 
     135           0 :     return 1;
     136             : }
     137             : 
     138             : 
     139             : static int
     140           0 : ngx_http_lua_ngx_http_time(lua_State *L)
     141             : {
     142             :     time_t                               t;
     143             :     u_char                              *p;
     144             : 
     145             :     u_char   buf[sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1];
     146             : 
     147           0 :     if (lua_gettop(L) != 1) {
     148           0 :         return luaL_error(L, "expecting one argument");
     149             :     }
     150             : 
     151           0 :     t = (time_t) luaL_checknumber(L, 1);
     152             : 
     153           0 :     p = buf;
     154           0 :     p = ngx_http_time(p, t);
     155             : 
     156           0 :     lua_pushlstring(L, (char *) buf, p - buf);
     157             : 
     158           0 :     return 1;
     159             : }
     160             : 
     161             : 
     162             : static int
     163           0 : ngx_http_lua_ngx_parse_http_time(lua_State *L)
     164             : {
     165             :     u_char                              *p;
     166             :     size_t                               len;
     167             :     time_t                               time;
     168             : 
     169           0 :     if (lua_gettop(L) != 1) {
     170           0 :         return luaL_error(L, "expecting one argument");
     171             :     }
     172             : 
     173           0 :     p = (u_char *) luaL_checklstring(L, 1, &len);
     174             : 
     175           0 :     time = ngx_http_parse_time(p, len);
     176           0 :     if (time == NGX_ERROR) {
     177           0 :         lua_pushnil(L);
     178           0 :         return 1;
     179             :     }
     180             : 
     181           0 :     lua_pushnumber(L, (lua_Number) time);
     182             : 
     183           0 :     return 1;
     184             : }
     185             : 
     186             : 
     187             : static int
     188           0 : ngx_http_lua_ngx_req_start_time(lua_State *L)
     189             : {
     190             :     ngx_http_request_t  *r;
     191             : 
     192           0 :     r = ngx_http_lua_get_req(L);
     193           0 :     if (r == NULL) {
     194           0 :         return luaL_error(L, "no request found");
     195             :     }
     196             : 
     197           0 :     lua_pushnumber(L, (lua_Number) (r->start_sec + r->start_msec / 1000.0L));
     198           0 :     return 1;
     199             : }
     200             : 
     201             : 
     202             : void
     203          18 : ngx_http_lua_inject_time_api(lua_State *L)
     204             : {
     205          18 :     lua_pushcfunction(L, ngx_http_lua_ngx_utctime);
     206          18 :     lua_setfield(L, -2, "utctime");
     207             : 
     208          18 :     lua_pushcfunction(L, ngx_http_lua_ngx_time);
     209          18 :     lua_setfield(L, -2, "get_now_ts"); /* deprecated */
     210             : 
     211          18 :     lua_pushcfunction(L, ngx_http_lua_ngx_localtime);
     212          18 :     lua_setfield(L, -2, "get_now"); /* deprecated */
     213             : 
     214          18 :     lua_pushcfunction(L, ngx_http_lua_ngx_localtime);
     215          18 :     lua_setfield(L, -2, "localtime");
     216             : 
     217          18 :     lua_pushcfunction(L, ngx_http_lua_ngx_time);
     218          18 :     lua_setfield(L, -2, "time");
     219             : 
     220          18 :     lua_pushcfunction(L, ngx_http_lua_ngx_now);
     221          18 :     lua_setfield(L, -2, "now");
     222             : 
     223          18 :     lua_pushcfunction(L, ngx_http_lua_ngx_update_time);
     224          18 :     lua_setfield(L, -2, "update_time");
     225             : 
     226          18 :     lua_pushcfunction(L, ngx_http_lua_ngx_today);
     227          18 :     lua_setfield(L, -2, "get_today"); /* deprecated */
     228             : 
     229          18 :     lua_pushcfunction(L, ngx_http_lua_ngx_today);
     230          18 :     lua_setfield(L, -2, "today");
     231             : 
     232          18 :     lua_pushcfunction(L, ngx_http_lua_ngx_cookie_time);
     233          18 :     lua_setfield(L, -2, "cookie_time");
     234             : 
     235          18 :     lua_pushcfunction(L, ngx_http_lua_ngx_http_time);
     236          18 :     lua_setfield(L, -2, "http_time");
     237             : 
     238          18 :     lua_pushcfunction(L, ngx_http_lua_ngx_parse_http_time);
     239          18 :     lua_setfield(L, -2, "parse_http_time");
     240          18 : }
     241             : 
     242             : 
     243             : void
     244          18 : ngx_http_lua_inject_req_time_api(lua_State *L)
     245             : {
     246          18 :     lua_pushcfunction(L, ngx_http_lua_ngx_req_start_time);
     247          18 :     lua_setfield(L, -2, "start_time");
     248          18 : }
     249             : 
     250             : 
     251             : #ifndef NGX_LUA_NO_FFI_API
     252             : double
     253           0 : ngx_http_lua_ffi_now(void)
     254             : {
     255             :     ngx_time_t              *tp;
     256             : 
     257           0 :     tp = ngx_timeofday();
     258             : 
     259           0 :     return tp->sec + tp->msec / 1000.0;
     260             : }
     261             : 
     262             : 
     263             : double
     264           0 : ngx_http_lua_ffi_req_start_time(ngx_http_request_t *r)
     265             : {
     266           0 :     return r->start_sec + r->start_msec / 1000.0;
     267             : }
     268             : 
     269             : 
     270             : long
     271           0 : ngx_http_lua_ffi_time(void)
     272             : {
     273           0 :     return (long) ngx_time();
     274             : }
     275             : #endif /* NGX_LUA_NO_FFI_API */
     276             : 
     277             : 
     278             : /* vi:set ft=c ts=4 sw=4 et fdm=marker: */

Generated by: LCOV version 1.13