wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / TimeUtil / TimeUtilConverter.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <Commons/Exception.h>
19 #include <CommonsJavaScript/ScopedJSStringRef.h>
20 #include <CommonsJavaScript/PrivateObject.h>
21 #include <CommonsJavaScript/Validator.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <Commons/RegexUtils.h>
24
25 #include "TimeUtilConverter.h"
26 #include "JSTZDate.h"
27 #include "JSTimeDuration.h"
28 #include <Logger.h>
29
30 using namespace WrtDeviceApis;
31
32 namespace DeviceAPI {
33 namespace Time {
34
35 TimeUtilConverter::TimeUtilConverter(JSContextRef context)
36 : WrtDeviceApis::CommonsJavaScript::Converter(context) {
37 }
38
39 TimeUtilConverter::~TimeUtilConverter() {
40 }
41
42 short TimeUtilConverter::toShort(const JSValueRef& arg)
43 {
44     double tmp = toNumber_(arg);
45     return (isNan(tmp) ? 0 : static_cast<short>(tmp));
46 }
47
48 TZDateProperties TimeUtilConverter::getPropertiesInTZDate(JSValueRef arg) {
49         if (JSValueIsNull(m_context, arg) || JSValueIsUndefined(m_context, arg) || !JSValueIsObjectOfClass(m_context, arg, JSTZDate::getClassRef())) {
50                 ThrowMsg(Commons::ConversionException,
51                          "Message is JS null or JS undefined.");
52         }
53         JSObjectRef obj = toJSObjectRef(arg);
54         return getPropertiesInTZDate(obj);
55 }
56
57
58 TZDateProperties TimeUtilConverter::getPropertiesInTZDate(JSObjectRef arg) {
59         LoggerD("TZDate object=" << arg);
60         if (!arg) {
61                 LoggerE("Object is null");
62                 ThrowMsg(Commons::ConversionException, "Private object not initialized");
63         }
64         TZDatePrivObject* privateObject = static_cast<TZDatePrivObject*>(JSObjectGetPrivate(arg));
65         if (!privateObject) {
66                 LoggerE("Private object is not set.");
67                 ThrowMsg(Commons::ConversionException, "Private object not initialized");
68         }
69         
70         TZDatePtr tzDate = privateObject->getObject();
71         TZDateProperties result;
72
73         result.timezone = tzDate->getTimezone();
74         result.year = tzDate->get(TZDate::TZDATE_YEAR);
75         result.month = tzDate->get(TZDate::TZDATE_MONTH);
76         result.day= tzDate->get(TZDate::TZDATE_DATE);
77         result.hours= tzDate->get(TZDate::TZDATE_HOUR_OF_DAY);
78         result.minutes= tzDate->get(TZDate::TZDATE_MINUTE);
79         result.seconds= tzDate->get(TZDate::TZDATE_SECOND);
80         result.milliseconds = tzDate->get(TZDate::TZDATE_MILLISECOND);
81         return result;
82 }
83
84 JSObjectRef TimeUtilConverter::makeDurationObject(const DurationProperties &duration) {
85         return JSTimeDuration::createJSObject(m_context, duration);
86 }
87
88 JSObjectRef TimeUtilConverter::makeMillisecondDurationObject(const long long length) {
89         DurationProperties duration;
90         duration.length = length;
91         duration.unit = MSECS_UNIT;
92         const long long dayToMsecs = 1000 * 60 * 60 * 24;
93         if ((length % dayToMsecs) == 0) {
94                 duration.length = length / dayToMsecs;
95                 duration.unit = DAYS_UNIT;
96         }
97
98         return makeDurationObject(duration);
99 }
100
101 DurationProperties TimeUtilConverter::getDurationPropertis(JSValueRef value) {
102         DurationProperties duration;
103         duration.length = getDurationLength(value);
104         duration.unit = getDurationUnit(value);
105         return duration;
106 }
107
108 long long TimeUtilConverter::getDurationLength(JSValueRef value) {
109         if (JSValueIsNull(m_context, value) || JSValueIsUndefined(m_context, value) || !JSValueIsObjectOfClass(m_context, value, JSTimeDuration::getClassRef())) {
110                 ThrowMsg(Commons::ConversionException,
111                          "Duration is JS null or JS undefined.");
112         }
113         JSObjectRef obj = toJSObjectRef(value);
114         return getDurationLength(obj);
115 }
116
117 long long TimeUtilConverter::getDurationLength(JSObjectRef object) {
118         TimeDurationPrivObject* privateObject = static_cast<TimeDurationPrivObject*>(JSObjectGetPrivate(object));
119         if (!privateObject) {
120                 LoggerE("Private object is not set.");
121                 ThrowMsg(Commons::ConversionException, "Private object not initialized");
122         }
123
124         DurationPropertiesPtr duration = privateObject->getObject();
125
126         return duration->length;
127 }
128
129 short TimeUtilConverter::getDurationUnit(JSValueRef value) {
130         if (JSValueIsNull(m_context, value) || JSValueIsUndefined(m_context, value) || !JSValueIsObjectOfClass(m_context, value, JSTimeDuration::getClassRef())) {
131                 ThrowMsg(Commons::ConversionException,
132                          "Duration is JS null or JS undefined.");
133         }
134         JSObjectRef obj = toJSObjectRef(value);
135         return getDurationUnit(obj);
136 }
137
138 short TimeUtilConverter::getDurationUnit(JSObjectRef object) {
139         TimeDurationPrivObject* privateObject = static_cast<TimeDurationPrivObject*>(JSObjectGetPrivate(object));
140         if (!privateObject) {
141                 LoggerE("Private object is not set.");
142                 ThrowMsg(Commons::ConversionException, "Private object not initialized");
143         }
144
145         DurationPropertiesPtr duration = privateObject->getObject();
146
147         return duration->unit;
148
149 }
150
151 short TimeUtilConverter::toDurationUnit(std::string strUnit) {
152         if (!strUnit.compare("DAYS"))
153                 return DAYS_UNIT;
154         else if (!strUnit.compare("SECS"))
155                 return SECONDS_UNIT;
156         else if (!strUnit.compare("MINS"))
157                 return MINUTES_UNIT;
158         else if (!strUnit.compare("HOURS"))
159                 return HOURS_UNIT;
160         else if (!strUnit.compare("MSECS"))
161                 return  MSECS_UNIT;
162         else
163                 return 0xff;
164 }
165
166 std::string TimeUtilConverter::toDurationUnitString(short unit) {
167         switch (unit) {
168                 case DAYS_UNIT:
169                         return "DAYS";
170                 case SECONDS_UNIT:
171                         return "SECS";
172                 case MINUTES_UNIT:
173                         return "MINS";
174                 case HOURS_UNIT:
175                         return "HOURS";
176                 case MSECS_UNIT:
177                         return "MSECS";
178                 default:
179                         return "";
180         }
181 }
182
183 long long TimeUtilConverter::convertDurationLength(DurationProperties duration, short unit) {
184         if (duration.unit < unit)
185                 ThrowMsg(Commons::ConversionException, "Unit is larger thatn duration's unit");
186
187         if (duration.unit == unit)
188                 return duration.length;
189
190         long long result = duration.length;
191
192         switch(unit) {
193                 case MSECS_UNIT:
194                         result = result * 1000;
195                         if (duration.unit == SECONDS_UNIT)
196                                 return result;
197                         // intentional fall-through
198
199                 case SECONDS_UNIT:
200                         result = result * 60;
201                         if (duration.unit == MINUTES_UNIT)
202                                 return result;
203                         // intentional fall-through
204
205                 case MINUTES_UNIT:
206                         result = result * 60;
207                         if (duration.unit == HOURS_UNIT)
208                                 return result;
209                         // intentional fall-through
210
211                 case HOURS_UNIT:
212                         result = result * 24;
213                         return result;
214                         // intentional fall-through
215         }
216     return result;
217 }
218
219 double TimeUtilConverter::getTimeInMilliseconds(JSValueRef arg) {
220         if (JSValueIsNull(m_context, arg) || JSValueIsUndefined(m_context, arg) || !JSValueIsObjectOfClass(m_context, arg, JSTZDate::getClassRef())) {
221                 ThrowMsg(Commons::ConversionException,
222                          "JSValueRef is JS null or JS undefined.");
223         }
224         JSObjectRef obj = toJSObjectRef(arg);
225
226         return getTimeInMilliseconds(obj);
227 }
228
229 double TimeUtilConverter::getTimeInMilliseconds(JSObjectRef arg) {
230         if (!arg) {
231                 LoggerE("Object is null");
232                 ThrowMsg(Commons::ConversionException, "Private object not initialized");
233         }
234
235         TZDatePrivObject* privateObject = static_cast<TZDatePrivObject*>(JSObjectGetPrivate(arg));
236         if (!privateObject) {
237                 LoggerE("Private object is not set.");
238                 ThrowMsg(Commons::ConversionException, "Private object not initialized");
239         }
240
241         TZDatePtr tzDate = privateObject->getObject();
242
243         return tzDate->getTime();
244 }
245
246 tm TimeUtilConverter::toTZDateTime(JSValueRef arg) {
247         if (JSValueIsNull(m_context, arg) || JSValueIsUndefined(m_context, arg) || !JSValueIsObjectOfClass(m_context, arg, JSTZDate::getClassRef())) {
248                 ThrowMsg(Commons::ConversionException,
249                          "JSValueRef is JS null or JS undefined.");
250         }
251         JSObjectRef obj = toJSObjectRef(arg);
252
253         return toTZDateTime(obj);
254 }
255
256 tm TimeUtilConverter::toTZDateTime(JSObjectRef arg) {
257         if (!arg) {
258                 LoggerE("Object is null");
259                 ThrowMsg(Commons::ConversionException, "Private object not initialized");
260         }
261
262         TZDatePrivObject* privateObject = static_cast<TZDatePrivObject*>(JSObjectGetPrivate(arg));
263         if (!privateObject) {
264                 LoggerE("Private object is not set.");
265                 ThrowMsg(Commons::ConversionException, "Private object not initialized");
266         }
267
268         TZDatePtr tzDate = privateObject->getObject();
269         TZDatePtr utcTzDate(new TZDate(tzDate->getUTCTimezoneName()));
270         if (!utcTzDate->setTime(tzDate->getTime()))
271                 ThrowMsg(Commons::ConversionException, "Can not set UTC Time");
272
273         TZDateProperties utcProperties = utcTzDate->makeProperties();
274         struct tm  utcTm;
275         memset(&utcTm, 0, sizeof(utcTm));
276
277         utcTm.tm_year = utcProperties.year - 1900;
278         utcTm.tm_mon = utcProperties.month;
279         utcTm.tm_mday= utcProperties.day;
280         utcTm.tm_hour= utcProperties.hours;
281         utcTm.tm_min=utcProperties.minutes;
282         utcTm.tm_sec= utcProperties.seconds;
283         utcTm.tm_isdst = 0;
284         return utcTm;
285 }
286
287 JSObjectRef TimeUtilConverter::toJSValueRefTZDate(const double milliseconds, const std::string &timezone) {
288         return JSTZDate::createJSObject(m_context, milliseconds, timezone);
289 }
290
291 }
292 }