Igor Sysoev | d59a047 | 2003-11-10 21:09:22 +0000 | [diff] [blame] | 1 | |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 2 | /* |
Igor Sysoev | ff8da91 | 2004-09-29 16:00:49 +0000 | [diff] [blame] | 3 | * Copyright (C) Igor Sysoev |
Maxim Konovalov | f8d59e3 | 2012-01-18 15:07:43 +0000 | [diff] [blame] | 4 | * Copyright (C) Nginx, Inc. |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | |
Igor Sysoev | d59a047 | 2003-11-10 21:09:22 +0000 | [diff] [blame] | 8 | #include <ngx_config.h> |
| 9 | #include <ngx_core.h> |
| 10 | |
| 11 | |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 12 | /* |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 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. |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 21 | #define NGX_TIME_SLOTS 64 |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 22 | |
Igor Sysoev | f4b34c4 | 2006-12-19 12:38:20 +0000 | [diff] [blame] | 23 | static ngx_uint_t slot; |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 24 | static ngx_atomic_t ngx_time_lock; |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 25 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 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; |
Igor Sysoev | c9d671c | 2011-03-16 15:46:57 +0000 | [diff] [blame] | 31 | volatile ngx_str_t ngx_cached_http_log_iso8601; |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 32 | |
Igor Sysoev | 6d45d8a | 2010-03-25 09:10:10 +0000 | [diff] [blame] | 33 | #if !(NGX_WIN32) |
Igor Sysoev | 2f916a9 | 2010-03-13 18:08:07 +0000 | [diff] [blame] | 34 | |
| 35 | /* |
Ruslan Ermilov | 47a04aa | 2012-04-03 07:37:31 +0000 | [diff] [blame] | 36 | * localtime() and localtime_r() are not Async-Signal-Safe functions, therefore, |
Igor Sysoev | 6d45d8a | 2010-03-25 09:10:10 +0000 | [diff] [blame] | 37 | * they must not be called by a signal handler, so we use the cached |
Igor Sysoev | 2f916a9 | 2010-03-13 18:08:07 +0000 | [diff] [blame] | 38 | * GMT offset value. Fortunately the value is changed only two times a year. |
| 39 | */ |
| 40 | |
| 41 | static ngx_int_t cached_gmtoff; |
| 42 | #endif |
| 43 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 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")]; |
Igor Sysoev | c9d671c | 2011-03-16 15:46:57 +0000 | [diff] [blame] | 51 | static u_char cached_http_log_iso8601[NGX_TIME_SLOTS] |
| 52 | [sizeof("1970-09-28T12:00:00+06:00")]; |
Igor Sysoev | d59a047 | 2003-11-10 21:09:22 +0000 | [diff] [blame] | 53 | |
| 54 | |
Igor Sysoev | 3f4685f | 2004-04-25 20:13:21 +0000 | [diff] [blame] | 55 | static char *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 56 | static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 57 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 58 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 59 | void |
| 60 | ngx_time_init(void) |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 61 | { |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 62 | ngx_cached_err_log_time.len = sizeof("1970/09/28 12:00:00") - 1; |
| 63 | ngx_cached_http_time.len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1; |
| 64 | ngx_cached_http_log_time.len = sizeof("28/Sep/1970:12:00:00 +0600") - 1; |
Igor Sysoev | c9d671c | 2011-03-16 15:46:57 +0000 | [diff] [blame] | 65 | ngx_cached_http_log_iso8601.len = sizeof("1970-09-28T12:00:00+06:00") - 1; |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 66 | |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 67 | ngx_cached_time = &cached_time[0]; |
Igor Sysoev | f5003d8 | 2003-12-04 14:53:00 +0000 | [diff] [blame] | 68 | |
Igor Sysoev | 6d45d8a | 2010-03-25 09:10:10 +0000 | [diff] [blame] | 69 | ngx_time_update(); |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 73 | void |
Igor Sysoev | 6d45d8a | 2010-03-25 09:10:10 +0000 | [diff] [blame] | 74 | ngx_time_update(void) |
Igor Sysoev | d59a047 | 2003-11-10 21:09:22 +0000 | [diff] [blame] | 75 | { |
Igor Sysoev | c9d671c | 2011-03-16 15:46:57 +0000 | [diff] [blame] | 76 | u_char *p0, *p1, *p2, *p3; |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 77 | ngx_tm_t tm, gmt; |
Igor Sysoev | 2f916a9 | 2010-03-13 18:08:07 +0000 | [diff] [blame] | 78 | time_t sec; |
| 79 | ngx_uint_t msec; |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 80 | ngx_time_t *tp; |
| 81 | struct timeval tv; |
Igor Sysoev | d59a047 | 2003-11-10 21:09:22 +0000 | [diff] [blame] | 82 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 83 | if (!ngx_trylock(&ngx_time_lock)) { |
Igor Sysoev | 898446c | 2004-02-26 17:10:01 +0000 | [diff] [blame] | 84 | return; |
Igor Sysoev | b54698b | 2004-02-23 20:57:12 +0000 | [diff] [blame] | 85 | } |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 86 | |
Igor Sysoev | 2f916a9 | 2010-03-13 18:08:07 +0000 | [diff] [blame] | 87 | ngx_gettimeofday(&tv); |
Igor Sysoev | b54698b | 2004-02-23 20:57:12 +0000 | [diff] [blame] | 88 | |
Igor Sysoev | 2f916a9 | 2010-03-13 18:08:07 +0000 | [diff] [blame] | 89 | sec = tv.tv_sec; |
| 90 | msec = tv.tv_usec / 1000; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 91 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 92 | ngx_current_msec = (ngx_msec_t) sec * 1000 + msec; |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 93 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 94 | tp = &cached_time[slot]; |
| 95 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 96 | if (tp->sec == sec) { |
Igor Sysoev | c3d106a | 2006-12-06 14:25:20 +0000 | [diff] [blame] | 97 | tp->msec = msec; |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 98 | ngx_unlock(&ngx_time_lock); |
| 99 | return; |
| 100 | } |
| 101 | |
Igor Sysoev | f51d4fe | 2007-01-29 11:52:25 +0000 | [diff] [blame] | 102 | if (slot == NGX_TIME_SLOTS - 1) { |
Igor Sysoev | c3d106a | 2006-12-06 14:25:20 +0000 | [diff] [blame] | 103 | slot = 0; |
| 104 | } else { |
| 105 | slot++; |
| 106 | } |
| 107 | |
| 108 | tp = &cached_time[slot]; |
| 109 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 110 | tp->sec = sec; |
Igor Sysoev | c3d106a | 2006-12-06 14:25:20 +0000 | [diff] [blame] | 111 | tp->msec = msec; |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 112 | |
| 113 | ngx_gmtime(sec, &gmt); |
Igor Sysoev | d59a047 | 2003-11-10 21:09:22 +0000 | [diff] [blame] | 114 | |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 115 | |
Igor Sysoev | 80a2901 | 2007-01-25 22:01:23 +0000 | [diff] [blame] | 116 | p0 = &cached_http_time[slot][0]; |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 117 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 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); |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 122 | |
Igor Sysoev | f690604 | 2004-11-25 16:17:31 +0000 | [diff] [blame] | 123 | #if (NGX_HAVE_GETTIMEZONE) |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 124 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 125 | tp->gmtoff = ngx_gettimezone(); |
| 126 | ngx_gmtime(sec + tp->gmtoff * 60, &tm); |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 127 | |
Igor Sysoev | 6d45d8a | 2010-03-25 09:10:10 +0000 | [diff] [blame] | 128 | #elif (NGX_HAVE_GMTOFF) |
| 129 | |
| 130 | ngx_localtime(sec, &tm); |
| 131 | cached_gmtoff = (ngx_int_t) (tm.ngx_tm_gmtoff / 60); |
| 132 | tp->gmtoff = cached_gmtoff; |
| 133 | |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 134 | #else |
Igor Sysoev | d59a047 | 2003-11-10 21:09:22 +0000 | [diff] [blame] | 135 | |
Igor Sysoev | 6d45d8a | 2010-03-25 09:10:10 +0000 | [diff] [blame] | 136 | ngx_localtime(sec, &tm); |
| 137 | cached_gmtoff = ngx_timezone(tm.ngx_tm_isdst); |
Igor Sysoev | 2f916a9 | 2010-03-13 18:08:07 +0000 | [diff] [blame] | 138 | tp->gmtoff = cached_gmtoff; |
Igor Sysoev | d59a047 | 2003-11-10 21:09:22 +0000 | [diff] [blame] | 139 | |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 140 | #endif |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 141 | |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 142 | |
Igor Sysoev | 80a2901 | 2007-01-25 22:01:23 +0000 | [diff] [blame] | 143 | p1 = &cached_err_log_time[slot][0]; |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 144 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 145 | (void) ngx_sprintf(p1, "%4d/%02d/%02d %02d:%02d:%02d", |
Igor Sysoev | 4959ec4 | 2005-05-23 12:07:45 +0000 | [diff] [blame] | 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); |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 149 | |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 150 | |
Igor Sysoev | 80a2901 | 2007-01-25 22:01:23 +0000 | [diff] [blame] | 151 | p2 = &cached_http_log_time[slot][0]; |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 152 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 153 | (void) ngx_sprintf(p2, "%02d/%s/%d:%02d:%02d:%02d %c%02d%02d", |
Igor Sysoev | 4959ec4 | 2005-05-23 12:07:45 +0000 | [diff] [blame] | 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, |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 157 | tp->gmtoff < 0 ? '-' : '+', |
| 158 | ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60)); |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 159 | |
Igor Sysoev | c9d671c | 2011-03-16 15:46:57 +0000 | [diff] [blame] | 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 | |
Igor Sysoev | b54698b | 2004-02-23 20:57:12 +0000 | [diff] [blame] | 169 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 170 | ngx_memory_barrier(); |
Igor Sysoev | b54698b | 2004-02-23 20:57:12 +0000 | [diff] [blame] | 171 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 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; |
Igor Sysoev | c9d671c | 2011-03-16 15:46:57 +0000 | [diff] [blame] | 176 | ngx_cached_http_log_iso8601.data = p3; |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 177 | |
| 178 | ngx_unlock(&ngx_time_lock); |
Igor Sysoev | d59a047 | 2003-11-10 21:09:22 +0000 | [diff] [blame] | 179 | } |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 180 | |
| 181 | |
Igor Sysoev | 6d45d8a | 2010-03-25 09:10:10 +0000 | [diff] [blame] | 182 | #if !(NGX_WIN32) |
| 183 | |
| 184 | void |
| 185 | ngx_time_sigsafe_update(void) |
| 186 | { |
| 187 | u_char *p; |
| 188 | ngx_tm_t tm; |
| 189 | time_t sec; |
Igor Sysoev | 6d45d8a | 2010-03-25 09:10:10 +0000 | [diff] [blame] | 190 | ngx_time_t *tp; |
| 191 | struct timeval tv; |
| 192 | |
| 193 | if (!ngx_trylock(&ngx_time_lock)) { |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | ngx_gettimeofday(&tv); |
| 198 | |
| 199 | sec = tv.tv_sec; |
Igor Sysoev | 6d45d8a | 2010-03-25 09:10:10 +0000 | [diff] [blame] | 200 | |
| 201 | tp = &cached_time[slot]; |
| 202 | |
| 203 | if (tp->sec == sec) { |
| 204 | ngx_unlock(&ngx_time_lock); |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | if (slot == NGX_TIME_SLOTS - 1) { |
| 209 | slot = 0; |
| 210 | } else { |
| 211 | slot++; |
| 212 | } |
| 213 | |
Maxim Dounin | 25197b3 | 2012-08-03 09:10:39 +0000 | [diff] [blame^] | 214 | tp = &cached_time[slot]; |
| 215 | |
| 216 | tp->sec = 0; |
| 217 | |
Igor Sysoev | 6d45d8a | 2010-03-25 09:10:10 +0000 | [diff] [blame] | 218 | ngx_gmtime(sec + cached_gmtoff * 60, &tm); |
| 219 | |
| 220 | p = &cached_err_log_time[slot][0]; |
| 221 | |
| 222 | (void) ngx_sprintf(p, "%4d/%02d/%02d %02d:%02d:%02d", |
| 223 | tm.ngx_tm_year, tm.ngx_tm_mon, |
| 224 | tm.ngx_tm_mday, tm.ngx_tm_hour, |
| 225 | tm.ngx_tm_min, tm.ngx_tm_sec); |
| 226 | |
| 227 | ngx_memory_barrier(); |
| 228 | |
| 229 | ngx_cached_err_log_time.data = p; |
| 230 | |
| 231 | ngx_unlock(&ngx_time_lock); |
| 232 | } |
| 233 | |
| 234 | #endif |
| 235 | |
| 236 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 237 | u_char * |
| 238 | ngx_http_time(u_char *buf, time_t t) |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 239 | { |
| 240 | ngx_tm_t tm; |
| 241 | |
| 242 | ngx_gmtime(t, &tm); |
| 243 | |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 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); |
Igor Sysoev | a7c4a2a | 2004-08-29 03:55:41 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 255 | u_char * |
| 256 | ngx_http_cookie_time(u_char *buf, time_t t) |
Igor Sysoev | a7c4a2a | 2004-08-29 03:55:41 +0000 | [diff] [blame] | 257 | { |
| 258 | ngx_tm_t tm; |
| 259 | |
| 260 | ngx_gmtime(t, &tm); |
| 261 | |
| 262 | /* |
| 263 | * Netscape 3.x does not understand 4-digit years at all and |
| 264 | * 2-digit years more than "37" |
| 265 | */ |
| 266 | |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 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); |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 282 | void |
| 283 | ngx_gmtime(time_t t, ngx_tm_t *tp) |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 284 | { |
Igor Sysoev | 4611ad3 | 2008-04-13 13:33:12 +0000 | [diff] [blame] | 285 | ngx_int_t yday; |
| 286 | ngx_uint_t n, sec, min, hour, mday, mon, year, wday, days, leap; |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 287 | |
Igor Sysoev | ac5deaa | 2008-01-31 15:14:31 +0000 | [diff] [blame] | 288 | /* the calculation is valid for positive time_t only */ |
Igor Sysoev | 4611ad3 | 2008-04-13 13:33:12 +0000 | [diff] [blame] | 289 | |
Igor Sysoev | ac5deaa | 2008-01-31 15:14:31 +0000 | [diff] [blame] | 290 | n = (ngx_uint_t) t; |
| 291 | |
| 292 | days = n / 86400; |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 293 | |
Ruslan Ermilov | b74f8ff | 2012-02-28 11:31:05 +0000 | [diff] [blame] | 294 | /* January 1, 1970 was Thursday */ |
Igor Sysoev | 4611ad3 | 2008-04-13 13:33:12 +0000 | [diff] [blame] | 295 | |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 296 | wday = (4 + days) % 7; |
| 297 | |
Igor Sysoev | ac5deaa | 2008-01-31 15:14:31 +0000 | [diff] [blame] | 298 | n %= 86400; |
| 299 | hour = n / 3600; |
| 300 | n %= 3600; |
| 301 | min = n / 60; |
| 302 | sec = n % 60; |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 303 | |
Igor Sysoev | 4611ad3 | 2008-04-13 13:33:12 +0000 | [diff] [blame] | 304 | /* |
| 305 | * the algorithm based on Gauss' formula, |
| 306 | * see src/http/ngx_http_parse_time.c |
| 307 | */ |
Igor Sysoev | 5f80078 | 2003-12-08 20:48:12 +0000 | [diff] [blame] | 308 | |
Igor Sysoev | 4611ad3 | 2008-04-13 13:33:12 +0000 | [diff] [blame] | 309 | /* days since March 1, 1 BC */ |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 310 | days = days - (31 + 28) + 719527; |
| 311 | |
Igor Sysoev | 4611ad3 | 2008-04-13 13:33:12 +0000 | [diff] [blame] | 312 | /* |
| 313 | * The "days" should be adjusted to 1 only, however, some March 1st's go |
| 314 | * to previous year, so we adjust them to 2. This causes also shift of the |
Ruslan Ermilov | 47a04aa | 2012-04-03 07:37:31 +0000 | [diff] [blame] | 315 | * last February days to next year, but we catch the case when "yday" |
Igor Sysoev | 4611ad3 | 2008-04-13 13:33:12 +0000 | [diff] [blame] | 316 | * becomes negative. |
| 317 | */ |
| 318 | |
| 319 | year = (days + 2) * 400 / (365 * 400 + 100 - 4 + 1); |
| 320 | |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 321 | yday = days - (365 * year + year / 4 - year / 100 + year / 400); |
| 322 | |
Igor Sysoev | 4611ad3 | 2008-04-13 13:33:12 +0000 | [diff] [blame] | 323 | if (yday < 0) { |
| 324 | leap = (year % 4 == 0) && (year % 100 || (year % 400 == 0)); |
| 325 | yday = 365 + leap + yday; |
| 326 | year--; |
| 327 | } |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 328 | |
Igor Sysoev | 4611ad3 | 2008-04-13 13:33:12 +0000 | [diff] [blame] | 329 | /* |
| 330 | * The empirical formula that maps "yday" to month. |
| 331 | * There are at least 10 variants, some of them are: |
| 332 | * mon = (yday + 31) * 15 / 459 |
| 333 | * mon = (yday + 31) * 17 / 520 |
| 334 | * mon = (yday + 31) * 20 / 612 |
| 335 | */ |
| 336 | |
| 337 | mon = (yday + 31) * 10 / 306; |
| 338 | |
| 339 | /* the Gauss' formula that evaluates days before the month */ |
| 340 | |
| 341 | mday = yday - (367 * mon / 12 - 30) + 1; |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 342 | |
| 343 | if (yday >= 306) { |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 344 | |
Igor Sysoev | 4611ad3 | 2008-04-13 13:33:12 +0000 | [diff] [blame] | 345 | year++; |
| 346 | mon -= 10; |
| 347 | |
Igor Sysoev | 11dbe97 | 2004-03-29 17:43:58 +0000 | [diff] [blame] | 348 | /* |
Igor Sysoev | e772e8f | 2004-04-21 20:13:48 +0000 | [diff] [blame] | 349 | * there is no "yday" in Win32 SYSTEMTIME |
Igor Sysoev | 11dbe97 | 2004-03-29 17:43:58 +0000 | [diff] [blame] | 350 | * |
| 351 | * yday -= 306; |
| 352 | */ |
| 353 | |
Igor Sysoev | 4611ad3 | 2008-04-13 13:33:12 +0000 | [diff] [blame] | 354 | } else { |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 355 | |
Igor Sysoev | 4611ad3 | 2008-04-13 13:33:12 +0000 | [diff] [blame] | 356 | mon += 2; |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 357 | |
Igor Sysoev | 4611ad3 | 2008-04-13 13:33:12 +0000 | [diff] [blame] | 358 | /* |
| 359 | * there is no "yday" in Win32 SYSTEMTIME |
| 360 | * |
| 361 | * yday += 31 + 28 + leap; |
| 362 | */ |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 365 | tp->ngx_tm_sec = (ngx_tm_sec_t) sec; |
| 366 | tp->ngx_tm_min = (ngx_tm_min_t) min; |
| 367 | tp->ngx_tm_hour = (ngx_tm_hour_t) hour; |
| 368 | tp->ngx_tm_mday = (ngx_tm_mday_t) mday; |
| 369 | tp->ngx_tm_mon = (ngx_tm_mon_t) mon; |
| 370 | tp->ngx_tm_year = (ngx_tm_year_t) year; |
| 371 | tp->ngx_tm_wday = (ngx_tm_wday_t) wday; |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 372 | } |
Igor Sysoev | c271775 | 2008-08-11 15:28:15 +0000 | [diff] [blame] | 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 | } |