DateMath.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
  3. * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
  4. * Copyright (C) 2009 Google Inc. All rights reserved.
  5. * Copyright (C) 2010 Research In Motion Limited. All rights reserved.
  6. *
  7. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  8. *
  9. * The contents of this file are subject to the Mozilla Public License Version
  10. * 1.1 (the "License"); you may not use this file except in compliance with
  11. * the License. You may obtain a copy of the License at
  12. * http://www.mozilla.org/MPL/
  13. *
  14. * Software distributed under the License is distributed on an "AS IS" basis,
  15. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  16. * for the specific language governing rights and limitations under the
  17. * License.
  18. *
  19. * The Original Code is Mozilla Communicator client code, released
  20. * March 31, 1998.
  21. *
  22. * The Initial Developer of the Original Code is
  23. * Netscape Communications Corporation.
  24. * Portions created by the Initial Developer are Copyright (C) 1998
  25. * the Initial Developer. All Rights Reserved.
  26. *
  27. * Contributor(s):
  28. *
  29. * Alternatively, the contents of this file may be used under the terms of
  30. * either of the GNU General Public License Version 2 or later (the "GPL"),
  31. * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  32. * in which case the provisions of the GPL or the LGPL are applicable instead
  33. * of those above. If you wish to allow use of your version of this file only
  34. * under the terms of either the GPL or the LGPL, and not to allow others to
  35. * use your version of this file under the terms of the MPL, indicate your
  36. * decision by deleting the provisions above and replace them with the notice
  37. * and other provisions required by the GPL or the LGPL. If you do not delete
  38. * the provisions above, a recipient may use your version of this file under
  39. * the terms of any one of the MPL, the GPL or the LGPL.
  40. *
  41. */
  42. #ifndef DateMath_h
  43. #define DateMath_h
  44. #include <math.h>
  45. #include <stdint.h>
  46. #include <string.h>
  47. #include <time.h>
  48. #include <wtf/CurrentTime.h>
  49. #include <wtf/Noncopyable.h>
  50. #include <wtf/OwnArrayPtr.h>
  51. #include <wtf/PassOwnArrayPtr.h>
  52. #include <wtf/text/WTFString.h>
  53. namespace WTF {
  54. struct LocalTimeOffset {
  55. LocalTimeOffset()
  56. : isDST(false)
  57. , offset(0)
  58. {
  59. }
  60. LocalTimeOffset(bool isDST, int offset)
  61. : isDST(isDST)
  62. , offset(offset)
  63. {
  64. }
  65. bool operator==(const LocalTimeOffset& other)
  66. {
  67. return isDST == other.isDST && offset == other.offset;
  68. }
  69. bool operator!=(const LocalTimeOffset& other)
  70. {
  71. return isDST != other.isDST || offset != other.offset;
  72. }
  73. bool isDST;
  74. int offset;
  75. };
  76. void initializeDates();
  77. int equivalentYearForDST(int year);
  78. // Not really math related, but this is currently the only shared place to put these.
  79. WTF_EXPORT_PRIVATE double parseES5DateFromNullTerminatedCharacters(const char* dateString);
  80. WTF_EXPORT_PRIVATE double parseDateFromNullTerminatedCharacters(const char* dateString);
  81. WTF_EXPORT_PRIVATE double parseDateFromNullTerminatedCharacters(const char* dateString, bool& haveTZ, int& offset);
  82. WTF_EXPORT_PRIVATE double timeClip(double);
  83. // dayOfWeek: [0, 6] 0 being Monday, day: [1, 31], month: [0, 11], year: ex: 2011, hours: [0, 23], minutes: [0, 59], seconds: [0, 59], utcOffset: [-720,720].
  84. String makeRFC2822DateString(unsigned dayOfWeek, unsigned day, unsigned month, unsigned year, unsigned hours, unsigned minutes, unsigned seconds, int utcOffset);
  85. inline double jsCurrentTime()
  86. {
  87. // JavaScript doesn't recognize fractions of a millisecond.
  88. return floor(WTF::currentTimeMS());
  89. }
  90. const char* const weekdayName[7] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
  91. const char* const monthName[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  92. const char* const monthFullName[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
  93. const double hoursPerDay = 24.0;
  94. const double minutesPerHour = 60.0;
  95. const double secondsPerHour = 60.0 * 60.0;
  96. const double secondsPerMinute = 60.0;
  97. const double msPerSecond = 1000.0;
  98. const double msPerMinute = 60.0 * 1000.0;
  99. const double msPerHour = 60.0 * 60.0 * 1000.0;
  100. const double msPerDay = 24.0 * 60.0 * 60.0 * 1000.0;
  101. const double msPerMonth = 2592000000.0;
  102. WTF_EXPORT_PRIVATE bool isLeapYear(int year);
  103. // Returns the number of days from 1970-01-01 to the specified date.
  104. WTF_EXPORT_PRIVATE double dateToDaysFrom1970(int year, int month, int day);
  105. WTF_EXPORT_PRIVATE int msToYear(double ms);
  106. WTF_EXPORT_PRIVATE double msToDays(double ms);
  107. WTF_EXPORT_PRIVATE int msToMinutes(double ms);
  108. WTF_EXPORT_PRIVATE int msToHours(double ms);
  109. WTF_EXPORT_PRIVATE int dayInYear(int year, int month, int day);
  110. WTF_EXPORT_PRIVATE int dayInYear(double ms, int year);
  111. WTF_EXPORT_PRIVATE int monthFromDayInYear(int dayInYear, bool leapYear);
  112. WTF_EXPORT_PRIVATE int dayInMonthFromDayInYear(int dayInYear, bool leapYear);
  113. // Returns combined offset in millisecond (UTC + DST).
  114. WTF_EXPORT_PRIVATE LocalTimeOffset calculateLocalTimeOffset(double utcInMilliseconds);
  115. } // namespace WTF
  116. using WTF::isLeapYear;
  117. using WTF::dateToDaysFrom1970;
  118. using WTF::dayInMonthFromDayInYear;
  119. using WTF::dayInYear;
  120. using WTF::minutesPerHour;
  121. using WTF::monthFromDayInYear;
  122. using WTF::msPerDay;
  123. using WTF::msPerHour;
  124. using WTF::msPerMinute;
  125. using WTF::msPerSecond;
  126. using WTF::msToYear;
  127. using WTF::msToDays;
  128. using WTF::msToMinutes;
  129. using WTF::msToHours;
  130. using WTF::secondsPerMinute;
  131. using WTF::parseDateFromNullTerminatedCharacters;
  132. using WTF::makeRFC2822DateString;
  133. using WTF::LocalTimeOffset;
  134. using WTF::calculateLocalTimeOffset;
  135. #endif // DateMath_h