]>
git.saurik.com Git - apple/javascriptcore.git/blob - wtf/DateMath.h
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.
6 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
18 * The Original Code is Mozilla Communicator client code, released
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.
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.
48 #include <wtf/CurrentTime.h>
49 #include <wtf/Noncopyable.h>
50 #include <wtf/UnusedParam.h>
53 void initializeDates();
54 int equivalentYearForDST(int year
);
56 // Not really math related, but this is currently the only shared place to put these.
57 double parseDateFromNullTerminatedCharacters(const char* dateString
);
58 double timeClip(double);
60 inline double jsCurrentTime()
62 // JavaScript doesn't recognize fractions of a millisecond.
63 return floor(WTF::currentTimeMS());
66 const char * const weekdayName
[7] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
67 const char * const monthName
[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
69 const double hoursPerDay
= 24.0;
70 const double minutesPerHour
= 60.0;
71 const double secondsPerHour
= 60.0 * 60.0;
72 const double secondsPerMinute
= 60.0;
73 const double msPerSecond
= 1000.0;
74 const double msPerMinute
= 60.0 * 1000.0;
75 const double msPerHour
= 60.0 * 60.0 * 1000.0;
76 const double msPerDay
= 24.0 * 60.0 * 60.0 * 1000.0;
77 const double msPerMonth
= 2592000000.0;
79 // Returns the number of days from 1970-01-01 to the specified date.
80 double dateToDaysFrom1970(int year
, int month
, int day
);
81 int msToYear(double ms
);
82 int dayInYear(double ms
, int year
);
83 int monthFromDayInYear(int dayInYear
, bool leapYear
);
84 int dayInMonthFromDayInYear(int dayInYear
, bool leapYear
);
88 using WTF::dateToDaysFrom1970
;
89 using WTF::dayInMonthFromDayInYear
;
91 using WTF::minutesPerHour
;
92 using WTF::monthFromDayInYear
;
94 using WTF::msPerSecond
;
96 using WTF::secondsPerMinute
;
101 struct GregorianDateTime
;
103 void msToGregorianDateTime(ExecState
*, double, bool outputIsUTC
, GregorianDateTime
&);
104 double gregorianDateTimeToMS(ExecState
*, const GregorianDateTime
&, double, bool inputIsUTC
);
105 double getUTCOffset(ExecState
*);
106 double parseDateFromNullTerminatedCharacters(ExecState
*, const char* dateString
);
108 // Intentionally overridding the default tm of the system.
109 // The members of tm differ on various operating systems.
110 struct GregorianDateTime
: Noncopyable
{
131 GregorianDateTime(ExecState
* exec
, const tm
& inTm
)
132 : second(inTm
.tm_sec
)
133 , minute(inTm
.tm_min
)
135 , weekDay(inTm
.tm_wday
)
136 , monthDay(inTm
.tm_mday
)
137 , yearDay(inTm
.tm_yday
)
140 , isDST(inTm
.tm_isdst
)
144 utcOffset
= static_cast<int>(inTm
.tm_gmtoff
);
146 utcOffset
= static_cast<int>(getUTCOffset(exec
) / WTF::msPerSecond
+ (isDST
? WTF::secondsPerHour
: 0));
150 int inZoneSize
= strlen(inTm
.tm_zone
) + 1;
151 timeZone
= new char[inZoneSize
];
152 strncpy(timeZone
, inTm
.tm_zone
, inZoneSize
);
161 memset(&ret
, 0, sizeof(ret
));
166 ret
.tm_wday
= weekDay
;
167 ret
.tm_mday
= monthDay
;
168 ret
.tm_yday
= yearDay
;
171 ret
.tm_isdst
= isDST
;
174 ret
.tm_gmtoff
= static_cast<long>(utcOffset
);
177 ret
.tm_zone
= timeZone
;
183 void copyFrom(const GregorianDateTime
& rhs
)
188 weekDay
= rhs
.weekDay
;
189 monthDay
= rhs
.monthDay
;
190 yearDay
= rhs
.yearDay
;
194 utcOffset
= rhs
.utcOffset
;
196 int inZoneSize
= strlen(rhs
.timeZone
) + 1;
197 timeZone
= new char[inZoneSize
];
198 strncpy(timeZone
, rhs
.timeZone
, inZoneSize
);
216 static inline int gmtoffset(const GregorianDateTime
& t
)