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.
7 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
19 * The Original Code is Mozilla Communicator client code, released
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.
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.
50 #include <wtf/CurrentTime.h>
51 #include <wtf/Noncopyable.h>
52 #include <wtf/OwnArrayPtr.h>
53 #include <wtf/PassOwnArrayPtr.h>
54 #include <wtf/UnusedParam.h>
57 void initializeDates();
58 int equivalentYearForDST(int year
);
60 // Not really math related, but this is currently the only shared place to put these.
61 double parseES5DateFromNullTerminatedCharacters(const char* dateString
);
62 double parseDateFromNullTerminatedCharacters(const char* dateString
);
63 double timeClip(double);
65 inline double jsCurrentTime()
67 // JavaScript doesn't recognize fractions of a millisecond.
68 return floor(WTF::currentTimeMS());
71 const char * const weekdayName
[7] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
72 const char * const monthName
[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
74 const double hoursPerDay
= 24.0;
75 const double minutesPerHour
= 60.0;
76 const double secondsPerHour
= 60.0 * 60.0;
77 const double secondsPerMinute
= 60.0;
78 const double msPerSecond
= 1000.0;
79 const double msPerMinute
= 60.0 * 1000.0;
80 const double msPerHour
= 60.0 * 60.0 * 1000.0;
81 const double msPerDay
= 24.0 * 60.0 * 60.0 * 1000.0;
82 const double msPerMonth
= 2592000000.0;
84 // Returns the number of days from 1970-01-01 to the specified date.
85 double dateToDaysFrom1970(int year
, int month
, int day
);
86 int msToYear(double ms
);
87 int dayInYear(double ms
, int year
);
88 int monthFromDayInYear(int dayInYear
, bool leapYear
);
89 int dayInMonthFromDayInYear(int dayInYear
, bool leapYear
);
91 // Returns offset milliseconds for UTC and DST.
92 int32_t calculateUTCOffset();
93 double calculateDSTOffset(double ms
, double utcOffset
);
97 using WTF::adoptArrayPtr
;
98 using WTF::dateToDaysFrom1970
;
99 using WTF::dayInMonthFromDayInYear
;
100 using WTF::dayInYear
;
101 using WTF::minutesPerHour
;
102 using WTF::monthFromDayInYear
;
104 using WTF::msPerMinute
;
105 using WTF::msPerSecond
;
107 using WTF::secondsPerMinute
;
108 using WTF::parseDateFromNullTerminatedCharacters
;
109 using WTF::calculateUTCOffset
;
110 using WTF::calculateDSTOffset
;
115 struct GregorianDateTime
;
117 void msToGregorianDateTime(ExecState
*, double, bool outputIsUTC
, GregorianDateTime
&);
118 double gregorianDateTimeToMS(ExecState
*, const GregorianDateTime
&, double, bool inputIsUTC
);
119 double getUTCOffset(ExecState
*);
120 double parseDateFromNullTerminatedCharacters(ExecState
*, const char* dateString
);
122 // Intentionally overridding the default tm of the system.
123 // The members of tm differ on various operating systems.
124 struct GregorianDateTime
{
125 WTF_MAKE_NONCOPYABLE(GregorianDateTime
);
141 GregorianDateTime(ExecState
* exec
, const tm
& inTm
)
142 : second(inTm
.tm_sec
)
143 , minute(inTm
.tm_min
)
145 , weekDay(inTm
.tm_wday
)
146 , monthDay(inTm
.tm_mday
)
147 , yearDay(inTm
.tm_yday
)
150 , isDST(inTm
.tm_isdst
)
154 utcOffset
= static_cast<int>(inTm
.tm_gmtoff
);
156 utcOffset
= static_cast<int>(getUTCOffset(exec
) / WTF::msPerSecond
+ (isDST
? WTF::secondsPerHour
: 0));
160 int inZoneSize
= strlen(inTm
.tm_zone
) + 1;
161 timeZone
= adoptArrayPtr(new char[inZoneSize
]);
162 strncpy(timeZone
.get(), inTm
.tm_zone
, inZoneSize
);
171 memset(&ret
, 0, sizeof(ret
));
176 ret
.tm_wday
= weekDay
;
177 ret
.tm_mday
= monthDay
;
178 ret
.tm_yday
= yearDay
;
181 ret
.tm_isdst
= isDST
;
184 ret
.tm_gmtoff
= static_cast<long>(utcOffset
);
187 ret
.tm_zone
= timeZone
.get();
193 void copyFrom(const GregorianDateTime
& rhs
)
198 weekDay
= rhs
.weekDay
;
199 monthDay
= rhs
.monthDay
;
200 yearDay
= rhs
.yearDay
;
204 utcOffset
= rhs
.utcOffset
;
206 int inZoneSize
= strlen(rhs
.timeZone
.get()) + 1;
207 timeZone
= adoptArrayPtr(new char[inZoneSize
]);
208 strncpy(timeZone
.get(), rhs
.timeZone
.get(), inZoneSize
);
223 OwnArrayPtr
<char> timeZone
;
226 static inline int gmtoffset(const GregorianDateTime
& t
)