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 *
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
45 #include <time.h>
46 #include <string.h>
47 #include <wtf/Noncopyable.h>
48
49 namespace WTF {
50
51 struct GregorianDateTime;
52
53 void initializeDates();
54 void msToGregorianDateTime(double, bool outputIsUTC, GregorianDateTime&);
55 double gregorianDateTimeToMS(const GregorianDateTime&, double, bool inputIsUTC);
56 double getUTCOffset();
57 int equivalentYearForDST(int year);
58 double getCurrentUTCTime();
59 double getCurrentUTCTimeWithMicroseconds();
60 void getLocalTime(const time_t*, tm*);
61
62 // Not really math related, but this is currently the only shared place to put these.
63 double parseDateFromNullTerminatedCharacters(const char*);
64 double timeClip(double);
65
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" };
68
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
78 // Intentionally overridding the default tm of the system
79 // Tee members of tm differ on various operating systems.
80 struct GregorianDateTime : Noncopyable {
GregorianDateTimeGregorianDateTime81 GregorianDateTime()
82 : second(0)
83 , minute(0)
84 , hour(0)
85 , weekDay(0)
86 , monthDay(0)
87 , yearDay(0)
88 , month(0)
89 , year(0)
90 , isDST(0)
91 , utcOffset(0)
92 , timeZone(0)
93 {
94 }
95
~GregorianDateTimeGregorianDateTime96 ~GregorianDateTime()
97 {
98 delete [] timeZone;
99 }
100
GregorianDateTimeGregorianDateTime101 GregorianDateTime(const tm& inTm)
102 : second(inTm.tm_sec)
103 , minute(inTm.tm_min)
104 , hour(inTm.tm_hour)
105 , weekDay(inTm.tm_wday)
106 , monthDay(inTm.tm_mday)
107 , yearDay(inTm.tm_yday)
108 , month(inTm.tm_mon)
109 , year(inTm.tm_year)
110 , isDST(inTm.tm_isdst)
111 {
112 #if HAVE(TM_GMTOFF)
113 utcOffset = static_cast<int>(inTm.tm_gmtoff);
114 #else
115 utcOffset = static_cast<int>(getUTCOffset() / msPerSecond + (isDST ? secondsPerHour : 0));
116 #endif
117
118 #if HAVE(TM_ZONE)
119 int inZoneSize = strlen(inTm.tm_zone) + 1;
120 timeZone = new char[inZoneSize];
121 strncpy(timeZone, inTm.tm_zone, inZoneSize);
122 #else
123 timeZone = 0;
124 #endif
125 }
126
tmGregorianDateTime127 operator tm() const
128 {
129 tm ret;
130 memset(&ret, 0, sizeof(ret));
131
132 ret.tm_sec = second;
133 ret.tm_min = minute;
134 ret.tm_hour = hour;
135 ret.tm_wday = weekDay;
136 ret.tm_mday = monthDay;
137 ret.tm_yday = yearDay;
138 ret.tm_mon = month;
139 ret.tm_year = year;
140 ret.tm_isdst = isDST;
141
142 #if HAVE(TM_GMTOFF)
143 ret.tm_gmtoff = static_cast<long>(utcOffset);
144 #endif
145 #if HAVE(TM_ZONE)
146 ret.tm_zone = timeZone;
147 #endif
148
149 return ret;
150 }
151
copyFromGregorianDateTime152 void copyFrom(const GregorianDateTime& rhs)
153 {
154 second = rhs.second;
155 minute = rhs.minute;
156 hour = rhs.hour;
157 weekDay = rhs.weekDay;
158 monthDay = rhs.monthDay;
159 yearDay = rhs.yearDay;
160 month = rhs.month;
161 year = rhs.year;
162 isDST = rhs.isDST;
163 utcOffset = rhs.utcOffset;
164 if (rhs.timeZone) {
165 int inZoneSize = strlen(rhs.timeZone) + 1;
166 timeZone = new char[inZoneSize];
167 strncpy(timeZone, rhs.timeZone, inZoneSize);
168 } else
169 timeZone = 0;
170 }
171
172 int second;
173 int minute;
174 int hour;
175 int weekDay;
176 int monthDay;
177 int yearDay;
178 int month;
179 int year;
180 int isDST;
181 int utcOffset;
182 char* timeZone;
183 };
184
gmtoffset(const GregorianDateTime & t)185 static inline int gmtoffset(const GregorianDateTime& t)
186 {
187 return t.utcOffset;
188 }
189
190 } // namespace WTF
191
192 #endif // DateMath_h
193