Mercurial > hg > nginx-quic
annotate src/core/ngx_times.c @ 5361:7094d6da2806
Win32: $request_time fixed.
On win32, time_t is 64 bits wide by default, and passing an ngx_msec_int_t
argument for %T format specifier doesn't work. This doesn't manifest itself
on other platforms as time_t and ngx_msec_int_t are usually of the same size.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Wed, 04 Sep 2013 20:48:30 +0400 |
parents | e23892a0c917 |
children | 777202558122 |
rev | line source |
---|---|
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
433
diff
changeset
|
1 |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
433
diff
changeset
|
2 /* |
444
42d11f017717
nginx-0.1.0-2004-09-29-20:00:49 import; remove years from copyright
Igor Sysoev <igor@sysoev.ru>
parents:
441
diff
changeset
|
3 * Copyright (C) Igor Sysoev |
4412 | 4 * Copyright (C) Nginx, Inc. |
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
433
diff
changeset
|
5 */ |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
433
diff
changeset
|
6 |
178
a8ff48d26cca
nginx-0.0.1-2003-11-11-00:09:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
7 |
a8ff48d26cca
nginx-0.0.1-2003-11-11-00:09:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
8 #include <ngx_config.h> |
a8ff48d26cca
nginx-0.0.1-2003-11-11-00:09:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
9 #include <ngx_core.h> |
a8ff48d26cca
nginx-0.0.1-2003-11-11-00:09:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
10 |
a8ff48d26cca
nginx-0.0.1-2003-11-11-00:09:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
11 |
370
54f76b0b8dca
nginx-0.0.7-2004-06-27-22:01:57 import
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
12 /* |
563 | 13 * The time may be updated by signal handler or by several threads. |
14 * The time update operations are rare and require to hold the ngx_time_lock. | |
15 * The time read operations are frequent, so they are lock-free and get time | |
16 * values and strings from the current slot. Thus thread may get the corrupted | |
17 * values only if it is preempted while copying and then it is not scheduled | |
18 * to run more than NGX_TIME_SLOTS seconds. | |
370
54f76b0b8dca
nginx-0.0.7-2004-06-27-22:01:57 import
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
19 */ |
54f76b0b8dca
nginx-0.0.7-2004-06-27-22:01:57 import
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
20 |
563 | 21 #define NGX_TIME_SLOTS 64 |
370
54f76b0b8dca
nginx-0.0.7-2004-06-27-22:01:57 import
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
22 |
932
1b8111df8051
the initial slot is out of array
Igor Sysoev <igor@sysoev.ru>
parents:
891
diff
changeset
|
23 static ngx_uint_t slot; |
563 | 24 static ngx_atomic_t ngx_time_lock; |
370
54f76b0b8dca
nginx-0.0.7-2004-06-27-22:01:57 import
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
25 |
563 | 26 volatile ngx_msec_t ngx_current_msec; |
27 volatile ngx_time_t *ngx_cached_time; | |
28 volatile ngx_str_t ngx_cached_err_log_time; | |
29 volatile ngx_str_t ngx_cached_http_time; | |
30 volatile ngx_str_t ngx_cached_http_log_time; | |
3873 | 31 volatile ngx_str_t ngx_cached_http_log_iso8601; |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
32 |
3475
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
33 #if !(NGX_WIN32) |
3474
d4c4cfdffe30
*) use previously cached GMT offset value to update time from a signal handler
Igor Sysoev <igor@sysoev.ru>
parents:
2220
diff
changeset
|
34 |
d4c4cfdffe30
*) use previously cached GMT offset value to update time from a signal handler
Igor Sysoev <igor@sysoev.ru>
parents:
2220
diff
changeset
|
35 /* |
4572
67653855682e
Fixed spelling in multiline C comments.
Ruslan Ermilov <ru@nginx.com>
parents:
4499
diff
changeset
|
36 * localtime() and localtime_r() are not Async-Signal-Safe functions, therefore, |
3475
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
37 * they must not be called by a signal handler, so we use the cached |
3474
d4c4cfdffe30
*) use previously cached GMT offset value to update time from a signal handler
Igor Sysoev <igor@sysoev.ru>
parents:
2220
diff
changeset
|
38 * GMT offset value. Fortunately the value is changed only two times a year. |
d4c4cfdffe30
*) use previously cached GMT offset value to update time from a signal handler
Igor Sysoev <igor@sysoev.ru>
parents:
2220
diff
changeset
|
39 */ |
d4c4cfdffe30
*) use previously cached GMT offset value to update time from a signal handler
Igor Sysoev <igor@sysoev.ru>
parents:
2220
diff
changeset
|
40 |
d4c4cfdffe30
*) use previously cached GMT offset value to update time from a signal handler
Igor Sysoev <igor@sysoev.ru>
parents:
2220
diff
changeset
|
41 static ngx_int_t cached_gmtoff; |
d4c4cfdffe30
*) use previously cached GMT offset value to update time from a signal handler
Igor Sysoev <igor@sysoev.ru>
parents:
2220
diff
changeset
|
42 #endif |
d4c4cfdffe30
*) use previously cached GMT offset value to update time from a signal handler
Igor Sysoev <igor@sysoev.ru>
parents:
2220
diff
changeset
|
43 |
563 | 44 static ngx_time_t cached_time[NGX_TIME_SLOTS]; |
45 static u_char cached_err_log_time[NGX_TIME_SLOTS] | |
46 [sizeof("1970/09/28 12:00:00")]; | |
47 static u_char cached_http_time[NGX_TIME_SLOTS] | |
48 [sizeof("Mon, 28 Sep 1970 06:00:00 GMT")]; | |
49 static u_char cached_http_log_time[NGX_TIME_SLOTS] | |
50 [sizeof("28/Sep/1970:12:00:00 +0600")]; | |
3873 | 51 static u_char cached_http_log_iso8601[NGX_TIME_SLOTS] |
52 [sizeof("1970-09-28T12:00:00+06:00")]; | |
178
a8ff48d26cca
nginx-0.0.1-2003-11-11-00:09:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
53 |
a8ff48d26cca
nginx-0.0.1-2003-11-11-00:09:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
54 |
326
8733703a37f3
nginx-0.0.3-2004-04-26-00:13:21 import
Igor Sysoev <igor@sysoev.ru>
parents:
324
diff
changeset
|
55 static char *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
56 static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
57 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
58 |
501 | 59 void |
60 ngx_time_init(void) | |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
61 { |
370
54f76b0b8dca
nginx-0.0.7-2004-06-27-22:01:57 import
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
62 ngx_cached_err_log_time.len = sizeof("1970/09/28 12:00:00") - 1; |
54f76b0b8dca
nginx-0.0.7-2004-06-27-22:01:57 import
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
63 ngx_cached_http_time.len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1; |
54f76b0b8dca
nginx-0.0.7-2004-06-27-22:01:57 import
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
64 ngx_cached_http_log_time.len = sizeof("28/Sep/1970:12:00:00 +0600") - 1; |
3873 | 65 ngx_cached_http_log_iso8601.len = sizeof("1970-09-28T12:00:00+06:00") - 1; |
323
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
66 |
370
54f76b0b8dca
nginx-0.0.7-2004-06-27-22:01:57 import
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
67 ngx_cached_time = &cached_time[0]; |
205
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
68 |
3475
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
69 ngx_time_update(); |
270
7bb9562216ce
nginx-0.0.2-2004-02-25-23:16:15 import
Igor Sysoev <igor@sysoev.ru>
parents:
266
diff
changeset
|
70 } |
266
5238e93961a1
nginx-0.0.2-2004-02-23-23:57:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
245
diff
changeset
|
71 |
270
7bb9562216ce
nginx-0.0.2-2004-02-25-23:16:15 import
Igor Sysoev <igor@sysoev.ru>
parents:
266
diff
changeset
|
72 |
563 | 73 void |
3475
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
74 ngx_time_update(void) |
270
7bb9562216ce
nginx-0.0.2-2004-02-25-23:16:15 import
Igor Sysoev <igor@sysoev.ru>
parents:
266
diff
changeset
|
75 { |
3873 | 76 u_char *p0, *p1, *p2, *p3; |
563 | 77 ngx_tm_t tm, gmt; |
3474
d4c4cfdffe30
*) use previously cached GMT offset value to update time from a signal handler
Igor Sysoev <igor@sysoev.ru>
parents:
2220
diff
changeset
|
78 time_t sec; |
d4c4cfdffe30
*) use previously cached GMT offset value to update time from a signal handler
Igor Sysoev <igor@sysoev.ru>
parents:
2220
diff
changeset
|
79 ngx_uint_t msec; |
563 | 80 ngx_time_t *tp; |
81 struct timeval tv; | |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
82 |
563 | 83 if (!ngx_trylock(&ngx_time_lock)) { |
271
e16dfb9b9afa
nginx-0.0.2-2004-02-26-20:10:01 import
Igor Sysoev <igor@sysoev.ru>
parents:
270
diff
changeset
|
84 return; |
266
5238e93961a1
nginx-0.0.2-2004-02-23-23:57:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
245
diff
changeset
|
85 } |
370
54f76b0b8dca
nginx-0.0.7-2004-06-27-22:01:57 import
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
86 |
3474
d4c4cfdffe30
*) use previously cached GMT offset value to update time from a signal handler
Igor Sysoev <igor@sysoev.ru>
parents:
2220
diff
changeset
|
87 ngx_gettimeofday(&tv); |
563 | 88 |
3474
d4c4cfdffe30
*) use previously cached GMT offset value to update time from a signal handler
Igor Sysoev <igor@sysoev.ru>
parents:
2220
diff
changeset
|
89 sec = tv.tv_sec; |
d4c4cfdffe30
*) use previously cached GMT offset value to update time from a signal handler
Igor Sysoev <igor@sysoev.ru>
parents:
2220
diff
changeset
|
90 msec = tv.tv_usec / 1000; |
563 | 91 |
92 ngx_current_msec = (ngx_msec_t) sec * 1000 + msec; | |
93 | |
94 tp = &cached_time[slot]; | |
266
5238e93961a1
nginx-0.0.2-2004-02-23-23:57:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
245
diff
changeset
|
95 |
563 | 96 if (tp->sec == sec) { |
891
8a3ba663be1a
slot number must be incremented once per second only,
Igor Sysoev <igor@sysoev.ru>
parents:
563
diff
changeset
|
97 tp->msec = msec; |
563 | 98 ngx_unlock(&ngx_time_lock); |
99 return; | |
100 } | |
370
54f76b0b8dca
nginx-0.0.7-2004-06-27-22:01:57 import
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
101 |
1086
a62d30719ea1
fix segfault if nginx was build with -O2 -fomit-frame-pointer,
Igor Sysoev <igor@sysoev.ru>
parents:
1081
diff
changeset
|
102 if (slot == NGX_TIME_SLOTS - 1) { |
891
8a3ba663be1a
slot number must be incremented once per second only,
Igor Sysoev <igor@sysoev.ru>
parents:
563
diff
changeset
|
103 slot = 0; |
8a3ba663be1a
slot number must be incremented once per second only,
Igor Sysoev <igor@sysoev.ru>
parents:
563
diff
changeset
|
104 } else { |
8a3ba663be1a
slot number must be incremented once per second only,
Igor Sysoev <igor@sysoev.ru>
parents:
563
diff
changeset
|
105 slot++; |
8a3ba663be1a
slot number must be incremented once per second only,
Igor Sysoev <igor@sysoev.ru>
parents:
563
diff
changeset
|
106 } |
8a3ba663be1a
slot number must be incremented once per second only,
Igor Sysoev <igor@sysoev.ru>
parents:
563
diff
changeset
|
107 |
8a3ba663be1a
slot number must be incremented once per second only,
Igor Sysoev <igor@sysoev.ru>
parents:
563
diff
changeset
|
108 tp = &cached_time[slot]; |
8a3ba663be1a
slot number must be incremented once per second only,
Igor Sysoev <igor@sysoev.ru>
parents:
563
diff
changeset
|
109 |
563 | 110 tp->sec = sec; |
891
8a3ba663be1a
slot number must be incremented once per second only,
Igor Sysoev <igor@sysoev.ru>
parents:
563
diff
changeset
|
111 tp->msec = msec; |
563 | 112 |
113 ngx_gmtime(sec, &gmt); | |
181
811bdd2af794
nginx-0.0.1-2003-11-12-20:25:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
180
diff
changeset
|
114 |
323
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
115 |
1081
4cf9d9b644f7
fix segfault if nginx was build with -O2 -fomit-frame-pointer
Igor Sysoev <igor@sysoev.ru>
parents:
932
diff
changeset
|
116 p0 = &cached_http_time[slot][0]; |
323
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
117 |
563 | 118 (void) ngx_sprintf(p0, "%s, %02d %s %4d %02d:%02d:%02d GMT", |
119 week[gmt.ngx_tm_wday], gmt.ngx_tm_mday, | |
120 months[gmt.ngx_tm_mon - 1], gmt.ngx_tm_year, | |
121 gmt.ngx_tm_hour, gmt.ngx_tm_min, gmt.ngx_tm_sec); | |
323
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
122 |
469 | 123 #if (NGX_HAVE_GETTIMEZONE) |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
326
diff
changeset
|
124 |
563 | 125 tp->gmtoff = ngx_gettimezone(); |
126 ngx_gmtime(sec + tp->gmtoff * 60, &tm); | |
323
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
127 |
3475
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
128 #elif (NGX_HAVE_GMTOFF) |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
129 |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
130 ngx_localtime(sec, &tm); |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
131 cached_gmtoff = (ngx_int_t) (tm.ngx_tm_gmtoff / 60); |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
132 tp->gmtoff = cached_gmtoff; |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
133 |
323
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
134 #else |
178
a8ff48d26cca
nginx-0.0.1-2003-11-11-00:09:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
135 |
3475
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
136 ngx_localtime(sec, &tm); |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
137 cached_gmtoff = ngx_timezone(tm.ngx_tm_isdst); |
3474
d4c4cfdffe30
*) use previously cached GMT offset value to update time from a signal handler
Igor Sysoev <igor@sysoev.ru>
parents:
2220
diff
changeset
|
138 tp->gmtoff = cached_gmtoff; |
178
a8ff48d26cca
nginx-0.0.1-2003-11-11-00:09:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
139 |
323
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
140 #endif |
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
141 |
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
142 |
1081
4cf9d9b644f7
fix segfault if nginx was build with -O2 -fomit-frame-pointer
Igor Sysoev <igor@sysoev.ru>
parents:
932
diff
changeset
|
143 p1 = &cached_err_log_time[slot][0]; |
323
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
144 |
563 | 145 (void) ngx_sprintf(p1, "%4d/%02d/%02d %02d:%02d:%02d", |
517 | 146 tm.ngx_tm_year, tm.ngx_tm_mon, |
147 tm.ngx_tm_mday, tm.ngx_tm_hour, | |
148 tm.ngx_tm_min, tm.ngx_tm_sec); | |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
149 |
323
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
150 |
1081
4cf9d9b644f7
fix segfault if nginx was build with -O2 -fomit-frame-pointer
Igor Sysoev <igor@sysoev.ru>
parents:
932
diff
changeset
|
151 p2 = &cached_http_log_time[slot][0]; |
323
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
152 |
563 | 153 (void) ngx_sprintf(p2, "%02d/%s/%d:%02d:%02d:%02d %c%02d%02d", |
517 | 154 tm.ngx_tm_mday, months[tm.ngx_tm_mon - 1], |
155 tm.ngx_tm_year, tm.ngx_tm_hour, | |
156 tm.ngx_tm_min, tm.ngx_tm_sec, | |
563 | 157 tp->gmtoff < 0 ? '-' : '+', |
158 ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60)); | |
323
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
159 |
3873 | 160 p3 = &cached_http_log_iso8601[slot][0]; |
161 | |
162 (void) ngx_sprintf(p3, "%4d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d", | |
163 tm.ngx_tm_year, tm.ngx_tm_mon, | |
164 tm.ngx_tm_mday, tm.ngx_tm_hour, | |
165 tm.ngx_tm_min, tm.ngx_tm_sec, | |
166 tp->gmtoff < 0 ? '-' : '+', | |
167 ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60)); | |
168 | |
266
5238e93961a1
nginx-0.0.2-2004-02-23-23:57:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
245
diff
changeset
|
169 |
563 | 170 ngx_memory_barrier(); |
266
5238e93961a1
nginx-0.0.2-2004-02-23-23:57:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
245
diff
changeset
|
171 |
563 | 172 ngx_cached_time = tp; |
173 ngx_cached_http_time.data = p0; | |
174 ngx_cached_err_log_time.data = p1; | |
175 ngx_cached_http_log_time.data = p2; | |
3873 | 176 ngx_cached_http_log_iso8601.data = p3; |
563 | 177 |
178 ngx_unlock(&ngx_time_lock); | |
178
a8ff48d26cca
nginx-0.0.1-2003-11-11-00:09:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
179 } |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
180 |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
181 |
3475
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
182 #if !(NGX_WIN32) |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
183 |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
184 void |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
185 ngx_time_sigsafe_update(void) |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
186 { |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
187 u_char *p; |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
188 ngx_tm_t tm; |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
189 time_t sec; |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
190 ngx_time_t *tp; |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
191 struct timeval tv; |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
192 |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
193 if (!ngx_trylock(&ngx_time_lock)) { |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
194 return; |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
195 } |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
196 |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
197 ngx_gettimeofday(&tv); |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
198 |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
199 sec = tv.tv_sec; |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
200 |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
201 tp = &cached_time[slot]; |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
202 |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
203 if (tp->sec == sec) { |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
204 ngx_unlock(&ngx_time_lock); |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
205 return; |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
206 } |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
207 |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
208 if (slot == NGX_TIME_SLOTS - 1) { |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
209 slot = 0; |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
210 } else { |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
211 slot++; |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
212 } |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
213 |
4782
e23892a0c917
Fixed possible use of old cached times if runtime went backwards.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4572
diff
changeset
|
214 tp = &cached_time[slot]; |
e23892a0c917
Fixed possible use of old cached times if runtime went backwards.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4572
diff
changeset
|
215 |
e23892a0c917
Fixed possible use of old cached times if runtime went backwards.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4572
diff
changeset
|
216 tp->sec = 0; |
e23892a0c917
Fixed possible use of old cached times if runtime went backwards.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4572
diff
changeset
|
217 |
3475
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
218 ngx_gmtime(sec + cached_gmtoff * 60, &tm); |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
219 |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
220 p = &cached_err_log_time[slot][0]; |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
221 |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
222 (void) ngx_sprintf(p, "%4d/%02d/%02d %02d:%02d:%02d", |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
223 tm.ngx_tm_year, tm.ngx_tm_mon, |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
224 tm.ngx_tm_mday, tm.ngx_tm_hour, |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
225 tm.ngx_tm_min, tm.ngx_tm_sec); |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
226 |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
227 ngx_memory_barrier(); |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
228 |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
229 ngx_cached_err_log_time.data = p; |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
230 |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
231 ngx_unlock(&ngx_time_lock); |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
232 } |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
233 |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
234 #endif |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
235 |
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
236 |
501 | 237 u_char * |
238 ngx_http_time(u_char *buf, time_t t) | |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
239 { |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
240 ngx_tm_t tm; |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
241 |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
242 ngx_gmtime(t, &tm); |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
243 |
455 | 244 return ngx_sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d GMT", |
245 week[tm.ngx_tm_wday], | |
246 tm.ngx_tm_mday, | |
247 months[tm.ngx_tm_mon - 1], | |
248 tm.ngx_tm_year, | |
249 tm.ngx_tm_hour, | |
250 tm.ngx_tm_min, | |
251 tm.ngx_tm_sec); | |
409
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
252 } |
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
253 |
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
254 |
501 | 255 u_char * |
256 ngx_http_cookie_time(u_char *buf, time_t t) | |
409
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
257 { |
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
258 ngx_tm_t tm; |
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
259 |
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
260 ngx_gmtime(t, &tm); |
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
261 |
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
262 /* |
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
263 * Netscape 3.x does not understand 4-digit years at all and |
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
264 * 2-digit years more than "37" |
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
265 */ |
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
266 |
455 | 267 return ngx_sprintf(buf, |
268 (tm.ngx_tm_year > 2037) ? | |
269 "%s, %02d-%s-%d %02d:%02d:%02d GMT": | |
270 "%s, %02d-%s-%02d %02d:%02d:%02d GMT", | |
271 week[tm.ngx_tm_wday], | |
272 tm.ngx_tm_mday, | |
273 months[tm.ngx_tm_mon - 1], | |
274 (tm.ngx_tm_year > 2037) ? tm.ngx_tm_year: | |
275 tm.ngx_tm_year % 100, | |
276 tm.ngx_tm_hour, | |
277 tm.ngx_tm_min, | |
278 tm.ngx_tm_sec); | |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
279 } |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
280 |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
281 |
501 | 282 void |
283 ngx_gmtime(time_t t, ngx_tm_t *tp) | |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
284 { |
1970
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
285 ngx_int_t yday; |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
286 ngx_uint_t n, sec, min, hour, mday, mon, year, wday, days, leap; |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
287 |
1874
085e536d856d
treat time_t as unsigned time
Igor Sysoev <igor@sysoev.ru>
parents:
1640
diff
changeset
|
288 /* the calculation is valid for positive time_t only */ |
1970
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
289 |
1874
085e536d856d
treat time_t as unsigned time
Igor Sysoev <igor@sysoev.ru>
parents:
1640
diff
changeset
|
290 n = (ngx_uint_t) t; |
085e536d856d
treat time_t as unsigned time
Igor Sysoev <igor@sysoev.ru>
parents:
1640
diff
changeset
|
291 |
085e536d856d
treat time_t as unsigned time
Igor Sysoev <igor@sysoev.ru>
parents:
1640
diff
changeset
|
292 days = n / 86400; |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
293 |
4499
778ef9c3fd2d
Fixed spelling in single-line comments.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
294 /* January 1, 1970 was Thursday */ |
1970
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
295 |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
296 wday = (4 + days) % 7; |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
297 |
1874
085e536d856d
treat time_t as unsigned time
Igor Sysoev <igor@sysoev.ru>
parents:
1640
diff
changeset
|
298 n %= 86400; |
085e536d856d
treat time_t as unsigned time
Igor Sysoev <igor@sysoev.ru>
parents:
1640
diff
changeset
|
299 hour = n / 3600; |
085e536d856d
treat time_t as unsigned time
Igor Sysoev <igor@sysoev.ru>
parents:
1640
diff
changeset
|
300 n %= 3600; |
085e536d856d
treat time_t as unsigned time
Igor Sysoev <igor@sysoev.ru>
parents:
1640
diff
changeset
|
301 min = n / 60; |
085e536d856d
treat time_t as unsigned time
Igor Sysoev <igor@sysoev.ru>
parents:
1640
diff
changeset
|
302 sec = n % 60; |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
303 |
1970
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
304 /* |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
305 * the algorithm based on Gauss' formula, |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
306 * see src/http/ngx_http_parse_time.c |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
307 */ |
208
0b67be7d4489
nginx-0.0.1-2003-12-08-23:48:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
207
diff
changeset
|
308 |
1970
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
309 /* days since March 1, 1 BC */ |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
310 days = days - (31 + 28) + 719527; |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
311 |
1970
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
312 /* |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
313 * The "days" should be adjusted to 1 only, however, some March 1st's go |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
314 * to previous year, so we adjust them to 2. This causes also shift of the |
4572
67653855682e
Fixed spelling in multiline C comments.
Ruslan Ermilov <ru@nginx.com>
parents:
4499
diff
changeset
|
315 * last February days to next year, but we catch the case when "yday" |
1970
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
316 * becomes negative. |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
317 */ |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
318 |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
319 year = (days + 2) * 400 / (365 * 400 + 100 - 4 + 1); |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
320 |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
321 yday = days - (365 * year + year / 4 - year / 100 + year / 400); |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
322 |
1970
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
323 if (yday < 0) { |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
324 leap = (year % 4 == 0) && (year % 100 || (year % 400 == 0)); |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
325 yday = 365 + leap + yday; |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
326 year--; |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
327 } |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
328 |
1970
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
329 /* |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
330 * The empirical formula that maps "yday" to month. |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
331 * There are at least 10 variants, some of them are: |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
332 * mon = (yday + 31) * 15 / 459 |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
333 * mon = (yday + 31) * 17 / 520 |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
334 * mon = (yday + 31) * 20 / 612 |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
335 */ |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
336 |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
337 mon = (yday + 31) * 10 / 306; |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
338 |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
339 /* the Gauss' formula that evaluates days before the month */ |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
340 |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
341 mday = yday - (367 * mon / 12 - 30) + 1; |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
342 |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
343 if (yday >= 306) { |
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
433
diff
changeset
|
344 |
1970
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
345 year++; |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
346 mon -= 10; |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
347 |
297
ee394e997c77
nginx-0.0.3-2004-03-29-21:43:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
348 /* |
324
0ab66f4b6c4d
nginx-0.0.3-2004-04-22-00:13:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
323
diff
changeset
|
349 * there is no "yday" in Win32 SYSTEMTIME |
297
ee394e997c77
nginx-0.0.3-2004-03-29-21:43:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
350 * |
ee394e997c77
nginx-0.0.3-2004-03-29-21:43:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
351 * yday -= 306; |
ee394e997c77
nginx-0.0.3-2004-03-29-21:43:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
352 */ |
ee394e997c77
nginx-0.0.3-2004-03-29-21:43:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
353 |
1970
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
354 } else { |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
355 |
1970
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
356 mon += 2; |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
357 |
1970
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
358 /* |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
359 * there is no "yday" in Win32 SYSTEMTIME |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
360 * |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
361 * yday += 31 + 28 + leap; |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
362 */ |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
363 } |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
364 |
290
87e73f067470
nginx-0.0.2-2004-03-16-10:10:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
271
diff
changeset
|
365 tp->ngx_tm_sec = (ngx_tm_sec_t) sec; |
87e73f067470
nginx-0.0.2-2004-03-16-10:10:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
271
diff
changeset
|
366 tp->ngx_tm_min = (ngx_tm_min_t) min; |
87e73f067470
nginx-0.0.2-2004-03-16-10:10:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
271
diff
changeset
|
367 tp->ngx_tm_hour = (ngx_tm_hour_t) hour; |
87e73f067470
nginx-0.0.2-2004-03-16-10:10:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
271
diff
changeset
|
368 tp->ngx_tm_mday = (ngx_tm_mday_t) mday; |
87e73f067470
nginx-0.0.2-2004-03-16-10:10:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
271
diff
changeset
|
369 tp->ngx_tm_mon = (ngx_tm_mon_t) mon; |
87e73f067470
nginx-0.0.2-2004-03-16-10:10:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
271
diff
changeset
|
370 tp->ngx_tm_year = (ngx_tm_year_t) year; |
87e73f067470
nginx-0.0.2-2004-03-16-10:10:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
271
diff
changeset
|
371 tp->ngx_tm_wday = (ngx_tm_wday_t) wday; |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
372 } |
2162 | 373 |
374 | |
375 time_t | |
376 ngx_next_time(time_t when) | |
377 { | |
378 time_t now, next; | |
379 struct tm tm; | |
380 | |
381 now = ngx_time(); | |
382 | |
383 ngx_libc_localtime(now, &tm); | |
384 | |
385 tm.tm_hour = (int) (when / 3600); | |
386 when %= 3600; | |
387 tm.tm_min = (int) (when / 60); | |
388 tm.tm_sec = (int) (when % 60); | |
389 | |
390 next = mktime(&tm); | |
391 | |
392 if (next == -1) { | |
393 return -1; | |
394 } | |
395 | |
396 if (next - now > 0) { | |
397 return next; | |
398 } | |
399 | |
400 tm.tm_mday++; | |
401 | |
402 /* mktime() should normalize a date (Jan 32, etc) */ | |
403 | |
404 next = mktime(&tm); | |
405 | |
406 if (next != -1) { | |
407 return next; | |
408 } | |
409 | |
410 return -1; | |
411 } |