diff options
Diffstat (limited to 'src/standards/Tizen/TimeUtil/TimeUtilConverter.cpp')
-rwxr-xr-x | src/standards/Tizen/TimeUtil/TimeUtilConverter.cpp | 240 |
1 files changed, 240 insertions, 0 deletions
diff --git a/src/standards/Tizen/TimeUtil/TimeUtilConverter.cpp b/src/standards/Tizen/TimeUtil/TimeUtilConverter.cpp new file mode 100755 index 0000000..adf60ad --- /dev/null +++ b/src/standards/Tizen/TimeUtil/TimeUtilConverter.cpp @@ -0,0 +1,240 @@ +/* + * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + + + +#include <dpl/log.h> +#include <Commons/Exception.h> +#include <CommonsJavaScript/ScopedJSStringRef.h> +#include <CommonsJavaScript/PrivateObject.h> +#include <CommonsJavaScript/Validator.h> +#include <CommonsJavaScript/JSUtils.h> +#include <Commons/RegexUtils.h> + +#include "TimeUtilConverter.h" + +#include "JSTZDate.h" + +using namespace TizenApis::Api::TimeUtil; +using namespace WrtDeviceApis; + +namespace TizenApis { +namespace Tizen1_0 { + +TimeUtilConverter::TimeUtilConverter(JSContextRef context) +: WrtDeviceApis::CommonsJavaScript::Converter(context) { +} + +TimeUtilConverter::~TimeUtilConverter() { +} + +short TimeUtilConverter::toShort(const JSValueRef& arg) +{ + double tmp = toNumber_(arg); + return (isNan(tmp) ? 0 : static_cast<short>(tmp)); +} + +long TimeUtilConverter::FromJSValueReftolong(const JSValueRef& arg) { + if (!(JSValueIsNull(m_context, arg) || JSValueIsUndefined(m_context, arg)) + && (JSValueIsNumber(m_context, arg))) + return toLong(arg); + else + ThrowMsg(Commons::InvalidArgumentException, + "JSValueRef is JS null or JS undefined."); +} + +TZDateProperties TimeUtilConverter::getPropertiesInTZDate(JSValueRef arg) { + if (JSValueIsNull(m_context, arg) || JSValueIsUndefined(m_context, arg)) { + ThrowMsg(Commons::InvalidArgumentException, + "Message is JS null or JS undefined."); + } + JSObjectRef obj = toJSObjectRef(arg); + return getPropertiesInTZDate(obj); +} + + +TZDateProperties TimeUtilConverter::getPropertiesInTZDate(JSObjectRef arg) { + LogDebug("TZDate object=" << arg); + if (!arg) { + LogError("Object is null"); + ThrowMsg(Commons::NullPointerException, "Private object not initialized"); + } + TZDatePrivObject* privateObject = static_cast<TZDatePrivObject*>(JSObjectGetPrivate(arg)); + if (!privateObject) { + LogError("Private object is not set."); + ThrowMsg(Commons::NullPointerException, "Private object not initialized"); + } + + ITZDatePtr TZDate = privateObject->getObject(); + TZDateProperties result; + + result.timezone = TZDate->getTimezone(); + result.year = TZDate->get(ITZDate::TZDATE_YEAR); + result.month = TZDate->get(ITZDate::TZDATE_MONTH); + result.day= TZDate->get(ITZDate::TZDATE_DATE); + result.hours= TZDate->get(ITZDate::TZDATE_HOUR_OF_DAY); + result.minutes= TZDate->get(ITZDate::TZDATE_MINUTE); + result.seconds= TZDate->get(ITZDate::TZDATE_SECOND); + result.milliseconds = TZDate->get(ITZDate::TZDATE_MILLISECOND); + return result; +} + +JSObjectRef TimeUtilConverter::makeDurationObject(const DurationProperties &duration) { + JSObjectRef propertyRef = JSObjectMake(m_context, NULL, NULL); + ScopedJSStringRef jsLengthString(JSStringCreateWithUTF8CString("length")); + ScopedJSStringRef jsUnitString(JSStringCreateWithUTF8CString("unit")); + std::string unitStr = "MSECS"; + switch (duration.unit) { + case DAYS_UNIT: + unitStr = "DAYS"; + break; + case SECONDS_UNIT: + unitStr = "SECS"; + break; + case MINUTES_UNIT: + unitStr = "MINS"; + break; + case HOURS_UNIT: + unitStr = "HOURS"; + break; + } + JSObjectSetProperty(m_context, propertyRef, jsLengthString.get(), toJSValueRef(static_cast<double>(duration.length)), kJSPropertyAttributeNone, NULL); + JSObjectSetProperty(m_context, propertyRef, jsUnitString.get(), toJSValueRef(unitStr), kJSPropertyAttributeNone, NULL); + + return propertyRef; +} + +JSObjectRef TimeUtilConverter::makeMillisecondDurationObject(const long long length) { + DurationProperties duration; + duration.length = length; + duration.unit = MSECS_UNIT; + const long long dayToMsecs = 1000 * 60 * 60 * 24; + if ((length % dayToMsecs) == 0) { + duration.length = length / dayToMsecs; + duration.unit = DAYS_UNIT; + } + + return makeDurationObject(duration); +} + +DurationProperties TimeUtilConverter::getDurationPropertis(JSValueRef value, JSValueRef* exception) { + DurationProperties duration; + duration.length = getDurationLength(value, exception); + duration.unit = getDurationUnit(value, exception); + return duration; +} + +long long TimeUtilConverter::getDurationLength(JSValueRef value, JSValueRef* exception) { + JSValueRef l_JSProperty = JSUtils::getJSProperty(m_context, value, "length", exception); + double result = 0; + + if (l_JSProperty != NULL && JSValueIsNumber(m_context, l_JSProperty)) { + result = toDouble(l_JSProperty); + } else { + ThrowMsg(Commons::InvalidArgumentException, "Length doesn't exist or wrong type"); + } + return static_cast<long long>(result); +} + +short TimeUtilConverter::getDurationUnit(JSValueRef value, JSValueRef* exception) { + JSValueRef l_JSProperty = JSUtils::getJSProperty(m_context, value, "unit", exception); + std::string result = "MSECS"; + + if (l_JSProperty != NULL && JSValueIsString(m_context, l_JSProperty)) { + result = toString(l_JSProperty); + } + + if (!result.compare("DAYS")) + return DAYS_UNIT; + else if (!result.compare("SECS")) + return SECONDS_UNIT; + else if (!result.compare("MINS")) + return MINUTES_UNIT; + else if (!result.compare("HOURS")) + return HOURS_UNIT; + else + return MSECS_UNIT; + +} + +long long TimeUtilConverter::convertDurationLength(DurationProperties duration, short unit) { + if (duration.unit < unit) + ThrowMsg(Commons::ConversionException, "Unit is larger thatn duration's unit"); + + if (duration.unit == unit) + return duration.length; + + long long result = duration.length; + switch(unit) { + case MSECS_UNIT: + result = result * 1000; + if (duration.unit == SECONDS_UNIT) + return result; + case SECONDS_UNIT: + result = result * 60; + if (duration.unit == MINUTES_UNIT) + return result; + case MINUTES_UNIT: + result = result * 60; + if (duration.unit == HOURS_UNIT) + return result; + case HOURS_UNIT: + result = result * 24; + return result; + } +} + +std::time_t TimeUtilConverter::toTZDateTimeT(JSValueRef arg) { + if (JSValueIsNull(m_context, arg) || JSValueIsUndefined(m_context, arg)) { + ThrowMsg(Commons::InvalidArgumentException, + "JSValueRef is JS null or JS undefined."); + } + JSObjectRef obj = toJSObjectRef(arg); + + return toTZDateTimeT(obj); +} + +std::time_t TimeUtilConverter::toTZDateTimeT(JSObjectRef arg) { + if (!arg) { + LogError("Object is null"); + ThrowMsg(Commons::NullPointerException, "Private object not initialized"); + } + + TZDatePrivObject* privateObject = static_cast<TZDatePrivObject*>(JSObjectGetPrivate(arg)); + if (!privateObject) { + LogError("Private object is not set."); + ThrowMsg(Commons::NullPointerException, "Private object not initialized"); + } + + ITZDatePtr TZDate = privateObject->getObject(); + + TZDateProperties UTCProperties = TZDate->toLocalTimezone(); + struct tm localTm; + + localTm.tm_year = UTCProperties.year - 1900; + localTm.tm_mon = UTCProperties.month; + localTm.tm_mday= UTCProperties.day; + localTm.tm_hour= UTCProperties.hours; + localTm.tm_min=UTCProperties.minutes; + localTm.tm_sec= UTCProperties.seconds; + localTm.tm_isdst = 0; + + return mktime(&localTm); +} + +} +} |