Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(225)

Side by Side Diff: third_party/WebKit/Source/wtf/DateMath.cpp

Issue 1611343002: wtf reformat test Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: pydent Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/wtf/DateMath.h ('k') | third_party/WebKit/Source/wtf/Deque.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) 2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved. 3 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
4 * Copyright (C) 2009 Google Inc. All rights reserved. 4 * Copyright (C) 2009 Google Inc. All rights reserved.
5 * Copyright (C) 2007-2009 Torch Mobile, Inc. 5 * Copyright (C) 2007-2009 Torch Mobile, Inc.
6 * Copyright (C) 2010 &yet, LLC. (nate@andyet.net) 6 * Copyright (C) 2010 &yet, LLC. (nate@andyet.net)
7 * 7 *
8 * The Original Code is Mozilla Communicator client code, released 8 * The Original Code is Mozilla Communicator client code, released
9 * March 31, 1998. 9 * March 31, 1998.
10 * 10 *
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 #include <sys/time.h> 91 #include <sys/time.h>
92 #endif 92 #endif
93 93
94 namespace WTF { 94 namespace WTF {
95 95
96 /* Constants */ 96 /* Constants */
97 97
98 static const double hoursPerDay = 24.0; 98 static const double hoursPerDay = 24.0;
99 static const double secondsPerDay = 24.0 * 60.0 * 60.0; 99 static const double secondsPerDay = 24.0 * 60.0 * 60.0;
100 100
101 static const double maxUnixTime = 2145859200.0; // 12/31/2037 101 static const double maxUnixTime = 2145859200.0; // 12/31/2037
102 102
103 // Day of year for the first day of each month, where index 0 is January, and da y 0 is January 1. 103 // Day of year for the first day of each month, where index 0 is January, and da y 0 is January 1.
104 // First for non-leap years, then for leap years. 104 // First for non-leap years, then for leap years.
105 static const int firstDayOfMonth[2][12] = { 105 static const int firstDayOfMonth[2][12] = {
106 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}, 106 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
107 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335} 107 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}};
108 };
109 108
110 static inline void getLocalTime(const time_t* localTime, struct tm* localTM) 109 static inline void getLocalTime(const time_t* localTime, struct tm* localTM) {
111 {
112 #if COMPILER(MSVC) 110 #if COMPILER(MSVC)
113 localtime_s(localTM, localTime); 111 localtime_s(localTM, localTime);
114 #else 112 #else
115 localtime_r(localTime, localTM); 113 localtime_r(localTime, localTM);
116 #endif 114 #endif
117 } 115 }
118 116
119 bool isLeapYear(int year) 117 bool isLeapYear(int year) {
120 { 118 if (year % 4 != 0)
121 if (year % 4 != 0) 119 return false;
122 return false; 120 if (year % 400 == 0)
123 if (year % 400 == 0)
124 return true;
125 if (year % 100 == 0)
126 return false;
127 return true; 121 return true;
122 if (year % 100 == 0)
123 return false;
124 return true;
128 } 125 }
129 126
130 static inline int daysInYear(int year) 127 static inline int daysInYear(int year) {
131 { 128 return 365 + isLeapYear(year);
132 return 365 + isLeapYear(year);
133 } 129 }
134 130
135 static inline double daysFrom1970ToYear(int year) 131 static inline double daysFrom1970ToYear(int year) {
136 { 132 // The Gregorian Calendar rules for leap years:
137 // The Gregorian Calendar rules for leap years: 133 // Every fourth year is a leap year. 2004, 2008, and 2012 are leap years.
138 // Every fourth year is a leap year. 2004, 2008, and 2012 are leap years. 134 // However, every hundredth year is not a leap year. 1900 and 2100 are not le ap years.
139 // However, every hundredth year is not a leap year. 1900 and 2100 are not leap years. 135 // Every four hundred years, there's a leap year after all. 2000 and 2400 are leap years.
140 // Every four hundred years, there's a leap year after all. 2000 and 2400 a re leap years.
141 136
142 static const int leapDaysBefore1971By4Rule = 1970 / 4; 137 static const int leapDaysBefore1971By4Rule = 1970 / 4;
143 static const int excludedLeapDaysBefore1971By100Rule = 1970 / 100; 138 static const int excludedLeapDaysBefore1971By100Rule = 1970 / 100;
144 static const int leapDaysBefore1971By400Rule = 1970 / 400; 139 static const int leapDaysBefore1971By400Rule = 1970 / 400;
145 140
146 const double yearMinusOne = year - 1; 141 const double yearMinusOne = year - 1;
147 const double yearsToAddBy4Rule = floor(yearMinusOne / 4.0) - leapDaysBefore1 971By4Rule; 142 const double yearsToAddBy4Rule =
148 const double yearsToExcludeBy100Rule = floor(yearMinusOne / 100.0) - exclude dLeapDaysBefore1971By100Rule; 143 floor(yearMinusOne / 4.0) - leapDaysBefore1971By4Rule;
149 const double yearsToAddBy400Rule = floor(yearMinusOne / 400.0) - leapDaysBef ore1971By400Rule; 144 const double yearsToExcludeBy100Rule =
145 floor(yearMinusOne / 100.0) - excludedLeapDaysBefore1971By100Rule;
146 const double yearsToAddBy400Rule =
147 floor(yearMinusOne / 400.0) - leapDaysBefore1971By400Rule;
150 148
151 return 365.0 * (year - 1970) + yearsToAddBy4Rule - yearsToExcludeBy100Rule + yearsToAddBy400Rule; 149 return 365.0 * (year - 1970) + yearsToAddBy4Rule - yearsToExcludeBy100Rule +
150 yearsToAddBy400Rule;
152 } 151 }
153 152
154 static double msToDays(double ms) 153 static double msToDays(double ms) {
155 { 154 return floor(ms / msPerDay);
156 return floor(ms / msPerDay);
157 } 155 }
158 156
159 static void appendTwoDigitNumber(StringBuilder& builder, int number) 157 static void appendTwoDigitNumber(StringBuilder& builder, int number) {
160 { 158 ASSERT(number >= 0 && number < 100);
161 ASSERT(number >= 0 && number < 100); 159 if (number <= 9)
162 if (number <= 9) 160 builder.append('0');
163 builder.append('0'); 161 builder.appendNumber(number);
164 builder.appendNumber(number);
165 } 162 }
166 163
167 int msToYear(double ms) 164 int msToYear(double ms) {
168 { 165 int approxYear = static_cast<int>(floor(ms / (msPerDay * 365.2425)) + 1970);
169 int approxYear = static_cast<int>(floor(ms / (msPerDay * 365.2425)) + 1970); 166 double msFromApproxYearTo1970 = msPerDay * daysFrom1970ToYear(approxYear);
170 double msFromApproxYearTo1970 = msPerDay * daysFrom1970ToYear(approxYear); 167 if (msFromApproxYearTo1970 > ms)
171 if (msFromApproxYearTo1970 > ms) 168 return approxYear - 1;
172 return approxYear - 1; 169 if (msFromApproxYearTo1970 + msPerDay * daysInYear(approxYear) <= ms)
173 if (msFromApproxYearTo1970 + msPerDay * daysInYear(approxYear) <= ms) 170 return approxYear + 1;
174 return approxYear + 1; 171 return approxYear;
175 return approxYear;
176 } 172 }
177 173
178 int dayInYear(double ms, int year) 174 int dayInYear(double ms, int year) {
179 { 175 return static_cast<int>(msToDays(ms) - daysFrom1970ToYear(year));
180 return static_cast<int>(msToDays(ms) - daysFrom1970ToYear(year));
181 } 176 }
182 177
183 static inline double msToMilliseconds(double ms) 178 static inline double msToMilliseconds(double ms) {
184 { 179 double result = fmod(ms, msPerDay);
185 double result = fmod(ms, msPerDay); 180 if (result < 0)
186 if (result < 0) 181 result += msPerDay;
187 result += msPerDay; 182 return result;
188 return result;
189 } 183 }
190 184
191 int monthFromDayInYear(int dayInYear, bool leapYear) 185 int monthFromDayInYear(int dayInYear, bool leapYear) {
192 { 186 const int d = dayInYear;
193 const int d = dayInYear; 187 int step;
194 int step;
195 188
196 if (d < (step = 31)) 189 if (d < (step = 31))
197 return 0; 190 return 0;
198 step += (leapYear ? 29 : 28); 191 step += (leapYear ? 29 : 28);
199 if (d < step) 192 if (d < step)
200 return 1; 193 return 1;
201 if (d < (step += 31)) 194 if (d < (step += 31))
202 return 2; 195 return 2;
203 if (d < (step += 30)) 196 if (d < (step += 30))
204 return 3; 197 return 3;
205 if (d < (step += 31)) 198 if (d < (step += 31))
206 return 4; 199 return 4;
207 if (d < (step += 30)) 200 if (d < (step += 30))
208 return 5; 201 return 5;
209 if (d < (step += 31)) 202 if (d < (step += 31))
210 return 6; 203 return 6;
211 if (d < (step += 31)) 204 if (d < (step += 31))
212 return 7; 205 return 7;
213 if (d < (step += 30)) 206 if (d < (step += 30))
214 return 8; 207 return 8;
215 if (d < (step += 31)) 208 if (d < (step += 31))
216 return 9; 209 return 9;
217 if (d < (step += 30)) 210 if (d < (step += 30))
218 return 10; 211 return 10;
219 return 11; 212 return 11;
220 } 213 }
221 214
222 static inline bool checkMonth(int dayInYear, int& startDayOfThisMonth, int& star tDayOfNextMonth, int daysInThisMonth) 215 static inline bool checkMonth(int dayInYear,
223 { 216 int& startDayOfThisMonth,
224 startDayOfThisMonth = startDayOfNextMonth; 217 int& startDayOfNextMonth,
225 startDayOfNextMonth += daysInThisMonth; 218 int daysInThisMonth) {
226 return (dayInYear <= startDayOfNextMonth); 219 startDayOfThisMonth = startDayOfNextMonth;
220 startDayOfNextMonth += daysInThisMonth;
221 return (dayInYear <= startDayOfNextMonth);
227 } 222 }
228 223
229 int dayInMonthFromDayInYear(int dayInYear, bool leapYear) 224 int dayInMonthFromDayInYear(int dayInYear, bool leapYear) {
230 { 225 const int d = dayInYear;
231 const int d = dayInYear; 226 int step;
232 int step; 227 int next = 30;
233 int next = 30;
234 228
235 if (d <= next) 229 if (d <= next)
236 return d + 1; 230 return d + 1;
237 const int daysInFeb = (leapYear ? 29 : 28); 231 const int daysInFeb = (leapYear ? 29 : 28);
238 if (checkMonth(d, step, next, daysInFeb)) 232 if (checkMonth(d, step, next, daysInFeb))
239 return d - step;
240 if (checkMonth(d, step, next, 31))
241 return d - step;
242 if (checkMonth(d, step, next, 30))
243 return d - step;
244 if (checkMonth(d, step, next, 31))
245 return d - step;
246 if (checkMonth(d, step, next, 30))
247 return d - step;
248 if (checkMonth(d, step, next, 31))
249 return d - step;
250 if (checkMonth(d, step, next, 31))
251 return d - step;
252 if (checkMonth(d, step, next, 30))
253 return d - step;
254 if (checkMonth(d, step, next, 31))
255 return d - step;
256 if (checkMonth(d, step, next, 30))
257 return d - step;
258 step = next;
259 return d - step; 233 return d - step;
234 if (checkMonth(d, step, next, 31))
235 return d - step;
236 if (checkMonth(d, step, next, 30))
237 return d - step;
238 if (checkMonth(d, step, next, 31))
239 return d - step;
240 if (checkMonth(d, step, next, 30))
241 return d - step;
242 if (checkMonth(d, step, next, 31))
243 return d - step;
244 if (checkMonth(d, step, next, 31))
245 return d - step;
246 if (checkMonth(d, step, next, 30))
247 return d - step;
248 if (checkMonth(d, step, next, 31))
249 return d - step;
250 if (checkMonth(d, step, next, 30))
251 return d - step;
252 step = next;
253 return d - step;
260 } 254 }
261 255
262 int dayInYear(int year, int month, int day) 256 int dayInYear(int year, int month, int day) {
263 { 257 return firstDayOfMonth[isLeapYear(year)][month] + day - 1;
264 return firstDayOfMonth[isLeapYear(year)][month] + day - 1;
265 } 258 }
266 259
267 double dateToDaysFrom1970(int year, int month, int day) 260 double dateToDaysFrom1970(int year, int month, int day) {
268 { 261 year += month / 12;
269 year += month / 12;
270 262
271 month %= 12; 263 month %= 12;
272 if (month < 0) { 264 if (month < 0) {
273 month += 12; 265 month += 12;
274 --year; 266 --year;
275 } 267 }
276 268
277 double yearday = floor(daysFrom1970ToYear(year)); 269 double yearday = floor(daysFrom1970ToYear(year));
278 ASSERT((year >= 1970 && yearday >= 0) || (year < 1970 && yearday < 0)); 270 ASSERT((year >= 1970 && yearday >= 0) || (year < 1970 && yearday < 0));
279 return yearday + dayInYear(year, month, day); 271 return yearday + dayInYear(year, month, day);
280 } 272 }
281 273
282 // There is a hard limit at 2038 that we currently do not have a workaround 274 // There is a hard limit at 2038 that we currently do not have a workaround
283 // for (rdar://problem/5052975). 275 // for (rdar://problem/5052975).
284 static inline int maximumYearForDST() 276 static inline int maximumYearForDST() {
285 { 277 return 2037;
286 return 2037;
287 } 278 }
288 279
289 static inline double jsCurrentTime() 280 static inline double jsCurrentTime() {
290 { 281 // JavaScript doesn't recognize fractions of a millisecond.
291 // JavaScript doesn't recognize fractions of a millisecond. 282 return floor(WTF::currentTimeMS());
292 return floor(WTF::currentTimeMS());
293 } 283 }
294 284
295 static inline int minimumYearForDST() 285 static inline int minimumYearForDST() {
296 { 286 // Because of the 2038 issue (see maximumYearForDST) if the current year is
297 // Because of the 2038 issue (see maximumYearForDST) if the current year is 287 // greater than the max year minus 27 (2010), we want to use the max year
298 // greater than the max year minus 27 (2010), we want to use the max year 288 // minus 27 instead, to ensure there is a range of 28 years that all years
299 // minus 27 instead, to ensure there is a range of 28 years that all years 289 // can map to.
300 // can map to. 290 return std::min(msToYear(jsCurrentTime()), maximumYearForDST() - 27);
301 return std::min(msToYear(jsCurrentTime()), maximumYearForDST() - 27);
302 } 291 }
303 292
304 /* 293 /*
305 * Find an equivalent year for the one given, where equivalence is deterined by 294 * Find an equivalent year for the one given, where equivalence is deterined by
306 * the two years having the same leapness and the first day of the year, falling 295 * the two years having the same leapness and the first day of the year, falling
307 * on the same day of the week. 296 * on the same day of the week.
308 * 297 *
309 * This function returns a year between this current year and 2037, however this 298 * This function returns a year between this current year and 2037, however this
310 * function will potentially return incorrect results if the current year is aft er 299 * function will potentially return incorrect results if the current year is aft er
311 * 2010, (rdar://problem/5052975), if the year passed in is before 1900 or after 300 * 2010, (rdar://problem/5052975), if the year passed in is before 1900 or after
312 * 2100, (rdar://problem/5055038). 301 * 2100, (rdar://problem/5055038).
313 */ 302 */
314 static int equivalentYearForDST(int year) 303 static int equivalentYearForDST(int year) {
315 { 304 // It is ok if the cached year is not the current year as long as the rules
316 // It is ok if the cached year is not the current year as long as the rules 305 // for DST did not change between the two years; if they did the app would nee d
317 // for DST did not change between the two years; if they did the app would n eed 306 // to be restarted.
318 // to be restarted. 307 static int minYear = minimumYearForDST();
319 static int minYear = minimumYearForDST(); 308 int maxYear = maximumYearForDST();
320 int maxYear = maximumYearForDST(); 309
321 310 int difference;
322 int difference; 311 if (year > maxYear)
323 if (year > maxYear) 312 difference = minYear - year;
324 difference = minYear - year; 313 else if (year < minYear)
325 else if (year < minYear) 314 difference = maxYear - year;
326 difference = maxYear - year; 315 else
327 else
328 return year;
329
330 int quotient = difference / 28;
331 int product = (quotient) * 28;
332
333 year += product;
334 ASSERT((year >= minYear && year <= maxYear) || (product - year == static_cas t<int>(std::numeric_limits<double>::quiet_NaN())));
335 return year; 316 return year;
336 } 317
337 318 int quotient = difference / 28;
338 static double calculateUTCOffset() 319 int product = (quotient)*28;
339 { 320
321 year += product;
322 ASSERT((year >= minYear && year <= maxYear) ||
323 (product - year ==
324 static_cast<int>(std::numeric_limits<double>::quiet_NaN())));
325 return year;
326 }
327
328 static double calculateUTCOffset() {
340 #if OS(WIN) 329 #if OS(WIN)
341 TIME_ZONE_INFORMATION timeZoneInformation; 330 TIME_ZONE_INFORMATION timeZoneInformation;
342 GetTimeZoneInformation(&timeZoneInformation); 331 GetTimeZoneInformation(&timeZoneInformation);
343 int32_t bias = timeZoneInformation.Bias + timeZoneInformation.StandardBias; 332 int32_t bias = timeZoneInformation.Bias + timeZoneInformation.StandardBias;
344 return -bias * 60 * 1000; 333 return -bias * 60 * 1000;
345 #else 334 #else
346 time_t localTime = time(0); 335 time_t localTime = time(0);
347 tm localt; 336 tm localt;
348 getLocalTime(&localTime, &localt); 337 getLocalTime(&localTime, &localt);
349 338
350 // tm_gmtoff includes any daylight savings offset, so subtract it. 339 // tm_gmtoff includes any daylight savings offset, so subtract it.
351 return static_cast<double>(localt.tm_gmtoff * msPerSecond - (localt.tm_isdst > 0 ? msPerHour : 0)); 340 return static_cast<double>(localt.tm_gmtoff * msPerSecond -
341 (localt.tm_isdst > 0 ? msPerHour : 0));
352 #endif 342 #endif
353 } 343 }
354 344
355 /* 345 /*
356 * Get the DST offset for the time passed in. 346 * Get the DST offset for the time passed in.
357 */ 347 */
358 static double calculateDSTOffsetSimple(double localTimeSeconds, double utcOffset ) 348 static double calculateDSTOffsetSimple(double localTimeSeconds,
359 { 349 double utcOffset) {
360 if (localTimeSeconds > maxUnixTime) 350 if (localTimeSeconds > maxUnixTime)
361 localTimeSeconds = maxUnixTime; 351 localTimeSeconds = maxUnixTime;
362 else if (localTimeSeconds < 0) // Go ahead a day to make localtime work (doe s not work with 0) 352 else if (localTimeSeconds <
363 localTimeSeconds += secondsPerDay; 353 0) // Go ahead a day to make localtime work (does not work with 0)
364 354 localTimeSeconds += secondsPerDay;
365 // FIXME: time_t has a potential problem in 2038 355
366 time_t localTime = static_cast<time_t>(localTimeSeconds); 356 // FIXME: time_t has a potential problem in 2038
367 357 time_t localTime = static_cast<time_t>(localTimeSeconds);
368 tm localTM; 358
369 getLocalTime(&localTime, &localTM); 359 tm localTM;
370 360 getLocalTime(&localTime, &localTM);
371 return localTM.tm_isdst > 0 ? msPerHour : 0; 361
362 return localTM.tm_isdst > 0 ? msPerHour : 0;
372 } 363 }
373 364
374 // Get the DST offset, given a time in UTC 365 // Get the DST offset, given a time in UTC
375 static double calculateDSTOffset(double ms, double utcOffset) 366 static double calculateDSTOffset(double ms, double utcOffset) {
376 { 367 // On Mac OS X, the call to localtime (see calculateDSTOffsetSimple) will retu rn historically accurate
377 // On Mac OS X, the call to localtime (see calculateDSTOffsetSimple) will re turn historically accurate 368 // DST information (e.g. New Zealand did not have DST from 1946 to 1974) howev er the JavaScript
378 // DST information (e.g. New Zealand did not have DST from 1946 to 1974) how ever the JavaScript 369 // standard explicitly dictates that historical information should not be cons idered when
379 // standard explicitly dictates that historical information should not be co nsidered when 370 // determining DST. For this reason we shift away from years that localtime ca n handle but would
380 // determining DST. For this reason we shift away from years that localtime can handle but would 371 // return historically accurate information.
381 // return historically accurate information. 372 int year = msToYear(ms);
382 int year = msToYear(ms); 373 int equivalentYear = equivalentYearForDST(year);
383 int equivalentYear = equivalentYearForDST(year); 374 if (year != equivalentYear) {
384 if (year != equivalentYear) { 375 bool leapYear = isLeapYear(year);
385 bool leapYear = isLeapYear(year); 376 int dayInYearLocal = dayInYear(ms, year);
386 int dayInYearLocal = dayInYear(ms, year); 377 int dayInMonth = dayInMonthFromDayInYear(dayInYearLocal, leapYear);
387 int dayInMonth = dayInMonthFromDayInYear(dayInYearLocal, leapYear); 378 int month = monthFromDayInYear(dayInYearLocal, leapYear);
388 int month = monthFromDayInYear(dayInYearLocal, leapYear); 379 double day = dateToDaysFrom1970(equivalentYear, month, dayInMonth);
389 double day = dateToDaysFrom1970(equivalentYear, month, dayInMonth); 380 ms = (day * msPerDay) + msToMilliseconds(ms);
390 ms = (day * msPerDay) + msToMilliseconds(ms); 381 }
391 } 382
392 383 return calculateDSTOffsetSimple(ms / msPerSecond, utcOffset);
393 return calculateDSTOffsetSimple(ms / msPerSecond, utcOffset); 384 }
394 } 385
395 386 void initializeDates() {
396 void initializeDates()
397 {
398 #if ENABLE(ASSERT) 387 #if ENABLE(ASSERT)
399 static bool alreadyInitialized; 388 static bool alreadyInitialized;
400 ASSERT(!alreadyInitialized); 389 ASSERT(!alreadyInitialized);
401 alreadyInitialized = true; 390 alreadyInitialized = true;
402 #endif 391 #endif
403 392
404 equivalentYearForDST(2000); // Need to call once to initialize a static used in this function. 393 equivalentYearForDST(
405 } 394 2000); // Need to call once to initialize a static used in this function.
406 395 }
407 static inline double ymdhmsToSeconds(int year, long mon, long day, long hour, lo ng minute, double second) 396
408 { 397 static inline double ymdhmsToSeconds(int year,
409 double days = (day - 32075) 398 long mon,
410 + floor(1461 * (year + 4800.0 + (mon - 14) / 12) / 4) 399 long day,
411 + 367 * (mon - 2 - (mon - 14) / 12 * 12) / 12 400 long hour,
412 - floor(3 * ((year + 4900.0 + (mon - 14) / 12) / 100) / 4) 401 long minute,
413 - 2440588; 402 double second) {
414 return ((days * hoursPerDay + hour) * minutesPerHour + minute) * secondsPerM inute + second; 403 double days =
404 (day - 32075) + floor(1461 * (year + 4800.0 + (mon - 14) / 12) / 4) +
405 367 * (mon - 2 - (mon - 14) / 12 * 12) / 12 -
406 floor(3 * ((year + 4900.0 + (mon - 14) / 12) / 100) / 4) - 2440588;
407 return ((days * hoursPerDay + hour) * minutesPerHour + minute) *
408 secondsPerMinute +
409 second;
415 } 410 }
416 411
417 // We follow the recommendation of RFC 2822 to consider all 412 // We follow the recommendation of RFC 2822 to consider all
418 // obsolete time zones not listed here equivalent to "-0000". 413 // obsolete time zones not listed here equivalent to "-0000".
419 static const struct KnownZone { 414 static const struct KnownZone {
420 #if !OS(WIN) 415 #if !OS(WIN)
421 const 416 const
422 #endif 417 #endif
423 char tzName[4]; 418 char tzName[4];
424 int tzOffset; 419 int tzOffset;
425 } known_zones[] = { 420 } known_zones[] = {{"UT", 0}, {"GMT", 0}, {"EST", -300}, {"EDT", -240},
426 { "UT", 0 }, 421 {"CST", -360}, {"CDT", -300}, {"MST", -420}, {"MDT", -360},
427 { "GMT", 0 }, 422 {"PST", -480}, {"PDT", -420}};
428 { "EST", -300 }, 423
429 { "EDT", -240 }, 424 inline static void skipSpacesAndComments(const char*& s) {
430 { "CST", -360 }, 425 int nesting = 0;
431 { "CDT", -300 }, 426 char ch;
432 { "MST", -420 }, 427 while ((ch = *s)) {
433 { "MDT", -360 }, 428 if (!isASCIISpace(ch)) {
434 { "PST", -480 }, 429 if (ch == '(')
435 { "PDT", -420 } 430 nesting++;
436 }; 431 else if (ch == ')' && nesting > 0)
437 432 nesting--;
438 inline static void skipSpacesAndComments(const char*& s) 433 else if (nesting == 0)
439 { 434 break;
440 int nesting = 0; 435 }
441 char ch; 436 s++;
442 while ((ch = *s)) { 437 }
443 if (!isASCIISpace(ch)) { 438 }
444 if (ch == '(') 439
445 nesting++; 440 // returns 0-11 (Jan-Dec); -1 on failure
446 else if (ch == ')' && nesting > 0) 441 static int findMonth(const char* monthStr) {
447 nesting--; 442 ASSERT(monthStr);
448 else if (nesting == 0) 443 char needle[4];
449 break; 444 for (int i = 0; i < 3; ++i) {
445 if (!*monthStr)
446 return -1;
447 needle[i] = static_cast<char>(toASCIILower(*monthStr++));
448 }
449 needle[3] = '\0';
450 const char* haystack = "janfebmaraprmayjunjulaugsepoctnovdec";
451 const char* str = strstr(haystack, needle);
452 if (str) {
453 int position = static_cast<int>(str - haystack);
454 if (position % 3 == 0)
455 return position / 3;
456 }
457 return -1;
458 }
459
460 static bool parseInt(const char* string,
461 char** stopPosition,
462 int base,
463 int* result) {
464 long longResult = strtol(string, stopPosition, base);
465 // Avoid the use of errno as it is not available on Windows CE
466 if (string == *stopPosition ||
467 longResult <= std::numeric_limits<int>::min() ||
468 longResult >= std::numeric_limits<int>::max())
469 return false;
470 *result = static_cast<int>(longResult);
471 return true;
472 }
473
474 static bool parseLong(const char* string,
475 char** stopPosition,
476 int base,
477 long* result) {
478 *result = strtol(string, stopPosition, base);
479 // Avoid the use of errno as it is not available on Windows CE
480 if (string == *stopPosition || *result == std::numeric_limits<long>::min() ||
481 *result == std::numeric_limits<long>::max())
482 return false;
483 return true;
484 }
485
486 // Odd case where 'exec' is allowed to be 0, to accomodate a caller in WebCore.
487 static double parseDateFromNullTerminatedCharacters(const char* dateString,
488 bool& haveTZ,
489 int& offset) {
490 haveTZ = false;
491 offset = 0;
492
493 // This parses a date in the form:
494 // Tuesday, 09-Nov-99 23:12:40 GMT
495 // or
496 // Sat, 01-Jan-2000 08:00:00 GMT
497 // or
498 // Sat, 01 Jan 2000 08:00:00 GMT
499 // or
500 // 01 Jan 99 22:00 +0100 (exceptions in rfc822/rfc2822)
501 // ### non RFC formats, added for Javascript:
502 // [Wednesday] January 09 1999 23:12:40 GMT
503 // [Wednesday] January 09 23:12:40 GMT 1999
504 //
505 // We ignore the weekday.
506
507 // Skip leading space
508 skipSpacesAndComments(dateString);
509
510 long month = -1;
511 const char* wordStart = dateString;
512 // Check contents of first words if not number
513 while (*dateString && !isASCIIDigit(*dateString)) {
514 if (isASCIISpace(*dateString) || *dateString == '(') {
515 if (dateString - wordStart >= 3)
516 month = findMonth(wordStart);
517 skipSpacesAndComments(dateString);
518 wordStart = dateString;
519 } else {
520 dateString++;
521 }
522 }
523
524 // Missing delimiter between month and day (like "January29")?
525 if (month == -1 && wordStart != dateString)
526 month = findMonth(wordStart);
527
528 skipSpacesAndComments(dateString);
529
530 if (!*dateString)
531 return std::numeric_limits<double>::quiet_NaN();
532
533 // ' 09-Nov-99 23:12:40 GMT'
534 char* newPosStr;
535 long day;
536 if (!parseLong(dateString, &newPosStr, 10, &day))
537 return std::numeric_limits<double>::quiet_NaN();
538 dateString = newPosStr;
539
540 if (!*dateString)
541 return std::numeric_limits<double>::quiet_NaN();
542
543 if (day < 0)
544 return std::numeric_limits<double>::quiet_NaN();
545
546 int year = 0;
547 if (day > 31) {
548 // ### where is the boundary and what happens below?
549 if (*dateString != '/')
550 return std::numeric_limits<double>::quiet_NaN();
551 // looks like a YYYY/MM/DD date
552 if (!*++dateString)
553 return std::numeric_limits<double>::quiet_NaN();
554 if (day <= std::numeric_limits<int>::min() ||
555 day >= std::numeric_limits<int>::max())
556 return std::numeric_limits<double>::quiet_NaN();
557 year = static_cast<int>(day);
558 if (!parseLong(dateString, &newPosStr, 10, &month))
559 return std::numeric_limits<double>::quiet_NaN();
560 month -= 1;
561 dateString = newPosStr;
562 if (*dateString++ != '/' || !*dateString)
563 return std::numeric_limits<double>::quiet_NaN();
564 if (!parseLong(dateString, &newPosStr, 10, &day))
565 return std::numeric_limits<double>::quiet_NaN();
566 dateString = newPosStr;
567 } else if (*dateString == '/' && month == -1) {
568 dateString++;
569 // This looks like a MM/DD/YYYY date, not an RFC date.
570 month = day - 1; // 0-based
571 if (!parseLong(dateString, &newPosStr, 10, &day))
572 return std::numeric_limits<double>::quiet_NaN();
573 if (day < 1 || day > 31)
574 return std::numeric_limits<double>::quiet_NaN();
575 dateString = newPosStr;
576 if (*dateString == '/')
577 dateString++;
578 if (!*dateString)
579 return std::numeric_limits<double>::quiet_NaN();
580 } else {
581 if (*dateString == '-')
582 dateString++;
583
584 skipSpacesAndComments(dateString);
585
586 if (*dateString == ',')
587 dateString++;
588
589 if (month == -1) { // not found yet
590 month = findMonth(dateString);
591 if (month == -1)
592 return std::numeric_limits<double>::quiet_NaN();
593
594 while (*dateString && *dateString != '-' && *dateString != ',' &&
595 !isASCIISpace(*dateString))
596 dateString++;
597
598 if (!*dateString)
599 return std::numeric_limits<double>::quiet_NaN();
600
601 // '-99 23:12:40 GMT'
602 if (*dateString != '-' && *dateString != '/' && *dateString != ',' &&
603 !isASCIISpace(*dateString))
604 return std::numeric_limits<double>::quiet_NaN();
605 dateString++;
606 }
607 }
608
609 if (month < 0 || month > 11)
610 return std::numeric_limits<double>::quiet_NaN();
611
612 // '99 23:12:40 GMT'
613 if (year <= 0 && *dateString) {
614 if (!parseInt(dateString, &newPosStr, 10, &year))
615 return std::numeric_limits<double>::quiet_NaN();
616 }
617
618 // Don't fail if the time is missing.
619 long hour = 0;
620 long minute = 0;
621 long second = 0;
622 if (!*newPosStr) {
623 dateString = newPosStr;
624 } else {
625 // ' 23:12:40 GMT'
626 if (!(isASCIISpace(*newPosStr) || *newPosStr == ',')) {
627 if (*newPosStr != ':')
628 return std::numeric_limits<double>::quiet_NaN();
629 // There was no year; the number was the hour.
630 year = -1;
631 } else {
632 // in the normal case (we parsed the year), advance to the next number
633 dateString = ++newPosStr;
634 skipSpacesAndComments(dateString);
635 }
636
637 parseLong(dateString, &newPosStr, 10, &hour);
638 // Do not check for errno here since we want to continue
639 // even if errno was set becasue we are still looking
640 // for the timezone!
641
642 // Read a number? If not, this might be a timezone name.
643 if (newPosStr != dateString) {
644 dateString = newPosStr;
645
646 if (hour < 0 || hour > 23)
647 return std::numeric_limits<double>::quiet_NaN();
648
649 if (!*dateString)
650 return std::numeric_limits<double>::quiet_NaN();
651
652 // ':12:40 GMT'
653 if (*dateString++ != ':')
654 return std::numeric_limits<double>::quiet_NaN();
655
656 if (!parseLong(dateString, &newPosStr, 10, &minute))
657 return std::numeric_limits<double>::quiet_NaN();
658 dateString = newPosStr;
659
660 if (minute < 0 || minute > 59)
661 return std::numeric_limits<double>::quiet_NaN();
662
663 // ':40 GMT'
664 if (*dateString && *dateString != ':' && !isASCIISpace(*dateString))
665 return std::numeric_limits<double>::quiet_NaN();
666
667 // seconds are optional in rfc822 + rfc2822
668 if (*dateString == ':') {
669 dateString++;
670
671 if (!parseLong(dateString, &newPosStr, 10, &second))
672 return std::numeric_limits<double>::quiet_NaN();
673 dateString = newPosStr;
674
675 if (second < 0 || second > 59)
676 return std::numeric_limits<double>::quiet_NaN();
677 }
678
679 skipSpacesAndComments(dateString);
680
681 if (strncasecmp(dateString, "AM", 2) == 0) {
682 if (hour > 12)
683 return std::numeric_limits<double>::quiet_NaN();
684 if (hour == 12)
685 hour = 0;
686 dateString += 2;
687 skipSpacesAndComments(dateString);
688 } else if (strncasecmp(dateString, "PM", 2) == 0) {
689 if (hour > 12)
690 return std::numeric_limits<double>::quiet_NaN();
691 if (hour != 12)
692 hour += 12;
693 dateString += 2;
694 skipSpacesAndComments(dateString);
695 }
696 }
697 }
698
699 // The year may be after the time but before the time zone.
700 if (isASCIIDigit(*dateString) && year == -1) {
701 if (!parseInt(dateString, &newPosStr, 10, &year))
702 return std::numeric_limits<double>::quiet_NaN();
703 dateString = newPosStr;
704 skipSpacesAndComments(dateString);
705 }
706
707 // Don't fail if the time zone is missing.
708 // Some websites omit the time zone (4275206).
709 if (*dateString) {
710 if (strncasecmp(dateString, "GMT", 3) == 0 ||
711 strncasecmp(dateString, "UTC", 3) == 0) {
712 dateString += 3;
713 haveTZ = true;
714 }
715
716 if (*dateString == '+' || *dateString == '-') {
717 int o;
718 if (!parseInt(dateString, &newPosStr, 10, &o))
719 return std::numeric_limits<double>::quiet_NaN();
720 dateString = newPosStr;
721
722 if (o < -9959 || o > 9959)
723 return std::numeric_limits<double>::quiet_NaN();
724
725 int sgn = (o < 0) ? -1 : 1;
726 o = abs(o);
727 if (*dateString != ':') {
728 if (o >= 24)
729 offset = ((o / 100) * 60 + (o % 100)) * sgn;
730 else
731 offset = o * 60 * sgn;
732 } else { // GMT+05:00
733 ++dateString; // skip the ':'
734 int o2;
735 if (!parseInt(dateString, &newPosStr, 10, &o2))
736 return std::numeric_limits<double>::quiet_NaN();
737 dateString = newPosStr;
738 offset = (o * 60 + o2) * sgn;
739 }
740 haveTZ = true;
741 } else {
742 for (size_t i = 0; i < WTF_ARRAY_LENGTH(known_zones); ++i) {
743 if (0 == strncasecmp(dateString, known_zones[i].tzName,
744 strlen(known_zones[i].tzName))) {
745 offset = known_zones[i].tzOffset;
746 dateString += strlen(known_zones[i].tzName);
747 haveTZ = true;
748 break;
450 } 749 }
451 s++; 750 }
452 } 751 }
453 } 752 }
454 753
455 // returns 0-11 (Jan-Dec); -1 on failure 754 skipSpacesAndComments(dateString);
456 static int findMonth(const char* monthStr) 755
457 { 756 if (*dateString && year == -1) {
458 ASSERT(monthStr); 757 if (!parseInt(dateString, &newPosStr, 10, &year))
459 char needle[4]; 758 return std::numeric_limits<double>::quiet_NaN();
460 for (int i = 0; i < 3; ++i) { 759 dateString = newPosStr;
461 if (!*monthStr)
462 return -1;
463 needle[i] = static_cast<char>(toASCIILower(*monthStr++));
464 }
465 needle[3] = '\0';
466 const char *haystack = "janfebmaraprmayjunjulaugsepoctnovdec";
467 const char *str = strstr(haystack, needle);
468 if (str) {
469 int position = static_cast<int>(str - haystack);
470 if (position % 3 == 0)
471 return position / 3;
472 }
473 return -1;
474 }
475
476 static bool parseInt(const char* string, char** stopPosition, int base, int* res ult)
477 {
478 long longResult = strtol(string, stopPosition, base);
479 // Avoid the use of errno as it is not available on Windows CE
480 if (string == *stopPosition || longResult <= std::numeric_limits<int>::min() || longResult >= std::numeric_limits<int>::max())
481 return false;
482 *result = static_cast<int>(longResult);
483 return true;
484 }
485
486 static bool parseLong(const char* string, char** stopPosition, int base, long* r esult)
487 {
488 *result = strtol(string, stopPosition, base);
489 // Avoid the use of errno as it is not available on Windows CE
490 if (string == *stopPosition || *result == std::numeric_limits<long>::min() | | *result == std::numeric_limits<long>::max())
491 return false;
492 return true;
493 }
494
495 // Odd case where 'exec' is allowed to be 0, to accomodate a caller in WebCore.
496 static double parseDateFromNullTerminatedCharacters(const char* dateString, bool & haveTZ, int& offset)
497 {
498 haveTZ = false;
499 offset = 0;
500
501 // This parses a date in the form:
502 // Tuesday, 09-Nov-99 23:12:40 GMT
503 // or
504 // Sat, 01-Jan-2000 08:00:00 GMT
505 // or
506 // Sat, 01 Jan 2000 08:00:00 GMT
507 // or
508 // 01 Jan 99 22:00 +0100 (exceptions in rfc822/rfc2822)
509 // ### non RFC formats, added for Javascript:
510 // [Wednesday] January 09 1999 23:12:40 GMT
511 // [Wednesday] January 09 23:12:40 GMT 1999
512 //
513 // We ignore the weekday.
514
515 // Skip leading space
516 skipSpacesAndComments(dateString); 760 skipSpacesAndComments(dateString);
517 761 }
518 long month = -1; 762
519 const char *wordStart = dateString; 763 // Trailing garbage
520 // Check contents of first words if not number 764 if (*dateString)
521 while (*dateString && !isASCIIDigit(*dateString)) { 765 return std::numeric_limits<double>::quiet_NaN();
522 if (isASCIISpace(*dateString) || *dateString == '(') { 766
523 if (dateString - wordStart >= 3) 767 // Y2K: Handle 2 digit years.
524 month = findMonth(wordStart); 768 if (year >= 0 && year < 100) {
525 skipSpacesAndComments(dateString); 769 if (year < 50)
526 wordStart = dateString; 770 year += 2000;
527 } else { 771 else
528 dateString++; 772 year += 1900;
529 } 773 }
530 } 774
531 775 return ymdhmsToSeconds(year, month + 1, day, hour, minute, second) *
532 // Missing delimiter between month and day (like "January29")? 776 msPerSecond;
533 if (month == -1 && wordStart != dateString) 777 }
534 month = findMonth(wordStart); 778
535 779 double parseDateFromNullTerminatedCharacters(const char* dateString) {
536 skipSpacesAndComments(dateString); 780 bool haveTZ;
537 781 int offset;
538 if (!*dateString) 782 double ms = parseDateFromNullTerminatedCharacters(dateString, haveTZ, offset);
539 return std::numeric_limits<double>::quiet_NaN(); 783 if (std::isnan(ms))
540 784 return std::numeric_limits<double>::quiet_NaN();
541 // ' 09-Nov-99 23:12:40 GMT' 785
542 char* newPosStr; 786 // fall back to local timezone
543 long day; 787 if (!haveTZ) {
544 if (!parseLong(dateString, &newPosStr, 10, &day))
545 return std::numeric_limits<double>::quiet_NaN();
546 dateString = newPosStr;
547
548 if (!*dateString)
549 return std::numeric_limits<double>::quiet_NaN();
550
551 if (day < 0)
552 return std::numeric_limits<double>::quiet_NaN();
553
554 int year = 0;
555 if (day > 31) {
556 // ### where is the boundary and what happens below?
557 if (*dateString != '/')
558 return std::numeric_limits<double>::quiet_NaN();
559 // looks like a YYYY/MM/DD date
560 if (!*++dateString)
561 return std::numeric_limits<double>::quiet_NaN();
562 if (day <= std::numeric_limits<int>::min() || day >= std::numeric_limits <int>::max())
563 return std::numeric_limits<double>::quiet_NaN();
564 year = static_cast<int>(day);
565 if (!parseLong(dateString, &newPosStr, 10, &month))
566 return std::numeric_limits<double>::quiet_NaN();
567 month -= 1;
568 dateString = newPosStr;
569 if (*dateString++ != '/' || !*dateString)
570 return std::numeric_limits<double>::quiet_NaN();
571 if (!parseLong(dateString, &newPosStr, 10, &day))
572 return std::numeric_limits<double>::quiet_NaN();
573 dateString = newPosStr;
574 } else if (*dateString == '/' && month == -1) {
575 dateString++;
576 // This looks like a MM/DD/YYYY date, not an RFC date.
577 month = day - 1; // 0-based
578 if (!parseLong(dateString, &newPosStr, 10, &day))
579 return std::numeric_limits<double>::quiet_NaN();
580 if (day < 1 || day > 31)
581 return std::numeric_limits<double>::quiet_NaN();
582 dateString = newPosStr;
583 if (*dateString == '/')
584 dateString++;
585 if (!*dateString)
586 return std::numeric_limits<double>::quiet_NaN();
587 } else {
588 if (*dateString == '-')
589 dateString++;
590
591 skipSpacesAndComments(dateString);
592
593 if (*dateString == ',')
594 dateString++;
595
596 if (month == -1) { // not found yet
597 month = findMonth(dateString);
598 if (month == -1)
599 return std::numeric_limits<double>::quiet_NaN();
600
601 while (*dateString && *dateString != '-' && *dateString != ',' && !i sASCIISpace(*dateString))
602 dateString++;
603
604 if (!*dateString)
605 return std::numeric_limits<double>::quiet_NaN();
606
607 // '-99 23:12:40 GMT'
608 if (*dateString != '-' && *dateString != '/' && *dateString != ',' & & !isASCIISpace(*dateString))
609 return std::numeric_limits<double>::quiet_NaN();
610 dateString++;
611 }
612 }
613
614 if (month < 0 || month > 11)
615 return std::numeric_limits<double>::quiet_NaN();
616
617 // '99 23:12:40 GMT'
618 if (year <= 0 && *dateString) {
619 if (!parseInt(dateString, &newPosStr, 10, &year))
620 return std::numeric_limits<double>::quiet_NaN();
621 }
622
623 // Don't fail if the time is missing.
624 long hour = 0;
625 long minute = 0;
626 long second = 0;
627 if (!*newPosStr) {
628 dateString = newPosStr;
629 } else {
630 // ' 23:12:40 GMT'
631 if (!(isASCIISpace(*newPosStr) || *newPosStr == ',')) {
632 if (*newPosStr != ':')
633 return std::numeric_limits<double>::quiet_NaN();
634 // There was no year; the number was the hour.
635 year = -1;
636 } else {
637 // in the normal case (we parsed the year), advance to the next numb er
638 dateString = ++newPosStr;
639 skipSpacesAndComments(dateString);
640 }
641
642 parseLong(dateString, &newPosStr, 10, &hour);
643 // Do not check for errno here since we want to continue
644 // even if errno was set becasue we are still looking
645 // for the timezone!
646
647 // Read a number? If not, this might be a timezone name.
648 if (newPosStr != dateString) {
649 dateString = newPosStr;
650
651 if (hour < 0 || hour > 23)
652 return std::numeric_limits<double>::quiet_NaN();
653
654 if (!*dateString)
655 return std::numeric_limits<double>::quiet_NaN();
656
657 // ':12:40 GMT'
658 if (*dateString++ != ':')
659 return std::numeric_limits<double>::quiet_NaN();
660
661 if (!parseLong(dateString, &newPosStr, 10, &minute))
662 return std::numeric_limits<double>::quiet_NaN();
663 dateString = newPosStr;
664
665 if (minute < 0 || minute > 59)
666 return std::numeric_limits<double>::quiet_NaN();
667
668 // ':40 GMT'
669 if (*dateString && *dateString != ':' && !isASCIISpace(*dateString))
670 return std::numeric_limits<double>::quiet_NaN();
671
672 // seconds are optional in rfc822 + rfc2822
673 if (*dateString ==':') {
674 dateString++;
675
676 if (!parseLong(dateString, &newPosStr, 10, &second))
677 return std::numeric_limits<double>::quiet_NaN();
678 dateString = newPosStr;
679
680 if (second < 0 || second > 59)
681 return std::numeric_limits<double>::quiet_NaN();
682 }
683
684 skipSpacesAndComments(dateString);
685
686 if (strncasecmp(dateString, "AM", 2) == 0) {
687 if (hour > 12)
688 return std::numeric_limits<double>::quiet_NaN();
689 if (hour == 12)
690 hour = 0;
691 dateString += 2;
692 skipSpacesAndComments(dateString);
693 } else if (strncasecmp(dateString, "PM", 2) == 0) {
694 if (hour > 12)
695 return std::numeric_limits<double>::quiet_NaN();
696 if (hour != 12)
697 hour += 12;
698 dateString += 2;
699 skipSpacesAndComments(dateString);
700 }
701 }
702 }
703
704 // The year may be after the time but before the time zone.
705 if (isASCIIDigit(*dateString) && year == -1) {
706 if (!parseInt(dateString, &newPosStr, 10, &year))
707 return std::numeric_limits<double>::quiet_NaN();
708 dateString = newPosStr;
709 skipSpacesAndComments(dateString);
710 }
711
712 // Don't fail if the time zone is missing.
713 // Some websites omit the time zone (4275206).
714 if (*dateString) {
715 if (strncasecmp(dateString, "GMT", 3) == 0 || strncasecmp(dateString, "U TC", 3) == 0) {
716 dateString += 3;
717 haveTZ = true;
718 }
719
720 if (*dateString == '+' || *dateString == '-') {
721 int o;
722 if (!parseInt(dateString, &newPosStr, 10, &o))
723 return std::numeric_limits<double>::quiet_NaN();
724 dateString = newPosStr;
725
726 if (o < -9959 || o > 9959)
727 return std::numeric_limits<double>::quiet_NaN();
728
729 int sgn = (o < 0) ? -1 : 1;
730 o = abs(o);
731 if (*dateString != ':') {
732 if (o >= 24)
733 offset = ((o / 100) * 60 + (o % 100)) * sgn;
734 else
735 offset = o * 60 * sgn;
736 } else { // GMT+05:00
737 ++dateString; // skip the ':'
738 int o2;
739 if (!parseInt(dateString, &newPosStr, 10, &o2))
740 return std::numeric_limits<double>::quiet_NaN();
741 dateString = newPosStr;
742 offset = (o * 60 + o2) * sgn;
743 }
744 haveTZ = true;
745 } else {
746 for (size_t i = 0; i < WTF_ARRAY_LENGTH(known_zones); ++i) {
747 if (0 == strncasecmp(dateString, known_zones[i].tzName, strlen(k nown_zones[i].tzName))) {
748 offset = known_zones[i].tzOffset;
749 dateString += strlen(known_zones[i].tzName);
750 haveTZ = true;
751 break;
752 }
753 }
754 }
755 }
756
757 skipSpacesAndComments(dateString);
758
759 if (*dateString && year == -1) {
760 if (!parseInt(dateString, &newPosStr, 10, &year))
761 return std::numeric_limits<double>::quiet_NaN();
762 dateString = newPosStr;
763 skipSpacesAndComments(dateString);
764 }
765
766 // Trailing garbage
767 if (*dateString)
768 return std::numeric_limits<double>::quiet_NaN();
769
770 // Y2K: Handle 2 digit years.
771 if (year >= 0 && year < 100) {
772 if (year < 50)
773 year += 2000;
774 else
775 year += 1900;
776 }
777
778 return ymdhmsToSeconds(year, month + 1, day, hour, minute, second) * msPerSe cond;
779 }
780
781 double parseDateFromNullTerminatedCharacters(const char* dateString)
782 {
783 bool haveTZ;
784 int offset;
785 double ms = parseDateFromNullTerminatedCharacters(dateString, haveTZ, offset );
786 if (std::isnan(ms))
787 return std::numeric_limits<double>::quiet_NaN();
788
789 // fall back to local timezone
790 if (!haveTZ) {
791 double utcOffset = calculateUTCOffset();
792 double dstOffset = calculateDSTOffset(ms, utcOffset);
793 offset = static_cast<int>((utcOffset + dstOffset) / msPerMinute);
794 }
795 return ms - (offset * msPerMinute);
796 }
797
798 // See http://tools.ietf.org/html/rfc2822#section-3.3 for more information.
799 String makeRFC2822DateString(unsigned dayOfWeek, unsigned day, unsigned month, u nsigned year, unsigned hours, unsigned minutes, unsigned seconds, int utcOffset)
800 {
801 StringBuilder stringBuilder;
802 stringBuilder.append(weekdayName[dayOfWeek]);
803 stringBuilder.appendLiteral(", ");
804 stringBuilder.appendNumber(day);
805 stringBuilder.append(' ');
806 stringBuilder.append(monthName[month]);
807 stringBuilder.append(' ');
808 stringBuilder.appendNumber(year);
809 stringBuilder.append(' ');
810
811 appendTwoDigitNumber(stringBuilder, hours);
812 stringBuilder.append(':');
813 appendTwoDigitNumber(stringBuilder, minutes);
814 stringBuilder.append(':');
815 appendTwoDigitNumber(stringBuilder, seconds);
816 stringBuilder.append(' ');
817
818 stringBuilder.append(utcOffset > 0 ? '+' : '-');
819 int absoluteUTCOffset = abs(utcOffset);
820 appendTwoDigitNumber(stringBuilder, absoluteUTCOffset / 60);
821 appendTwoDigitNumber(stringBuilder, absoluteUTCOffset % 60);
822
823 return stringBuilder.toString();
824 }
825
826 double convertToLocalTime(double ms)
827 {
828 double utcOffset = calculateUTCOffset(); 788 double utcOffset = calculateUTCOffset();
829 double dstOffset = calculateDSTOffset(ms, utcOffset); 789 double dstOffset = calculateDSTOffset(ms, utcOffset);
830 return (ms + utcOffset + dstOffset); 790 offset = static_cast<int>((utcOffset + dstOffset) / msPerMinute);
831 } 791 }
832 792 return ms - (offset * msPerMinute);
833 } // namespace WTF 793 }
794
795 // See http://tools.ietf.org/html/rfc2822#section-3.3 for more information.
796 String makeRFC2822DateString(unsigned dayOfWeek,
797 unsigned day,
798 unsigned month,
799 unsigned year,
800 unsigned hours,
801 unsigned minutes,
802 unsigned seconds,
803 int utcOffset) {
804 StringBuilder stringBuilder;
805 stringBuilder.append(weekdayName[dayOfWeek]);
806 stringBuilder.appendLiteral(", ");
807 stringBuilder.appendNumber(day);
808 stringBuilder.append(' ');
809 stringBuilder.append(monthName[month]);
810 stringBuilder.append(' ');
811 stringBuilder.appendNumber(year);
812 stringBuilder.append(' ');
813
814 appendTwoDigitNumber(stringBuilder, hours);
815 stringBuilder.append(':');
816 appendTwoDigitNumber(stringBuilder, minutes);
817 stringBuilder.append(':');
818 appendTwoDigitNumber(stringBuilder, seconds);
819 stringBuilder.append(' ');
820
821 stringBuilder.append(utcOffset > 0 ? '+' : '-');
822 int absoluteUTCOffset = abs(utcOffset);
823 appendTwoDigitNumber(stringBuilder, absoluteUTCOffset / 60);
824 appendTwoDigitNumber(stringBuilder, absoluteUTCOffset % 60);
825
826 return stringBuilder.toString();
827 }
828
829 double convertToLocalTime(double ms) {
830 double utcOffset = calculateUTCOffset();
831 double dstOffset = calculateDSTOffset(ms, utcOffset);
832 return (ms + utcOffset + dstOffset);
833 }
834
835 } // namespace WTF
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/DateMath.h ('k') | third_party/WebKit/Source/wtf/Deque.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698