]> git.saurik.com Git - apple/javascriptcore.git/blame - wtf/DateMath.h
JavaScriptCore-903.tar.gz
[apple/javascriptcore.git] / wtf / DateMath.h
CommitLineData
b37bf2e1
A
1/*
2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
ba379fdc 4 * Copyright (C) 2009 Google Inc. All rights reserved.
14957cd0 5 * Copyright (C) 2010 Research In Motion Limited. All rights reserved.
b37bf2e1
A
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#ifndef DateMath_h
44#define DateMath_h
45
f9bf01c6 46#include <math.h>
14957cd0 47#include <stdint.h>
b37bf2e1 48#include <string.h>
f9bf01c6
A
49#include <time.h>
50#include <wtf/CurrentTime.h>
b37bf2e1 51#include <wtf/Noncopyable.h>
14957cd0
A
52#include <wtf/OwnArrayPtr.h>
53#include <wtf/PassOwnArrayPtr.h>
f9bf01c6 54#include <wtf/UnusedParam.h>
b37bf2e1 55
ba379fdc 56namespace WTF {
ba379fdc 57void initializeDates();
b37bf2e1 58int equivalentYearForDST(int year);
9dae56ea 59
14957cd0
A
60// Not really math related, but this is currently the only shared place to put these.
61double parseES5DateFromNullTerminatedCharacters(const char* dateString);
f9bf01c6 62double parseDateFromNullTerminatedCharacters(const char* dateString);
9dae56ea 63double timeClip(double);
b37bf2e1 64
f9bf01c6
A
65inline double jsCurrentTime()
66{
67 // JavaScript doesn't recognize fractions of a millisecond.
68 return floor(WTF::currentTimeMS());
69}
70
b37bf2e1
A
71const char * const weekdayName[7] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
72const char * const monthName[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
73
74const double hoursPerDay = 24.0;
75const double minutesPerHour = 60.0;
76const double secondsPerHour = 60.0 * 60.0;
77const double secondsPerMinute = 60.0;
78const double msPerSecond = 1000.0;
79const double msPerMinute = 60.0 * 1000.0;
80const double msPerHour = 60.0 * 60.0 * 1000.0;
81const double msPerDay = 24.0 * 60.0 * 60.0 * 1000.0;
f9bf01c6 82const double msPerMonth = 2592000000.0;
b37bf2e1 83
f9bf01c6
A
84// Returns the number of days from 1970-01-01 to the specified date.
85double dateToDaysFrom1970(int year, int month, int day);
86int msToYear(double ms);
87int dayInYear(double ms, int year);
88int monthFromDayInYear(int dayInYear, bool leapYear);
89int dayInMonthFromDayInYear(int dayInYear, bool leapYear);
90
14957cd0
A
91// Returns offset milliseconds for UTC and DST.
92int32_t calculateUTCOffset();
93double calculateDSTOffset(double ms, double utcOffset);
94
f9bf01c6
A
95} // namespace WTF
96
14957cd0 97using WTF::adoptArrayPtr;
f9bf01c6
A
98using WTF::dateToDaysFrom1970;
99using WTF::dayInMonthFromDayInYear;
100using WTF::dayInYear;
101using WTF::minutesPerHour;
102using WTF::monthFromDayInYear;
103using WTF::msPerDay;
14957cd0 104using WTF::msPerMinute;
f9bf01c6
A
105using WTF::msPerSecond;
106using WTF::msToYear;
107using WTF::secondsPerMinute;
14957cd0
A
108using WTF::parseDateFromNullTerminatedCharacters;
109using WTF::calculateUTCOffset;
110using WTF::calculateDSTOffset;
f9bf01c6
A
111
112#if USE(JSC)
113namespace JSC {
114class ExecState;
115struct GregorianDateTime;
116
117void msToGregorianDateTime(ExecState*, double, bool outputIsUTC, GregorianDateTime&);
118double gregorianDateTimeToMS(ExecState*, const GregorianDateTime&, double, bool inputIsUTC);
119double getUTCOffset(ExecState*);
120double parseDateFromNullTerminatedCharacters(ExecState*, const char* dateString);
121
122// Intentionally overridding the default tm of the system.
123// The members of tm differ on various operating systems.
14957cd0
A
124struct GregorianDateTime {
125 WTF_MAKE_NONCOPYABLE(GregorianDateTime);
126public:
b37bf2e1
A
127 GregorianDateTime()
128 : second(0)
129 , minute(0)
130 , hour(0)
131 , weekDay(0)
132 , monthDay(0)
133 , yearDay(0)
134 , month(0)
135 , year(0)
136 , isDST(0)
137 , utcOffset(0)
b37bf2e1 138 {
b37bf2e1 139 }
9dae56ea 140
f9bf01c6 141 GregorianDateTime(ExecState* exec, const tm& inTm)
b37bf2e1
A
142 : second(inTm.tm_sec)
143 , minute(inTm.tm_min)
144 , hour(inTm.tm_hour)
145 , weekDay(inTm.tm_wday)
146 , monthDay(inTm.tm_mday)
147 , yearDay(inTm.tm_yday)
148 , month(inTm.tm_mon)
149 , year(inTm.tm_year)
150 , isDST(inTm.tm_isdst)
151 {
f9bf01c6
A
152 UNUSED_PARAM(exec);
153#if HAVE(TM_GMTOFF)
b37bf2e1 154 utcOffset = static_cast<int>(inTm.tm_gmtoff);
f9bf01c6
A
155#else
156 utcOffset = static_cast<int>(getUTCOffset(exec) / WTF::msPerSecond + (isDST ? WTF::secondsPerHour : 0));
157#endif
b37bf2e1 158
f9bf01c6 159#if HAVE(TM_ZONE)
b37bf2e1 160 int inZoneSize = strlen(inTm.tm_zone) + 1;
14957cd0
A
161 timeZone = adoptArrayPtr(new char[inZoneSize]);
162 strncpy(timeZone.get(), inTm.tm_zone, inZoneSize);
b37bf2e1 163#else
14957cd0 164 timeZone = nullptr;
b37bf2e1
A
165#endif
166 }
167
168 operator tm() const
169 {
170 tm ret;
171 memset(&ret, 0, sizeof(ret));
172
173 ret.tm_sec = second;
174 ret.tm_min = minute;
175 ret.tm_hour = hour;
176 ret.tm_wday = weekDay;
177 ret.tm_mday = monthDay;
178 ret.tm_yday = yearDay;
179 ret.tm_mon = month;
180 ret.tm_year = year;
181 ret.tm_isdst = isDST;
182
f9bf01c6 183#if HAVE(TM_GMTOFF)
b37bf2e1 184 ret.tm_gmtoff = static_cast<long>(utcOffset);
f9bf01c6
A
185#endif
186#if HAVE(TM_ZONE)
14957cd0 187 ret.tm_zone = timeZone.get();
b37bf2e1
A
188#endif
189
190 return ret;
191 }
192
9dae56ea
A
193 void copyFrom(const GregorianDateTime& rhs)
194 {
195 second = rhs.second;
196 minute = rhs.minute;
197 hour = rhs.hour;
198 weekDay = rhs.weekDay;
199 monthDay = rhs.monthDay;
200 yearDay = rhs.yearDay;
201 month = rhs.month;
202 year = rhs.year;
203 isDST = rhs.isDST;
204 utcOffset = rhs.utcOffset;
205 if (rhs.timeZone) {
14957cd0
A
206 int inZoneSize = strlen(rhs.timeZone.get()) + 1;
207 timeZone = adoptArrayPtr(new char[inZoneSize]);
208 strncpy(timeZone.get(), rhs.timeZone.get(), inZoneSize);
9dae56ea 209 } else
14957cd0 210 timeZone = nullptr;
9dae56ea
A
211 }
212
b37bf2e1
A
213 int second;
214 int minute;
215 int hour;
216 int weekDay;
217 int monthDay;
218 int yearDay;
219 int month;
220 int year;
221 int isDST;
222 int utcOffset;
14957cd0 223 OwnArrayPtr<char> timeZone;
b37bf2e1
A
224};
225
9dae56ea
A
226static inline int gmtoffset(const GregorianDateTime& t)
227{
228 return t.utcOffset;
229}
f9bf01c6
A
230} // namespace JSC
231#endif // USE(JSC)
b37bf2e1
A
232
233#endif // DateMath_h