Skip to content

Commit

Permalink
feature: added new config directives set_formatted_gmt_time and set_f…
Browse files Browse the repository at this point in the history
…ormatted_local_time. thanks Trurl McByte for the patch in #11.
  • Loading branch information
agentzh committed Dec 13, 2013
1 parent 9abe49d commit b36fff6
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/ngx_http_set_local_today.c
Expand Up @@ -6,6 +6,11 @@
#include <ndk.h>


#ifndef NGX_HTTP_SET_MISC_FMT_DATE_LEN
#define NGX_HTTP_SET_MISC_FMT_DATE_LEN 256
#endif


ngx_int_t
ngx_http_set_local_today(ngx_http_request_t *r, ngx_str_t *res,
ngx_http_variable_value_t *v)
Expand Down Expand Up @@ -36,3 +41,70 @@ ngx_http_set_local_today(ngx_http_request_t *r, ngx_str_t *res,
return NGX_OK;
}


ngx_int_t
ngx_http_set_formatted_gmt_time(ngx_http_request_t *r, ngx_str_t *res,
ngx_http_variable_value_t *v)
{
time_t now;
u_char *p;
ngx_tm_t tm;

if (v->not_found || v->len == 0) {
res->data = NULL;
res->len = 0;
return NGX_OK;
}

now = ngx_time();
ngx_libc_gmtime(now, &tm);

p = ngx_palloc(r->pool, NGX_HTTP_SET_MISC_FMT_DATE_LEN);
if (p == NULL) {
return NGX_ERROR;
}

res->len = strftime((char *) p, NGX_HTTP_SET_MISC_FMT_DATE_LEN,
(char *) v->data, &tm);
if (res->len == 0) {
return NGX_ERROR;
}

res->data = p;

return NGX_OK;
}


ngx_int_t
ngx_http_set_formatted_local_time(ngx_http_request_t *r, ngx_str_t *res,
ngx_http_variable_value_t *v)
{
time_t now;
u_char *p;
ngx_tm_t tm;

if (v->not_found || v->len == 0) {
res->data = NULL;
res->len = 0;
return NGX_OK;
}

now = ngx_time();
ngx_libc_localtime(now, &tm);

p = ngx_palloc(r->pool, NGX_HTTP_SET_MISC_FMT_DATE_LEN);
if (p == NULL) {
return NGX_ERROR;
}

res->len = strftime((char *) p, NGX_HTTP_SET_MISC_FMT_DATE_LEN,
(char *) v->data, &tm);
if (res->len == 0) {
return NGX_ERROR;
}

res->data = p;

return NGX_OK;
}
6 changes: 6 additions & 0 deletions src/ngx_http_set_local_today.h
Expand Up @@ -5,3 +5,9 @@

ngx_int_t ngx_http_set_local_today(ngx_http_request_t *r, ngx_str_t *res,
ngx_http_variable_value_t *v);

ngx_int_t ngx_http_set_formatted_gmt_time(ngx_http_request_t *r, ngx_str_t *res,
ngx_http_variable_value_t *v);

ngx_int_t ngx_http_set_formatted_local_time(ngx_http_request_t *r,
ngx_str_t *res, ngx_http_variable_value_t *v);
34 changes: 34 additions & 0 deletions src/ngx_http_set_misc_module.c
Expand Up @@ -157,6 +157,22 @@ static ndk_set_var_t ngx_http_set_misc_local_today_filter = {
};


static ndk_set_var_t ngx_http_set_misc_formatted_gmt_time_filter = {
NDK_SET_VAR_VALUE,
ngx_http_set_formatted_gmt_time,
2,
NULL
};


static ndk_set_var_t ngx_http_set_misc_formatted_local_time_filter = {
NDK_SET_VAR_VALUE,
ngx_http_set_formatted_local_time,
2,
NULL
};


static ndk_set_var_t ngx_http_set_misc_set_random_filter = {
NDK_SET_VAR_MULTI_VALUE,
ngx_http_set_misc_set_random,
Expand Down Expand Up @@ -344,6 +360,24 @@ static ngx_command_t ngx_http_set_misc_commands[] = {
0,
&ngx_http_set_misc_local_today_filter
},
{
ngx_string("set_formatted_gmt_time"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE2,
ndk_set_var_value,
0,
0,
&ngx_http_set_misc_formatted_gmt_time_filter
},
{
ngx_string("set_formatted_local_time"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE2,
ndk_set_var_value,
0,
0,
&ngx_http_set_misc_formatted_local_time_filter
},
{ ngx_string ("set_random"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE3,
Expand Down
85 changes: 85 additions & 0 deletions t/formatted-time.t
@@ -0,0 +1,85 @@
# vi:filetype=

use lib 'lib';
use Test::Nginx::Socket;
use POSIX qw(strftime);

my $fmt="%a %b %e %H:%M:%S %Y";

our $str_local = (strftime $fmt, localtime time()).'|'.(strftime $fmt, localtime time()+1).'|'.(strftime $fmt, localtime time()+2);

our $str_gmt = (strftime $fmt, gmtime time()).'|'.(strftime $fmt, gmtime time()+1).'|'.(strftime $fmt, gmtime time()+2);

repeat_each(2);

plan tests => repeat_each() * 2 * blocks();

log_level('warn');

run_tests();

#no_diff();

__DATA__
=== TEST 1: local time format
--- config
location /foo {
set_formatted_local_time $today "%a %b %e %H:%M:%S %Y";
echo $today;
}
--- request
GET /foo
--- response_body_like eval: $main::str_local
=== TEST 2: GMT time format
--- config
location /bar {
set_formatted_gmt_time $today "%a %b %e %H:%M:%S %Y";
echo $today;
}
--- request
GET /bar
--- response_body_like eval: $main::str_gmt
=== TEST 3: set_formatted_gmt_time (empty formatter)
--- config
location /bar {
set_formatted_gmt_time $today "";
echo "[$today]";
}
--- request
GET /bar
--- response_body
[]
=== TEST 4: set_formatted_local_time (empty formatter)
--- config
location /bar {
set_formatted_local_time $today "";
echo "[$today]";
}
--- request
GET /bar
--- response_body
[]
=== TEST 5: set_formatted_local_time (constant formatter)
--- config
location /bar {
set_formatted_local_time $today "hello world";
echo "[$today]";
}
--- request
GET /bar
--- response_body
[hello world]

0 comments on commit b36fff6

Please sign in to comment.