Skip to content

Instantly share code, notes, and snippets.

@selboo
Created June 21, 2022 04:48
Show Gist options
  • Save selboo/48b360812494830798f96e82d5218a50 to your computer and use it in GitHub Desktop.
Save selboo/48b360812494830798f96e82d5218a50 to your computer and use it in GitHub Desktop.
diff -ruNa openresty-1.21.4.1/bundle/nginx-1.21.4/src/core/ngx_times.c openresty-1.21.4.1.time_fmt/bundle/nginx-1.21.4/src/core/ngx_times.c
--- openresty-1.21.4.1/bundle/nginx-1.21.4/src/core/ngx_times.c 2021-11-05 13:06:14.000000000 +0800
+++ openresty-1.21.4.1.time_fmt/bundle/nginx-1.21.4/src/core/ngx_times.c 2022-05-20 13:16:54.000000000 +0800
@@ -31,8 +31,10 @@
volatile ngx_str_t ngx_cached_err_log_time;
volatile ngx_str_t ngx_cached_http_time;
volatile ngx_str_t ngx_cached_http_log_time;
+volatile ngx_str_t ngx_cached_http_log_time_data;
volatile ngx_str_t ngx_cached_http_log_iso8601;
volatile ngx_str_t ngx_cached_syslog_time;
+volatile ngx_tm_t *ngx_cached_tm;
#if !(NGX_WIN32)
@@ -45,6 +47,7 @@
static ngx_int_t cached_gmtoff;
#endif
+static ngx_tm_t cached_http_log_tm[NGX_TIME_SLOTS];
static ngx_time_t cached_time[NGX_TIME_SLOTS];
static u_char cached_err_log_time[NGX_TIME_SLOTS]
[sizeof("1970/09/28 12:00:00")];
@@ -52,6 +55,8 @@
[sizeof("Mon, 28 Sep 1970 06:00:00 GMT")];
static u_char cached_http_log_time[NGX_TIME_SLOTS]
[sizeof("28/Sep/1970:12:00:00 +0600")];
+static u_char cached_http_log_time_data[NGX_TIME_SLOTS]
+ [sizeof("1970-09-28 12:00:00")];
static u_char cached_http_log_iso8601[NGX_TIME_SLOTS]
[sizeof("1970-09-28T12:00:00+06:00")];
static u_char cached_syslog_time[NGX_TIME_SLOTS]
@@ -68,10 +73,12 @@
ngx_cached_err_log_time.len = sizeof("1970/09/28 12:00:00") - 1;
ngx_cached_http_time.len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1;
ngx_cached_http_log_time.len = sizeof("28/Sep/1970:12:00:00 +0600") - 1;
+ ngx_cached_http_log_time_data.len = sizeof("1970-09-28 12:00:00") - 1;
ngx_cached_http_log_iso8601.len = sizeof("1970-09-28T12:00:00+06:00") - 1;
ngx_cached_syslog_time.len = sizeof("Sep 28 12:00:00") - 1;
ngx_cached_time = &cached_time[0];
+ ngx_cached_tm = &cached_http_log_tm[0];
ngx_time_update();
}
@@ -80,7 +87,7 @@
void
ngx_time_update(void)
{
- u_char *p0, *p1, *p2, *p3, *p4;
+ u_char *p0, *p1, *p2, *p3, *p4, *p5;
ngx_tm_t tm, gmt;
time_t sec;
ngx_uint_t msec;
@@ -146,6 +153,7 @@
#endif
+ cached_http_log_tm[slot] = tm;
p1 = &cached_err_log_time[slot][0];
@@ -179,7 +187,15 @@
months[tm.ngx_tm_mon - 1], tm.ngx_tm_mday,
tm.ngx_tm_hour, tm.ngx_tm_min, tm.ngx_tm_sec);
+ p5 = &cached_http_log_time_data[slot][0];
+
+ (void) ngx_sprintf(p5, "%4d-%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);
+
ngx_memory_barrier();
+ ngx_cached_tm = &cached_http_log_tm[slot];
ngx_cached_time = tp;
ngx_cached_http_time.data = p0;
@@ -187,6 +203,7 @@
ngx_cached_http_log_time.data = p2;
ngx_cached_http_log_iso8601.data = p3;
ngx_cached_syslog_time.data = p4;
+ ngx_cached_http_log_time_data.data = p5;
ngx_unlock(&ngx_time_lock);
}
diff -ruNa openresty-1.21.4.1/bundle/nginx-1.21.4/src/core/ngx_times.h openresty-1.21.4.1.time_fmt/bundle/nginx-1.21.4/src/core/ngx_times.h
--- openresty-1.21.4.1/bundle/nginx-1.21.4/src/core/ngx_times.h 2021-11-05 13:06:14.000000000 +0800
+++ openresty-1.21.4.1.time_fmt/bundle/nginx-1.21.4/src/core/ngx_times.h 2022-05-20 13:17:23.000000000 +0800
@@ -31,14 +31,17 @@
#define ngx_next_time_n "mktime()"
+extern volatile ngx_tm_t *ngx_cached_tm;
extern volatile ngx_time_t *ngx_cached_time;
#define ngx_time() ngx_cached_time->sec
+#define ngx_time2() ngx_cached_time->sec - (ngx_cached_time->sec % 60)
#define ngx_timeofday() (ngx_time_t *) ngx_cached_time
extern volatile ngx_str_t ngx_cached_err_log_time;
extern volatile ngx_str_t ngx_cached_http_time;
extern volatile ngx_str_t ngx_cached_http_log_time;
+extern volatile ngx_str_t ngx_cached_http_log_time_data;
extern volatile ngx_str_t ngx_cached_http_log_iso8601;
extern volatile ngx_str_t ngx_cached_syslog_time;
diff -ruNa openresty-1.21.4.1/bundle/nginx-1.21.4/src/http/modules/ngx_http_log_module.c openresty-1.21.4.1.time_fmt/bundle/nginx-1.21.4/src/http/modules/ngx_http_log_module.c
--- openresty-1.21.4.1/bundle/nginx-1.21.4/src/http/modules/ngx_http_log_module.c 2022-05-16 14:44:45.000000000 +0800
+++ openresty-1.21.4.1.time_fmt/bundle/nginx-1.21.4/src/http/modules/ngx_http_log_module.c 2022-05-20 13:18:34.000000000 +0800
@@ -117,6 +117,8 @@
ngx_http_log_op_t *op);
static u_char *ngx_http_log_time(ngx_http_request_t *r, u_char *buf,
ngx_http_log_op_t *op);
+static u_char *ngx_http_log_time_data(ngx_http_request_t *r, u_char *buf,
+ ngx_http_log_op_t *op);
static u_char *ngx_http_log_iso8601(ngx_http_request_t *r, u_char *buf,
ngx_http_log_op_t *op);
static u_char *ngx_http_log_msec(ngx_http_request_t *r, u_char *buf,
@@ -244,6 +246,8 @@
{ ngx_string("pipe"), 1, ngx_http_log_pipe },
{ ngx_string("time_local"), sizeof("28/Sep/1970:12:00:00 +0600") - 1,
ngx_http_log_time },
+ { ngx_string("time_data"), sizeof("1970-09-28 12:00:00") - 1,
+ ngx_http_log_time_data },
{ ngx_string("time_iso8601"), sizeof("1970-09-28T12:00:00+06:00") - 1,
ngx_http_log_iso8601 },
{ ngx_string("msec"), NGX_TIME_T_LEN + 4, ngx_http_log_msec },
@@ -827,6 +831,13 @@
}
static u_char *
+ngx_http_log_time_data(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
+{
+ return ngx_cpymem(buf, ngx_cached_http_log_time_data.data,
+ ngx_cached_http_log_time_data.len);
+}
+
+static u_char *
ngx_http_log_iso8601(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
{
return ngx_cpymem(buf, ngx_cached_http_log_iso8601.data,
diff -ruNa openresty-1.21.4.1/bundle/nginx-1.21.4/src/http/ngx_http_variables.c openresty-1.21.4.1.time_fmt/bundle/nginx-1.21.4/src/http/ngx_http_variables.c
--- openresty-1.21.4.1/bundle/nginx-1.21.4/src/http/ngx_http_variables.c 2021-11-05 13:06:14.000000000 +0800
+++ openresty-1.21.4.1.time_fmt/bundle/nginx-1.21.4/src/http/ngx_http_variables.c 2022-05-20 13:19:44.000000000 +0800
@@ -144,6 +144,33 @@
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_variable_time_local(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_variable_time_data(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
+
+static ngx_int_t ngx_http_variable_unix_time(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_variable_unix_time2(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_variable_year(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_variable_year2(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_variable_month(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_variable_day(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_variable_hour(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_variable_hour12(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_variable_minute(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_variable_second(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_variable_time_http(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_variables_time_fmt(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, ngx_int_t t, ngx_int_t len);
/*
* TODO:
@@ -365,6 +392,42 @@
{ ngx_string("time_local"), NULL, ngx_http_variable_time_local,
0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
+ { ngx_string("time_data"), NULL, ngx_http_variable_time_data,
+ 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
+
+ { ngx_string("unix_time"), NULL, ngx_http_variable_unix_time,
+ 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
+
+ { ngx_string("unix_time2"), NULL, ngx_http_variable_unix_time2,
+ 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
+
+ { ngx_string("year"), NULL, ngx_http_variable_year,
+ 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
+
+ { ngx_string("year2"), NULL, ngx_http_variable_year2,
+ 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
+
+ { ngx_string("month"), NULL, ngx_http_variable_month,
+ 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
+
+ { ngx_string("day"), NULL, ngx_http_variable_day,
+ 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
+
+ { ngx_string("hour"), NULL, ngx_http_variable_hour,
+ 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
+
+ { ngx_string("hour12"), NULL, ngx_http_variable_hour12,
+ 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
+
+ { ngx_string("minute"), NULL, ngx_http_variable_minute,
+ 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
+
+ { ngx_string("second"), NULL, ngx_http_variable_second,
+ 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
+
+ { ngx_string("time_http"), NULL, ngx_http_variable_time_http,
+ 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
+
#if (NGX_HAVE_TCP_INFO)
{ ngx_string("tcpinfo_rtt"), NULL, ngx_http_variable_tcpinfo,
0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
@@ -2358,6 +2421,165 @@
return NGX_OK;
}
+static ngx_int_t
+ngx_http_variable_unix_time(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data)
+{
+ u_char *p;
+
+ p = ngx_pnalloc(r->pool, NGX_TIME_T_LEN);
+ if (p == NULL) {
+ return NGX_ERROR;
+ }
+
+ v->len = ngx_sprintf(p, "%T", ngx_time()) - p;
+ v->valid = 1;
+ v->no_cacheable = 0;
+ v->not_found = 0;
+ v->data = p;
+
+ return NGX_OK;
+}
+
+static ngx_int_t
+ngx_http_variable_unix_time2(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data)
+{
+ u_char *p;
+
+ p = ngx_pnalloc(r->pool, NGX_TIME_T_LEN);
+ if (p == NULL) {
+ return NGX_ERROR;
+ }
+
+ v->len = ngx_sprintf(p, "%T", ngx_time2()) - p;
+ v->valid = 1;
+ v->no_cacheable = 0;
+ v->not_found = 0;
+ v->data = p;
+
+ return NGX_OK;
+}
+
+
+static ngx_int_t
+ngx_http_variable_year(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data)
+{
+ return ngx_http_variables_time_fmt(r, v,
+ ngx_cached_tm->ngx_tm_year,
+ NGX_INT32_LEN);
+}
+
+
+static ngx_int_t
+ngx_http_variable_year2(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data)
+{
+ return ngx_http_variables_time_fmt(r, v,
+ ngx_cached_tm->ngx_tm_year % 100, 2);
+}
+
+
+static ngx_int_t
+ngx_http_variable_month(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data)
+{
+ return ngx_http_variables_time_fmt(r, v, ngx_cached_tm->ngx_tm_mon, 2);
+}
+
+
+static ngx_int_t
+ngx_http_variable_day(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data)
+{
+ return ngx_http_variables_time_fmt(r, v, ngx_cached_tm->ngx_tm_mday, 2);
+}
+
+
+static ngx_int_t
+ngx_http_variable_hour(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data)
+{
+ return ngx_http_variables_time_fmt(r, v, ngx_cached_tm->ngx_tm_hour, 2);
+}
+
+
+static ngx_int_t
+ngx_http_variable_hour12(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data)
+{
+ ngx_int_t hour;
+
+ hour = ngx_cached_tm->ngx_tm_hour;
+
+ if (hour == 0 || hour == 12) {
+ return ngx_http_variables_time_fmt(r, v, 12, 2);
+ } else {
+ return ngx_http_variables_time_fmt(r, v, hour % 12, 2);
+ }
+}
+
+
+static ngx_int_t
+ngx_http_variable_minute(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data)
+{
+ return ngx_http_variables_time_fmt(r, v, ngx_cached_tm->ngx_tm_min, 2);
+}
+
+
+static ngx_int_t
+ngx_http_variable_second(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data)
+{
+ return ngx_http_variables_time_fmt(r, v, ngx_cached_tm->ngx_tm_sec, 2);
+}
+
+
+static ngx_int_t
+ngx_http_variable_time_http(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data)
+{
+ u_char *p;
+
+ p = ngx_pnalloc(r->pool, ngx_cached_http_time.len);
+ if (p == NULL) {
+ return NGX_ERROR;
+ }
+
+ ngx_memcpy(p, ngx_cached_http_time.data, ngx_cached_http_time.len);
+
+ v->len = ngx_cached_http_time.len;
+ v->valid = 1;
+ v->no_cacheable = 0;
+ v->not_found = 0;
+ v->data = p;
+
+ return NGX_OK;
+}
+
+
+static ngx_int_t
+ngx_http_variables_time_fmt(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, ngx_int_t t, ngx_int_t len)
+{
+ u_char *p;
+
+ p = ngx_pnalloc(r->pool, len);
+ if (p == NULL) {
+ return NGX_ERROR;
+ }
+
+ v->len = ngx_sprintf(p, "%02i", t) - p;
+ v->valid = 1;
+ v->no_cacheable = 0;
+ v->not_found = 0;
+ v->data = p;
+
+ return NGX_OK;
+}
+
static ngx_int_t
ngx_http_variable_time_iso8601(ngx_http_request_t *r,
@@ -2405,6 +2627,27 @@
return NGX_OK;
}
+static ngx_int_t
+ngx_http_variable_time_data(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data)
+{
+ u_char *p;
+
+ p = ngx_pnalloc(r->pool, ngx_cached_http_log_time_data.len);
+ if (p == NULL) {
+ return NGX_ERROR;
+ }
+
+ ngx_memcpy(p, ngx_cached_http_log_time_data.data, ngx_cached_http_log_time_data.len);
+
+ v->len = ngx_cached_http_log_time_data.len;
+ v->valid = 1;
+ v->no_cacheable = 0;
+ v->not_found = 0;
+ v->data = p;
+
+ return NGX_OK;
+}
void *
ngx_http_map_find(ngx_http_request_t *r, ngx_http_map_t *map, ngx_str_t *match)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment