tizen beta release
[framework/web/webkit-efl.git] / Source / JavaScriptCore / runtime / JSDateMath.h
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  * Copyright (C) 2010 Research In Motion Limited. All rights reserved.
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 JSDateMath_h
44 #define JSDateMath_h
45
46 #include <wtf/DateMath.h>
47
48 namespace JSC {
49
50 class ExecState;
51 struct GregorianDateTime;
52
53 void msToGregorianDateTime(ExecState*, double, bool outputIsUTC, GregorianDateTime&);
54 double gregorianDateTimeToMS(ExecState*, const GregorianDateTime&, double, bool inputIsUTC);
55 double getUTCOffset(ExecState*);
56 double parseDateFromNullTerminatedCharacters(ExecState*, const char* dateString);
57
58 // Intentionally overridding the default tm of the system.
59 // The members of tm differ on various operating systems.
60 struct GregorianDateTime {
61     WTF_MAKE_NONCOPYABLE(GregorianDateTime);
62 public:
63     GregorianDateTime()
64         : second(0)
65         , minute(0)
66         , hour(0)
67         , weekDay(0)
68         , monthDay(0)
69         , yearDay(0)
70         , month(0)
71         , year(0)
72         , isDST(0)
73         , utcOffset(0)
74     {
75     }
76
77     GregorianDateTime(ExecState* exec, const tm& inTm)
78         : second(inTm.tm_sec)
79         , minute(inTm.tm_min)
80         , hour(inTm.tm_hour)
81         , weekDay(inTm.tm_wday)
82         , monthDay(inTm.tm_mday)
83         , yearDay(inTm.tm_yday)
84         , month(inTm.tm_mon)
85         , year(inTm.tm_year)
86         , isDST(inTm.tm_isdst)
87     {
88         UNUSED_PARAM(exec);
89 #if HAVE(TM_GMTOFF)
90         utcOffset = static_cast<int>(inTm.tm_gmtoff);
91 #else
92         utcOffset = static_cast<int>(getUTCOffset(exec) / WTF::msPerSecond + (isDST ? WTF::secondsPerHour : 0));
93 #endif
94
95 #if HAVE(TM_ZONE)
96         int inZoneSize = strlen(inTm.tm_zone) + 1;
97         timeZone = adoptArrayPtr(new char[inZoneSize]);
98         strncpy(timeZone.get(), inTm.tm_zone, inZoneSize);
99 #else
100         timeZone = nullptr;
101 #endif
102     }
103
104     operator tm() const
105     {
106         tm ret;
107         memset(&ret, 0, sizeof(ret));
108
109         ret.tm_sec   =  second;
110         ret.tm_min   =  minute;
111         ret.tm_hour  =  hour;
112         ret.tm_wday  =  weekDay;
113         ret.tm_mday  =  monthDay;
114         ret.tm_yday  =  yearDay;
115         ret.tm_mon   =  month;
116         ret.tm_year  =  year;
117         ret.tm_isdst =  isDST;
118
119 #if HAVE(TM_GMTOFF)
120         ret.tm_gmtoff = static_cast<long>(utcOffset);
121 #endif
122 #if HAVE(TM_ZONE)
123         ret.tm_zone = timeZone.get();
124 #endif
125
126         return ret;
127     }
128
129     void copyFrom(const GregorianDateTime& rhs)
130     {
131         second = rhs.second;
132         minute = rhs.minute;
133         hour = rhs.hour;
134         weekDay = rhs.weekDay;
135         monthDay = rhs.monthDay;
136         yearDay = rhs.yearDay;
137         month = rhs.month;
138         year = rhs.year;
139         isDST = rhs.isDST;
140         utcOffset = rhs.utcOffset;
141         if (rhs.timeZone) {
142             int inZoneSize = strlen(rhs.timeZone.get()) + 1;
143             timeZone = adoptArrayPtr(new char[inZoneSize]);
144             strncpy(timeZone.get(), rhs.timeZone.get(), inZoneSize);
145         } else
146             timeZone = nullptr;
147     }
148
149     int second;
150     int minute;
151     int hour;
152     int weekDay;
153     int monthDay;
154     int yearDay;
155     int month;
156     int year;
157     int isDST;
158     int utcOffset;
159     OwnArrayPtr<char> timeZone;
160 };
161
162 static inline int gmtoffset(const GregorianDateTime& t)
163 {
164     return t.utcOffset;
165 }
166
167 } // namespace JSC
168
169 #endif // JSDateMath_h