| 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 | |
| 43 | #pragma once |
| 44 | |
| 45 | #include <math.h> |
| 46 | #include <stdint.h> |
| 47 | #include <string.h> |
| 48 | #include <time.h> |
| 49 | #include <wtf/WallTime.h> |
| 50 | #include <wtf/text/WTFString.h> |
| 51 | |
| 52 | namespace WTF { |
| 53 | |
| 54 | enum TimeType { |
| 55 | UTCTime = 0, |
| 56 | LocalTime |
| 57 | }; |
| 58 | |
| 59 | struct LocalTimeOffset { |
| 60 | LocalTimeOffset() |
| 61 | : isDST(false) |
| 62 | , offset(0) |
| 63 | { |
| 64 | } |
| 65 | |
| 66 | LocalTimeOffset(bool isDST, int offset) |
| 67 | : isDST(isDST) |
| 68 | , offset(offset) |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | bool operator==(const LocalTimeOffset& other) |
| 73 | { |
| 74 | return isDST == other.isDST && offset == other.offset; |
| 75 | } |
| 76 | |
| 77 | bool operator!=(const LocalTimeOffset& other) |
| 78 | { |
| 79 | return isDST != other.isDST || offset != other.offset; |
| 80 | } |
| 81 | |
| 82 | bool isDST; |
| 83 | int offset; |
| 84 | }; |
| 85 | |
| 86 | void initializeDates(); |
| 87 | int equivalentYearForDST(int year); |
| 88 | |
| 89 | // Not really math related, but this is currently the only shared place to put these. |
| 90 | WTF_EXPORT_PRIVATE double parseES5DateFromNullTerminatedCharacters(const char* dateString); |
| 91 | WTF_EXPORT_PRIVATE double parseDateFromNullTerminatedCharacters(const char* dateString); |
| 92 | WTF_EXPORT_PRIVATE double parseDateFromNullTerminatedCharacters(const char* dateString, bool& haveTZ, int& offset); |
| 93 | WTF_EXPORT_PRIVATE double timeClip(double); |
| 94 | // 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]. |
| 95 | String makeRFC2822DateString(unsigned dayOfWeek, unsigned day, unsigned month, unsigned year, unsigned hours, unsigned minutes, unsigned seconds, int utcOffset); |
| 96 | |
| 97 | inline double jsCurrentTime() |
| 98 | { |
| 99 | // JavaScript doesn't recognize fractions of a millisecond. |
| 100 | return floor(WallTime::now().secondsSinceEpoch().milliseconds()); |
| 101 | } |
| 102 | |
| 103 | const char* const weekdayName[7] = { "Mon" , "Tue" , "Wed" , "Thu" , "Fri" , "Sat" , "Sun" }; |
| 104 | const char* const monthName[12] = { "Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec" }; |
| 105 | const char* const monthFullName[12] = { "January" , "February" , "March" , "April" , "May" , "June" , "July" , "August" , "September" , "October" , "November" , "December" }; |
| 106 | |
| 107 | const double hoursPerDay = 24.0; |
| 108 | const double minutesPerHour = 60.0; |
| 109 | const double secondsPerMinute = 60.0; |
| 110 | const double msPerSecond = 1000.0; |
| 111 | const double msPerMonth = 2592000000.0; |
| 112 | const double secondsPerHour = secondsPerMinute * minutesPerHour; |
| 113 | const double secondsPerDay = secondsPerHour * hoursPerDay; |
| 114 | const double msPerMinute = msPerSecond * secondsPerMinute; |
| 115 | const double msPerHour = msPerSecond * secondsPerHour; |
| 116 | const double msPerDay = msPerSecond * secondsPerDay; |
| 117 | |
| 118 | WTF_EXPORT_PRIVATE bool isLeapYear(int year); |
| 119 | |
| 120 | // Returns the number of days from 1970-01-01 to the specified date. |
| 121 | WTF_EXPORT_PRIVATE double dateToDaysFrom1970(int year, int month, int day); |
| 122 | WTF_EXPORT_PRIVATE int msToYear(double ms); |
| 123 | WTF_EXPORT_PRIVATE double msToDays(double ms); |
| 124 | WTF_EXPORT_PRIVATE int msToMinutes(double ms); |
| 125 | WTF_EXPORT_PRIVATE int msToHours(double ms); |
| 126 | WTF_EXPORT_PRIVATE int dayInYear(int year, int month, int day); |
| 127 | WTF_EXPORT_PRIVATE int dayInYear(double ms, int year); |
| 128 | WTF_EXPORT_PRIVATE int monthFromDayInYear(int dayInYear, bool leapYear); |
| 129 | WTF_EXPORT_PRIVATE int dayInMonthFromDayInYear(int dayInYear, bool leapYear); |
| 130 | |
| 131 | // Returns combined offset in millisecond (UTC + DST). |
| 132 | WTF_EXPORT_PRIVATE LocalTimeOffset calculateLocalTimeOffset(double utcInMilliseconds, TimeType = UTCTime); |
| 133 | |
| 134 | } // namespace WTF |
| 135 | |
| 136 | using WTF::isLeapYear; |
| 137 | using WTF::dateToDaysFrom1970; |
| 138 | using WTF::dayInMonthFromDayInYear; |
| 139 | using WTF::dayInYear; |
| 140 | using WTF::minutesPerHour; |
| 141 | using WTF::monthFromDayInYear; |
| 142 | using WTF::msPerDay; |
| 143 | using WTF::msPerHour; |
| 144 | using WTF::msPerMinute; |
| 145 | using WTF::msPerSecond; |
| 146 | using WTF::msToYear; |
| 147 | using WTF::msToDays; |
| 148 | using WTF::msToMinutes; |
| 149 | using WTF::msToHours; |
| 150 | using WTF::secondsPerDay; |
| 151 | using WTF::secondsPerMinute; |
| 152 | using WTF::parseDateFromNullTerminatedCharacters; |
| 153 | using WTF::makeRFC2822DateString; |
| 154 | using WTF::LocalTimeOffset; |
| 155 | using WTF::calculateLocalTimeOffset; |
| 156 | |