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 |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | |
Igor Sysoev | d59a047 | 2003-11-10 21:09:22 +0000 | [diff] [blame] | 7 | #include <ngx_config.h> |
| 8 | #include <ngx_core.h> |
| 9 | |
| 10 | |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 11 | /* |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 12 | * The time may be updated by signal handler or by several threads. |
| 13 | * The time update operations are rare and require to hold the ngx_time_lock. |
| 14 | * The time read operations are frequent, so they are lock-free and get time |
| 15 | * values and strings from the current slot. Thus thread may get the corrupted |
| 16 | * values only if it is preempted while copying and then it is not scheduled |
| 17 | * to run more than NGX_TIME_SLOTS seconds. |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 20 | #define NGX_TIME_SLOTS 64 |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 21 | |
Igor Sysoev | f4b34c4 | 2006-12-19 12:38:20 +0000 | [diff] [blame] | 22 | static ngx_uint_t slot; |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 23 | static ngx_atomic_t ngx_time_lock; |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 24 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 25 | volatile ngx_msec_t ngx_current_msec; |
| 26 | volatile ngx_time_t *ngx_cached_time; |
| 27 | volatile ngx_str_t ngx_cached_err_log_time; |
| 28 | volatile ngx_str_t ngx_cached_http_time; |
| 29 | volatile ngx_str_t ngx_cached_http_log_time; |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 30 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 31 | static ngx_time_t cached_time[NGX_TIME_SLOTS]; |
| 32 | static u_char cached_err_log_time[NGX_TIME_SLOTS] |
| 33 | [sizeof("1970/09/28 12:00:00")]; |
| 34 | static u_char cached_http_time[NGX_TIME_SLOTS] |
| 35 | [sizeof("Mon, 28 Sep 1970 06:00:00 GMT")]; |
| 36 | static u_char cached_http_log_time[NGX_TIME_SLOTS] |
| 37 | [sizeof("28/Sep/1970:12:00:00 +0600")]; |
Igor Sysoev | d59a047 | 2003-11-10 21:09:22 +0000 | [diff] [blame] | 38 | |
| 39 | |
Igor Sysoev | 3f4685f | 2004-04-25 20:13:21 +0000 | [diff] [blame] | 40 | static char *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 41 | static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 42 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 43 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 44 | void |
| 45 | ngx_time_init(void) |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 46 | { |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 47 | ngx_cached_err_log_time.len = sizeof("1970/09/28 12:00:00") - 1; |
| 48 | ngx_cached_http_time.len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1; |
| 49 | ngx_cached_http_log_time.len = sizeof("28/Sep/1970:12:00:00 +0600") - 1; |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 50 | |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 51 | ngx_cached_time = &cached_time[0]; |
Igor Sysoev | f5003d8 | 2003-12-04 14:53:00 +0000 | [diff] [blame] | 52 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 53 | ngx_time_update(0, 0); |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 57 | void |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 58 | ngx_time_update(time_t sec, ngx_uint_t msec) |
Igor Sysoev | d59a047 | 2003-11-10 21:09:22 +0000 | [diff] [blame] | 59 | { |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 60 | u_char *p0, *p1, *p2; |
| 61 | ngx_tm_t tm, gmt; |
| 62 | ngx_time_t *tp; |
| 63 | struct timeval tv; |
Igor Sysoev | d59a047 | 2003-11-10 21:09:22 +0000 | [diff] [blame] | 64 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 65 | if (!ngx_trylock(&ngx_time_lock)) { |
Igor Sysoev | 898446c | 2004-02-26 17:10:01 +0000 | [diff] [blame] | 66 | return; |
Igor Sysoev | b54698b | 2004-02-23 20:57:12 +0000 | [diff] [blame] | 67 | } |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 68 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 69 | if (sec == 0) { |
| 70 | ngx_gettimeofday(&tv); |
Igor Sysoev | b54698b | 2004-02-23 20:57:12 +0000 | [diff] [blame] | 71 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 72 | sec = tv.tv_sec; |
| 73 | msec = tv.tv_usec / 1000; |
| 74 | } |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 75 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 76 | ngx_current_msec = (ngx_msec_t) sec * 1000 + msec; |
Igor Sysoev | c024730 | 2004-06-27 18:01:57 +0000 | [diff] [blame] | 77 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 78 | tp = &cached_time[slot]; |
| 79 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 80 | if (tp->sec == sec) { |
Igor Sysoev | c3d106a | 2006-12-06 14:25:20 +0000 | [diff] [blame] | 81 | tp->msec = msec; |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 82 | ngx_unlock(&ngx_time_lock); |
| 83 | return; |
| 84 | } |
| 85 | |
Igor Sysoev | f51d4fe | 2007-01-29 11:52:25 +0000 | [diff] [blame] | 86 | if (slot == NGX_TIME_SLOTS - 1) { |
Igor Sysoev | c3d106a | 2006-12-06 14:25:20 +0000 | [diff] [blame] | 87 | slot = 0; |
| 88 | } else { |
| 89 | slot++; |
| 90 | } |
| 91 | |
| 92 | tp = &cached_time[slot]; |
| 93 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 94 | tp->sec = sec; |
Igor Sysoev | c3d106a | 2006-12-06 14:25:20 +0000 | [diff] [blame] | 95 | tp->msec = msec; |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 96 | |
| 97 | ngx_gmtime(sec, &gmt); |
Igor Sysoev | d59a047 | 2003-11-10 21:09:22 +0000 | [diff] [blame] | 98 | |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 99 | |
Igor Sysoev | 80a2901 | 2007-01-25 22:01:23 +0000 | [diff] [blame] | 100 | p0 = &cached_http_time[slot][0]; |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 101 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 102 | (void) ngx_sprintf(p0, "%s, %02d %s %4d %02d:%02d:%02d GMT", |
| 103 | week[gmt.ngx_tm_wday], gmt.ngx_tm_mday, |
| 104 | months[gmt.ngx_tm_mon - 1], gmt.ngx_tm_year, |
| 105 | gmt.ngx_tm_hour, gmt.ngx_tm_min, gmt.ngx_tm_sec); |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 106 | |
Igor Sysoev | f690604 | 2004-11-25 16:17:31 +0000 | [diff] [blame] | 107 | #if (NGX_HAVE_GETTIMEZONE) |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 108 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 109 | tp->gmtoff = ngx_gettimezone(); |
| 110 | ngx_gmtime(sec + tp->gmtoff * 60, &tm); |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 111 | |
Igor Sysoev | f690604 | 2004-11-25 16:17:31 +0000 | [diff] [blame] | 112 | #elif (NGX_HAVE_GMTOFF) |
Igor Sysoev | 415b1ce | 2004-06-17 17:18:53 +0000 | [diff] [blame] | 113 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 114 | ngx_localtime(sec, &tm); |
| 115 | tp->gmtoff = (ngx_int_t) (tm.ngx_tm_gmtoff / 60); |
Igor Sysoev | 415b1ce | 2004-06-17 17:18:53 +0000 | [diff] [blame] | 116 | |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 117 | #else |
Igor Sysoev | d59a047 | 2003-11-10 21:09:22 +0000 | [diff] [blame] | 118 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 119 | ngx_localtime(sec, &tm); |
| 120 | tp->gmtoff = ngx_timezone(tm.ngx_tm_isdst); |
Igor Sysoev | d59a047 | 2003-11-10 21:09:22 +0000 | [diff] [blame] | 121 | |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 122 | #endif |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 123 | |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 124 | |
Igor Sysoev | 80a2901 | 2007-01-25 22:01:23 +0000 | [diff] [blame] | 125 | p1 = &cached_err_log_time[slot][0]; |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 126 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 127 | (void) ngx_sprintf(p1, "%4d/%02d/%02d %02d:%02d:%02d", |
Igor Sysoev | 4959ec4 | 2005-05-23 12:07:45 +0000 | [diff] [blame] | 128 | tm.ngx_tm_year, tm.ngx_tm_mon, |
| 129 | tm.ngx_tm_mday, tm.ngx_tm_hour, |
| 130 | tm.ngx_tm_min, tm.ngx_tm_sec); |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 131 | |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 132 | |
Igor Sysoev | 80a2901 | 2007-01-25 22:01:23 +0000 | [diff] [blame] | 133 | p2 = &cached_http_log_time[slot][0]; |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 134 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 135 | (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] | 136 | tm.ngx_tm_mday, months[tm.ngx_tm_mon - 1], |
| 137 | tm.ngx_tm_year, tm.ngx_tm_hour, |
| 138 | tm.ngx_tm_min, tm.ngx_tm_sec, |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 139 | tp->gmtoff < 0 ? '-' : '+', |
| 140 | ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60)); |
Igor Sysoev | 732a271 | 2004-04-21 18:54:33 +0000 | [diff] [blame] | 141 | |
Igor Sysoev | b54698b | 2004-02-23 20:57:12 +0000 | [diff] [blame] | 142 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 143 | ngx_memory_barrier(); |
Igor Sysoev | b54698b | 2004-02-23 20:57:12 +0000 | [diff] [blame] | 144 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 145 | ngx_cached_time = tp; |
| 146 | ngx_cached_http_time.data = p0; |
| 147 | ngx_cached_err_log_time.data = p1; |
| 148 | ngx_cached_http_log_time.data = p2; |
| 149 | |
| 150 | ngx_unlock(&ngx_time_lock); |
Igor Sysoev | d59a047 | 2003-11-10 21:09:22 +0000 | [diff] [blame] | 151 | } |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 152 | |
| 153 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 154 | u_char * |
| 155 | ngx_http_time(u_char *buf, time_t t) |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 156 | { |
| 157 | ngx_tm_t tm; |
| 158 | |
| 159 | ngx_gmtime(t, &tm); |
| 160 | |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 161 | return ngx_sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d GMT", |
| 162 | week[tm.ngx_tm_wday], |
| 163 | tm.ngx_tm_mday, |
| 164 | months[tm.ngx_tm_mon - 1], |
| 165 | tm.ngx_tm_year, |
| 166 | tm.ngx_tm_hour, |
| 167 | tm.ngx_tm_min, |
| 168 | tm.ngx_tm_sec); |
Igor Sysoev | a7c4a2a | 2004-08-29 03:55:41 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 172 | u_char * |
| 173 | ngx_http_cookie_time(u_char *buf, time_t t) |
Igor Sysoev | a7c4a2a | 2004-08-29 03:55:41 +0000 | [diff] [blame] | 174 | { |
| 175 | ngx_tm_t tm; |
| 176 | |
| 177 | ngx_gmtime(t, &tm); |
| 178 | |
| 179 | /* |
| 180 | * Netscape 3.x does not understand 4-digit years at all and |
| 181 | * 2-digit years more than "37" |
| 182 | */ |
| 183 | |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 184 | return ngx_sprintf(buf, |
| 185 | (tm.ngx_tm_year > 2037) ? |
| 186 | "%s, %02d-%s-%d %02d:%02d:%02d GMT": |
| 187 | "%s, %02d-%s-%02d %02d:%02d:%02d GMT", |
| 188 | week[tm.ngx_tm_wday], |
| 189 | tm.ngx_tm_mday, |
| 190 | months[tm.ngx_tm_mon - 1], |
| 191 | (tm.ngx_tm_year > 2037) ? tm.ngx_tm_year: |
| 192 | tm.ngx_tm_year % 100, |
| 193 | tm.ngx_tm_hour, |
| 194 | tm.ngx_tm_min, |
| 195 | tm.ngx_tm_sec); |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 199 | void |
| 200 | ngx_gmtime(time_t t, ngx_tm_t *tp) |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 201 | { |
Igor Sysoev | 4611ad3 | 2008-04-13 13:33:12 +0000 | [diff] [blame] | 202 | ngx_int_t yday; |
| 203 | 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] | 204 | |
Igor Sysoev | ac5deaa | 2008-01-31 15:14:31 +0000 | [diff] [blame] | 205 | /* the calculation is valid for positive time_t only */ |
Igor Sysoev | 4611ad3 | 2008-04-13 13:33:12 +0000 | [diff] [blame] | 206 | |
Igor Sysoev | ac5deaa | 2008-01-31 15:14:31 +0000 | [diff] [blame] | 207 | n = (ngx_uint_t) t; |
| 208 | |
| 209 | days = n / 86400; |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 210 | |
| 211 | /* Jaunary 1, 1970 was Thursday */ |
Igor Sysoev | 4611ad3 | 2008-04-13 13:33:12 +0000 | [diff] [blame] | 212 | |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 213 | wday = (4 + days) % 7; |
| 214 | |
Igor Sysoev | ac5deaa | 2008-01-31 15:14:31 +0000 | [diff] [blame] | 215 | n %= 86400; |
| 216 | hour = n / 3600; |
| 217 | n %= 3600; |
| 218 | min = n / 60; |
| 219 | sec = n % 60; |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 220 | |
Igor Sysoev | 4611ad3 | 2008-04-13 13:33:12 +0000 | [diff] [blame] | 221 | /* |
| 222 | * the algorithm based on Gauss' formula, |
| 223 | * see src/http/ngx_http_parse_time.c |
| 224 | */ |
Igor Sysoev | 5f80078 | 2003-12-08 20:48:12 +0000 | [diff] [blame] | 225 | |
Igor Sysoev | 4611ad3 | 2008-04-13 13:33:12 +0000 | [diff] [blame] | 226 | /* days since March 1, 1 BC */ |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 227 | days = days - (31 + 28) + 719527; |
| 228 | |
Igor Sysoev | 4611ad3 | 2008-04-13 13:33:12 +0000 | [diff] [blame] | 229 | /* |
| 230 | * The "days" should be adjusted to 1 only, however, some March 1st's go |
| 231 | * to previous year, so we adjust them to 2. This causes also shift of the |
| 232 | * last Feburary days to next year, but we catch the case when "yday" |
| 233 | * becomes negative. |
| 234 | */ |
| 235 | |
| 236 | year = (days + 2) * 400 / (365 * 400 + 100 - 4 + 1); |
| 237 | |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 238 | yday = days - (365 * year + year / 4 - year / 100 + year / 400); |
| 239 | |
Igor Sysoev | 4611ad3 | 2008-04-13 13:33:12 +0000 | [diff] [blame] | 240 | if (yday < 0) { |
| 241 | leap = (year % 4 == 0) && (year % 100 || (year % 400 == 0)); |
| 242 | yday = 365 + leap + yday; |
| 243 | year--; |
| 244 | } |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 245 | |
Igor Sysoev | 4611ad3 | 2008-04-13 13:33:12 +0000 | [diff] [blame] | 246 | /* |
| 247 | * The empirical formula that maps "yday" to month. |
| 248 | * There are at least 10 variants, some of them are: |
| 249 | * mon = (yday + 31) * 15 / 459 |
| 250 | * mon = (yday + 31) * 17 / 520 |
| 251 | * mon = (yday + 31) * 20 / 612 |
| 252 | */ |
| 253 | |
| 254 | mon = (yday + 31) * 10 / 306; |
| 255 | |
| 256 | /* the Gauss' formula that evaluates days before the month */ |
| 257 | |
| 258 | mday = yday - (367 * mon / 12 - 30) + 1; |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 259 | |
| 260 | if (yday >= 306) { |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 261 | |
Igor Sysoev | 4611ad3 | 2008-04-13 13:33:12 +0000 | [diff] [blame] | 262 | year++; |
| 263 | mon -= 10; |
| 264 | |
Igor Sysoev | 11dbe97 | 2004-03-29 17:43:58 +0000 | [diff] [blame] | 265 | /* |
Igor Sysoev | e772e8f | 2004-04-21 20:13:48 +0000 | [diff] [blame] | 266 | * there is no "yday" in Win32 SYSTEMTIME |
Igor Sysoev | 11dbe97 | 2004-03-29 17:43:58 +0000 | [diff] [blame] | 267 | * |
| 268 | * yday -= 306; |
| 269 | */ |
| 270 | |
Igor Sysoev | 4611ad3 | 2008-04-13 13:33:12 +0000 | [diff] [blame] | 271 | } else { |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 272 | |
Igor Sysoev | 4611ad3 | 2008-04-13 13:33:12 +0000 | [diff] [blame] | 273 | mon += 2; |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 274 | |
Igor Sysoev | 4611ad3 | 2008-04-13 13:33:12 +0000 | [diff] [blame] | 275 | /* |
| 276 | * there is no "yday" in Win32 SYSTEMTIME |
| 277 | * |
| 278 | * yday += 31 + 28 + leap; |
| 279 | */ |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 282 | tp->ngx_tm_sec = (ngx_tm_sec_t) sec; |
| 283 | tp->ngx_tm_min = (ngx_tm_min_t) min; |
| 284 | tp->ngx_tm_hour = (ngx_tm_hour_t) hour; |
| 285 | tp->ngx_tm_mday = (ngx_tm_mday_t) mday; |
| 286 | tp->ngx_tm_mon = (ngx_tm_mon_t) mon; |
| 287 | tp->ngx_tm_year = (ngx_tm_year_t) year; |
| 288 | tp->ngx_tm_wday = (ngx_tm_wday_t) wday; |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 289 | } |
Igor Sysoev | c271775 | 2008-08-11 15:28:15 +0000 | [diff] [blame] | 290 | |
| 291 | |
| 292 | time_t |
| 293 | ngx_next_time(time_t when) |
| 294 | { |
| 295 | time_t now, next; |
| 296 | struct tm tm; |
| 297 | |
| 298 | now = ngx_time(); |
| 299 | |
| 300 | ngx_libc_localtime(now, &tm); |
| 301 | |
| 302 | tm.tm_hour = (int) (when / 3600); |
| 303 | when %= 3600; |
| 304 | tm.tm_min = (int) (when / 60); |
| 305 | tm.tm_sec = (int) (when % 60); |
| 306 | |
| 307 | next = mktime(&tm); |
| 308 | |
| 309 | if (next == -1) { |
| 310 | return -1; |
| 311 | } |
| 312 | |
| 313 | if (next - now > 0) { |
| 314 | return next; |
| 315 | } |
| 316 | |
| 317 | tm.tm_mday++; |
| 318 | |
| 319 | /* mktime() should normalize a date (Jan 32, etc) */ |
| 320 | |
| 321 | next = mktime(&tm); |
| 322 | |
| 323 | if (next != -1) { |
| 324 | return next; |
| 325 | } |
| 326 | |
| 327 | return -1; |
| 328 | } |