]> git.saurik.com Git - apple/javascriptcore.git/blame - wtf/DateMath.h
JavaScriptCore-621.1.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.
b37bf2e1
A
5 *
6 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 *
8 * The contents of this file are subject to the Mozilla Public License Version
9 * 1.1 (the "License"); you may not use this file except in compliance with
10 * the License. You may obtain a copy of the License at
11 * http://www.mozilla.org/MPL/
12 *
13 * Software distributed under the License is distributed on an "AS IS" basis,
14 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
15 * for the specific language governing rights and limitations under the
16 * License.
17 *
18 * The Original Code is Mozilla Communicator client code, released
19 * March 31, 1998.
20 *
21 * The Initial Developer of the Original Code is
22 * Netscape Communications Corporation.
23 * Portions created by the Initial Developer are Copyright (C) 1998
24 * the Initial Developer. All Rights Reserved.
25 *
26 * Contributor(s):
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * either of the GNU General Public License Version 2 or later (the "GPL"),
30 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 * in which case the provisions of the GPL or the LGPL are applicable instead
32 * of those above. If you wish to allow use of your version of this file only
33 * under the terms of either the GPL or the LGPL, and not to allow others to
34 * use your version of this file under the terms of the MPL, indicate your
35 * decision by deleting the provisions above and replace them with the notice
36 * and other provisions required by the GPL or the LGPL. If you do not delete
37 * the provisions above, a recipient may use your version of this file under
38 * the terms of any one of the MPL, the GPL or the LGPL.
39 *
40 */
41
42#ifndef DateMath_h
43#define DateMath_h
44
f9bf01c6 45#include <math.h>
b37bf2e1 46#include <string.h>
f9bf01c6
A
47#include <time.h>
48#include <wtf/CurrentTime.h>
b37bf2e1 49#include <wtf/Noncopyable.h>
f9bf01c6 50#include <wtf/UnusedParam.h>
b37bf2e1 51
ba379fdc 52namespace WTF {
ba379fdc 53void initializeDates();
b37bf2e1 54int equivalentYearForDST(int year);
9dae56ea
A
55
56// Not really math related, but this is currently the only shared place to put these.
f9bf01c6 57double parseDateFromNullTerminatedCharacters(const char* dateString);
9dae56ea 58double timeClip(double);
b37bf2e1 59
f9bf01c6
A
60inline double jsCurrentTime()
61{
62 // JavaScript doesn't recognize fractions of a millisecond.
63 return floor(WTF::currentTimeMS());
64}
65
b37bf2e1
A
66const char * const weekdayName[7] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
67const char * const monthName[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
68
69const double hoursPerDay = 24.0;
70const double minutesPerHour = 60.0;
71const double secondsPerHour = 60.0 * 60.0;
72const double secondsPerMinute = 60.0;
73const double msPerSecond = 1000.0;
74const double msPerMinute = 60.0 * 1000.0;
75const double msPerHour = 60.0 * 60.0 * 1000.0;
76const double msPerDay = 24.0 * 60.0 * 60.0 * 1000.0;
f9bf01c6 77const double msPerMonth = 2592000000.0;
b37bf2e1 78
f9bf01c6
A
79// Returns the number of days from 1970-01-01 to the specified date.
80double dateToDaysFrom1970(int year, int month, int day);
81int msToYear(double ms);
82int dayInYear(double ms, int year);
83int monthFromDayInYear(int dayInYear, bool leapYear);
84int dayInMonthFromDayInYear(int dayInYear, bool leapYear);
85
86} // namespace WTF
87
88using WTF::dateToDaysFrom1970;
89using WTF::dayInMonthFromDayInYear;
90using WTF::dayInYear;
91using WTF::minutesPerHour;
92using WTF::monthFromDayInYear;
93using WTF::msPerDay;
94using WTF::msPerSecond;
95using WTF::msToYear;
96using WTF::secondsPerMinute;
97
98#if USE(JSC)
99namespace JSC {
100class ExecState;
101struct GregorianDateTime;
102
103void msToGregorianDateTime(ExecState*, double, bool outputIsUTC, GregorianDateTime&);
104double gregorianDateTimeToMS(ExecState*, const GregorianDateTime&, double, bool inputIsUTC);
105double getUTCOffset(ExecState*);
106double parseDateFromNullTerminatedCharacters(ExecState*, const char* dateString);
107
108// Intentionally overridding the default tm of the system.
109// The members of tm differ on various operating systems.
b37bf2e1
A
110struct GregorianDateTime : Noncopyable {
111 GregorianDateTime()
112 : second(0)
113 , minute(0)
114 , hour(0)
115 , weekDay(0)
116 , monthDay(0)
117 , yearDay(0)
118 , month(0)
119 , year(0)
120 , isDST(0)
121 , utcOffset(0)
122 , timeZone(0)
123 {
124 }
125
126 ~GregorianDateTime()
127 {
128 delete [] timeZone;
129 }
9dae56ea 130
f9bf01c6 131 GregorianDateTime(ExecState* exec, const tm& inTm)
b37bf2e1
A
132 : second(inTm.tm_sec)
133 , minute(inTm.tm_min)
134 , hour(inTm.tm_hour)
135 , weekDay(inTm.tm_wday)
136 , monthDay(inTm.tm_mday)
137 , yearDay(inTm.tm_yday)
138 , month(inTm.tm_mon)
139 , year(inTm.tm_year)
140 , isDST(inTm.tm_isdst)
141 {
f9bf01c6
A
142 UNUSED_PARAM(exec);
143#if HAVE(TM_GMTOFF)
b37bf2e1 144 utcOffset = static_cast<int>(inTm.tm_gmtoff);
f9bf01c6
A
145#else
146 utcOffset = static_cast<int>(getUTCOffset(exec) / WTF::msPerSecond + (isDST ? WTF::secondsPerHour : 0));
147#endif
b37bf2e1 148
f9bf01c6 149#if HAVE(TM_ZONE)
b37bf2e1
A
150 int inZoneSize = strlen(inTm.tm_zone) + 1;
151 timeZone = new char[inZoneSize];
152 strncpy(timeZone, inTm.tm_zone, inZoneSize);
153#else
b37bf2e1
A
154 timeZone = 0;
155#endif
156 }
157
158 operator tm() const
159 {
160 tm ret;
161 memset(&ret, 0, sizeof(ret));
162
163 ret.tm_sec = second;
164 ret.tm_min = minute;
165 ret.tm_hour = hour;
166 ret.tm_wday = weekDay;
167 ret.tm_mday = monthDay;
168 ret.tm_yday = yearDay;
169 ret.tm_mon = month;
170 ret.tm_year = year;
171 ret.tm_isdst = isDST;
172
f9bf01c6 173#if HAVE(TM_GMTOFF)
b37bf2e1 174 ret.tm_gmtoff = static_cast<long>(utcOffset);
f9bf01c6
A
175#endif
176#if HAVE(TM_ZONE)
b37bf2e1
A
177 ret.tm_zone = timeZone;
178#endif
179
180 return ret;
181 }
182
9dae56ea
A
183 void copyFrom(const GregorianDateTime& rhs)
184 {
185 second = rhs.second;
186 minute = rhs.minute;
187 hour = rhs.hour;
188 weekDay = rhs.weekDay;
189 monthDay = rhs.monthDay;
190 yearDay = rhs.yearDay;
191 month = rhs.month;
192 year = rhs.year;
193 isDST = rhs.isDST;
194 utcOffset = rhs.utcOffset;
195 if (rhs.timeZone) {
196 int inZoneSize = strlen(rhs.timeZone) + 1;
197 timeZone = new char[inZoneSize];
198 strncpy(timeZone, rhs.timeZone, inZoneSize);
199 } else
200 timeZone = 0;
201 }
202
b37bf2e1
A
203 int second;
204 int minute;
205 int hour;
206 int weekDay;
207 int monthDay;
208 int yearDay;
209 int month;
210 int year;
211 int isDST;
212 int utcOffset;
213 char* timeZone;
214};
215
9dae56ea
A
216static inline int gmtoffset(const GregorianDateTime& t)
217{
218 return t.utcOffset;
219}
f9bf01c6
A
220} // namespace JSC
221#endif // USE(JSC)
b37bf2e1
A
222
223#endif // DateMath_h