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 <unicode/ustring.h>
19 #include <unicode/udat.h>
20 #include <unicode/dtptngen.h>
23 #include <Commons/Exception.h>
24 #include "TimeUtilTools.h"
30 using namespace WrtDeviceApis;
32 long TimeUtilTools::tolong(const int32_t num)
34 return static_cast<long>(num);
37 int32_t TimeUtilTools::toint32_t(const long long num)
39 return static_cast<int32_t>(num);
42 int32_t TimeUtilTools::toint32_t(const long num)
44 return static_cast<int32_t>(num);
47 int32_t TimeUtilTools::toint32_t(const int num)
49 return static_cast<int32_t>(num);
52 UnicodeString *TimeUtilTools::toUnicodeString(const std::string str)
54 UnicodeString *id = new UnicodeString(str.c_str());
59 std::string TimeUtilTools::toString(UnicodeString uniStr)
61 int bufferLen = sizeof(UChar)*static_cast<int>(uniStr.length()) + 1;
62 char *resultBuffer = (char *)malloc(bufferLen);
64 ThrowMsg(Commons::PlatformException, "memory allocation error");
65 memset(resultBuffer, 0, bufferLen);
66 CheckedArrayByteSink sink(resultBuffer, bufferLen);
68 if (sink.Overflowed())
69 ThrowMsg(Commons::PlatformException, "Converting error");
71 std::string str(resultBuffer);
77 TimeZone *TimeUtilTools::makeTimeZone(const std::string &name)
79 LoggerD("entered timezone name : " << name);
81 UnicodeString *id = toUnicodeString(name);
83 TimeZone *tz = TimeZone::createTimeZone(*id);
89 bool TimeUtilTools::compareTimeZoneName(Calendar *cal, const std::string &name) {
91 cal->getTimeZone().getID(id);
92 std::string timezone = toString(id);
94 if ((timezone != name) && (timezone == "Etc/Unknown"))
100 UnicodeString TimeUtilTools::getDateTimeFormat(DateTimeFormatType type, bool bLocale) {
101 UErrorCode ec = U_ZERO_ERROR;
102 DateTimePatternGenerator *dateTimepatten = DateTimePatternGenerator::createInstance((bLocale ? Locale::getDefault() : Locale::getEnglish()), ec);
104 UnicodeString patten;
105 LoggerD("Type : " << type);
106 if (type == DATE_FORMAT)
107 patten = dateTimepatten->getBestPattern(UDAT_YEAR_MONTH_WEEKDAY_DAY, ec);
108 else if (type == DATE_SHORT_FORMAT)
109 patten = dateTimepatten->getBestPattern(UDAT_YEAR_NUM_MONTH_DAY, ec);
113 ret = vconf_get_int(VCONFKEY_REGIONFORMAT_TIME1224, &value);
114 // if failed, set default time format
116 value = VCONFKEY_TIME_FORMAT_12;
119 std::string skeletone;
120 if (type != TIME_FORMAT)
121 skeletone = UDAT_YEAR_MONTH_WEEKDAY_DAY;
122 if (value == VCONFKEY_TIME_FORMAT_12)
123 skeletone += "hhmmss";
125 skeletone += "HHmmss";
127 UnicodeString *skeletoneUniStr = toUnicodeString(skeletone);
128 patten = dateTimepatten->getBestPattern(*skeletoneUniStr, ec);
129 delete skeletoneUniStr;
132 patten += " 'GMT'Z v'";
134 LoggerD("pattern : " << toString(patten));
142 void TimeUtilTools::printDate(Calendar *cal)
144 UErrorCode ec = U_ZERO_ERROR;
149 LoggerD("year : " << tolong(cal->get(UCAL_YEAR, ec)));
150 LoggerD("month : " << tolong(cal->get(UCAL_MONTH, ec)));
151 LoggerD("day : " << tolong(cal->get(UCAL_DATE, ec)));
152 LoggerD("hours : " << tolong(cal->get(UCAL_HOUR, ec)));
153 LoggerD("hours of day : " << tolong(cal->get(UCAL_HOUR_OF_DAY, ec)));
154 LoggerD("AM/PM : " << tolong(cal->get(UCAL_AM_PM, ec)));
155 LoggerD("dayofweek : " << tolong(cal->get(UCAL_DAY_OF_WEEK, ec)));
156 LoggerD("minues : " << tolong(cal->get(UCAL_MINUTE, ec)));
157 LoggerD("seconds : " << tolong(cal->get(UCAL_SECOND, ec)));
158 LoggerD("miliseconds : " << tolong(cal->get(UCAL_MILLISECOND, ec)));
159 LoggerD("zone offset : " << tolong(cal->get(UCAL_ZONE_OFFSET, ec)));
160 LoggerD("dst offset : " << tolong(cal->get(UCAL_DST_OFFSET, ec)));
161 LoggerD("is leap month? : " << tolong(cal->get(UCAL_IS_LEAP_MONTH, ec)));