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

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

Issue 394903004: document.lastModified should consider user's local time zone (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Fix windows build error Created 6 years, 5 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
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 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 } 188 }
189 189
190 static inline double msToMilliseconds(double ms) 190 static inline double msToMilliseconds(double ms)
191 { 191 {
192 double result = fmod(ms, msPerDay); 192 double result = fmod(ms, msPerDay);
193 if (result < 0) 193 if (result < 0)
194 result += msPerDay; 194 result += msPerDay;
195 return result; 195 return result;
196 } 196 }
197 197
198 static int msToMinutes(double ms)
199 {
200 double result = fmod(floor(ms / msPerMinute), minutesPerHour);
201 if (result < 0)
202 result += minutesPerHour;
203 return static_cast<int>(result);
204 }
205
206 static int msToHours(double ms)
207 {
208 double result = fmod(floor(ms/msPerHour), hoursPerDay);
209 if (result < 0)
210 result += hoursPerDay;
211 return static_cast<int>(result);
212 }
213
214 int monthFromDayInYear(int dayInYear, bool leapYear) 198 int monthFromDayInYear(int dayInYear, bool leapYear)
215 { 199 {
216 const int d = dayInYear; 200 const int d = dayInYear;
217 int step; 201 int step;
218 202
219 if (d < (step = 31)) 203 if (d < (step = 31))
220 return 0; 204 return 0;
221 step += (leapYear ? 29 : 28); 205 step += (leapYear ? 29 : 28);
222 if (d < step) 206 if (d < step)
223 return 1; 207 return 1;
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 return year; 335 return year;
352 336
353 int quotient = difference / 28; 337 int quotient = difference / 28;
354 int product = (quotient) * 28; 338 int product = (quotient) * 28;
355 339
356 year += product; 340 year += product;
357 ASSERT((year >= minYear && year <= maxYear) || (product - year == static_cas t<int>(std::numeric_limits<double>::quiet_NaN()))); 341 ASSERT((year >= minYear && year <= maxYear) || (product - year == static_cas t<int>(std::numeric_limits<double>::quiet_NaN())));
358 return year; 342 return year;
359 } 343 }
360 344
361 int32_t calculateUTCOffset() 345 static double calculateUTCOffset()
362 { 346 {
363 #if OS(WIN) 347 #if OS(WIN)
364 TIME_ZONE_INFORMATION timeZoneInformation; 348 TIME_ZONE_INFORMATION timeZoneInformation;
365 GetTimeZoneInformation(&timeZoneInformation); 349 GetTimeZoneInformation(&timeZoneInformation);
366 int32_t bias = timeZoneInformation.Bias + timeZoneInformation.StandardBias; 350 int32_t bias = timeZoneInformation.Bias + timeZoneInformation.StandardBias;
367 return -bias * 60 * 1000; 351 return -bias * 60 * 1000;
368 #else 352 #else
369 time_t localTime = time(0); 353 time_t localTime = time(0);
370 tm localt; 354 tm localt;
371 getLocalTime(&localTime, &localt); 355 getLocalTime(&localTime, &localt);
372 356
373 // Get the difference between this time zone and UTC on the 1st of January o f this year. 357 // tm_gmtoff includes any daylight savings offset, so subtract it.
374 localt.tm_sec = 0; 358 return static_cast<double>(localt.tm_gmtoff * msPerSecond - (localt.tm_isdst > 0 ? 3600 * msPerSecond : 0));
tkent 2014/07/28 00:40:26 nit: 3600 * msPerSend -> msPerHour
kangil_ 2014/07/31 00:45:46 Done.
375 localt.tm_min = 0;
376 localt.tm_hour = 0;
377 localt.tm_mday = 1;
378 localt.tm_mon = 0;
379 // Not setting localt.tm_year!
380 localt.tm_wday = 0;
381 localt.tm_yday = 0;
382 localt.tm_isdst = 0;
383 #if HAVE(TM_GMTOFF)
384 localt.tm_gmtoff = 0;
385 #endif
386 #if HAVE(TM_ZONE)
387 localt.tm_zone = 0;
388 #endif
389
390 #if HAVE(TIMEGM)
391 time_t utcOffset = timegm(&localt) - mktime(&localt);
392 #else
393 // Using a canned date of 01/01/2009 on platforms with weaker date-handling foo.
394 localt.tm_year = 109;
395 time_t utcOffset = 1230768000 - mktime(&localt);
396 #endif
397
398 return static_cast<int32_t>(utcOffset * 1000);
399 #endif 359 #endif
400 } 360 }
401 361
402 /* 362 /*
403 * Get the DST offset for the time passed in. 363 * Get the DST offset for the time passed in.
404 */ 364 */
405 static double calculateDSTOffsetSimple(double localTimeSeconds, double utcOffset ) 365 static double calculateDSTOffsetSimple(double localTimeSeconds, double utcOffset )
406 { 366 {
407 if (localTimeSeconds > maxUnixTime) 367 if (localTimeSeconds > maxUnixTime)
408 localTimeSeconds = maxUnixTime; 368 localTimeSeconds = maxUnixTime;
409 else if (localTimeSeconds < 0) // Go ahead a day to make localtime work (doe s not work with 0) 369 else if (localTimeSeconds < 0) // Go ahead a day to make localtime work (doe s not work with 0)
410 localTimeSeconds += secondsPerDay; 370 localTimeSeconds += secondsPerDay;
411 371
412 //input is UTC so we have to shift back to local time to determine DST thus the + getUTCOffset()
413 double offsetTime = (localTimeSeconds * msPerSecond) + utcOffset;
414
415 // Offset from UTC but doesn't include DST obviously
416 int offsetHour = msToHours(offsetTime);
417 int offsetMinute = msToMinutes(offsetTime);
418
419 // FIXME: time_t has a potential problem in 2038 372 // FIXME: time_t has a potential problem in 2038
420 time_t localTime = static_cast<time_t>(localTimeSeconds); 373 time_t localTime = static_cast<time_t>(localTimeSeconds);
421 374
422 tm localTM; 375 tm localTM;
423 getLocalTime(&localTime, &localTM); 376 getLocalTime(&localTime, &localTM);
424 377
425 double diff = ((localTM.tm_hour - offsetHour) * secondsPerHour) + ((localTM. tm_min - offsetMinute) * 60); 378 return localTM.tm_isdst > 0 ? secondsPerHour * msPerSecond : 0;
tkent 2014/07/28 00:40:26 nit: secondsPerHour * msPerSecond -> msPerHour
kangil_ 2014/07/31 00:45:46 Done.
426
427 if (diff < 0)
428 diff += secondsPerDay;
429
430 return (diff * msPerSecond);
431 } 379 }
432 380
433 // Get the DST offset, given a time in UTC 381 // Get the DST offset, given a time in UTC
434 double calculateDSTOffset(double ms, double utcOffset) 382 static double calculateDSTOffset(double ms, double utcOffset)
435 { 383 {
436 // On Mac OS X, the call to localtime (see calculateDSTOffsetSimple) will re turn historically accurate 384 // On Mac OS X, the call to localtime (see calculateDSTOffsetSimple) will re turn historically accurate
437 // DST information (e.g. New Zealand did not have DST from 1946 to 1974) how ever the JavaScript 385 // DST information (e.g. New Zealand did not have DST from 1946 to 1974) how ever the JavaScript
438 // standard explicitly dictates that historical information should not be co nsidered when 386 // standard explicitly dictates that historical information should not be co nsidered when
439 // determining DST. For this reason we shift away from years that localtime can handle but would 387 // determining DST. For this reason we shift away from years that localtime can handle but would
440 // return historically accurate information. 388 // return historically accurate information.
441 int year = msToYear(ms); 389 int year = msToYear(ms);
442 int equivalentYear = equivalentYearForDST(year); 390 int equivalentYear = equivalentYearForDST(year);
443 if (year != equivalentYear) { 391 if (year != equivalentYear) {
444 bool leapYear = isLeapYear(year); 392 bool leapYear = isLeapYear(year);
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after
874 stringBuilder.append(' '); 822 stringBuilder.append(' ');
875 823
876 stringBuilder.append(utcOffset > 0 ? '+' : '-'); 824 stringBuilder.append(utcOffset > 0 ? '+' : '-');
877 int absoluteUTCOffset = abs(utcOffset); 825 int absoluteUTCOffset = abs(utcOffset);
878 stringBuilder.append(twoDigitStringFromNumber(absoluteUTCOffset / 60)); 826 stringBuilder.append(twoDigitStringFromNumber(absoluteUTCOffset / 60));
879 stringBuilder.append(twoDigitStringFromNumber(absoluteUTCOffset % 60)); 827 stringBuilder.append(twoDigitStringFromNumber(absoluteUTCOffset % 60));
880 828
881 return stringBuilder.toString(); 829 return stringBuilder.toString();
882 } 830 }
883 831
832 double convertToLocalTime(double ms)
833 {
834 double utcOffset = calculateUTCOffset();
835 double dstOffset = calculateDSTOffset(ms, utcOffset);
836 return (ms + utcOffset + dstOffset);
837 }
838
884 } // namespace WTF 839 } // namespace WTF
OLDNEW
« Source/core/html/shadow/DateTimeFieldElements.cpp ('K') | « Source/wtf/DateMath.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698