2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
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>
25 #include "TimeUtilConverter.h"
27 #include "JSTimeDuration.h"
30 using namespace WrtDeviceApis;
35 TimeUtilConverter::TimeUtilConverter(JSContextRef context)
36 : WrtDeviceApis::CommonsJavaScript::Converter(context) {
39 TimeUtilConverter::~TimeUtilConverter() {
42 short TimeUtilConverter::toShort(const JSValueRef& arg)
44 double tmp = toNumber_(arg);
45 return (isNan(tmp) ? 0 : static_cast<short>(tmp));
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.");
53 JSObjectRef obj = toJSObjectRef(arg);
54 return getPropertiesInTZDate(obj);
58 TZDateProperties TimeUtilConverter::getPropertiesInTZDate(JSObjectRef arg) {
59 LoggerD("TZDate object=" << arg);
61 LoggerE("Object is null");
62 ThrowMsg(Commons::ConversionException, "Private object not initialized");
64 TZDatePrivObject* privateObject = static_cast<TZDatePrivObject*>(JSObjectGetPrivate(arg));
66 LoggerE("Private object is not set.");
67 ThrowMsg(Commons::ConversionException, "Private object not initialized");
70 TZDatePtr tzDate = privateObject->getObject();
71 TZDateProperties result;
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);
84 JSObjectRef TimeUtilConverter::makeDurationObject(const DurationProperties &duration) {
85 return JSTimeDuration::createJSObject(m_context, duration);
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;
98 return makeDurationObject(duration);
101 DurationProperties TimeUtilConverter::getDurationPropertis(JSValueRef value) {
102 DurationProperties duration;
103 duration.length = getDurationLength(value);
104 duration.unit = getDurationUnit(value);
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.");
113 JSObjectRef obj = toJSObjectRef(value);
114 return getDurationLength(obj);
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");
124 DurationPropertiesPtr duration = privateObject->getObject();
126 return duration->length;
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.");
134 JSObjectRef obj = toJSObjectRef(value);
135 return getDurationUnit(obj);
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");
145 DurationPropertiesPtr duration = privateObject->getObject();
147 return duration->unit;
151 short TimeUtilConverter::toDurationUnit(std::string strUnit) {
152 if (!strUnit.compare("DAYS"))
154 else if (!strUnit.compare("SECS"))
156 else if (!strUnit.compare("MINS"))
158 else if (!strUnit.compare("HOURS"))
160 else if (!strUnit.compare("MSECS"))
166 std::string TimeUtilConverter::toDurationUnitString(short unit) {
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");
187 if (duration.unit == unit)
188 return duration.length;
190 long long result = duration.length;
194 result = result * 1000;
195 if (duration.unit == SECONDS_UNIT)
197 // intentional fall-through
200 result = result * 60;
201 if (duration.unit == MINUTES_UNIT)
203 // intentional fall-through
206 result = result * 60;
207 if (duration.unit == HOURS_UNIT)
209 // intentional fall-through
212 result = result * 24;
214 // intentional fall-through
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.");
224 JSObjectRef obj = toJSObjectRef(arg);
226 return getTimeInMilliseconds(obj);
229 double TimeUtilConverter::getTimeInMilliseconds(JSObjectRef arg) {
231 LoggerE("Object is null");
232 ThrowMsg(Commons::ConversionException, "Private object not initialized");
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");
241 TZDatePtr tzDate = privateObject->getObject();
243 return tzDate->getTime();
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.");
251 JSObjectRef obj = toJSObjectRef(arg);
253 return toTZDateTime(obj);
256 tm TimeUtilConverter::toTZDateTime(JSObjectRef arg) {
258 LoggerE("Object is null");
259 ThrowMsg(Commons::ConversionException, "Private object not initialized");
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");
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");
273 TZDateProperties utcProperties = utcTzDate->makeProperties();
275 memset(&utcTm, 0, sizeof(utcTm));
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;
287 JSObjectRef TimeUtilConverter::toJSValueRefTZDate(const double milliseconds, const std::string &timezone) {
288 return JSTZDate::createJSObject(m_context, milliseconds, timezone);