wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / TimeUtil / TimeUtilTools.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 <unicode/ustring.h>
19 #include <unicode/udat.h>
20 #include <unicode/dtptngen.h>
21 #include <string.h>
22 #include <vconf.h>
23 #include <Commons/Exception.h>
24 #include "TimeUtilTools.h"
25 #include <Logger.h>
26
27 namespace DeviceAPI {
28 namespace Time {
29
30 using namespace WrtDeviceApis;
31
32 long TimeUtilTools::tolong(const int32_t num)
33 {
34         return static_cast<long>(num);
35 }
36
37 int32_t TimeUtilTools::toint32_t(const long long num)
38 {
39         return static_cast<int32_t>(num);
40 }
41
42 int32_t TimeUtilTools::toint32_t(const long num)
43 {
44         return static_cast<int32_t>(num);
45 }
46
47 int32_t TimeUtilTools::toint32_t(const int num)
48 {
49         return static_cast<int32_t>(num);
50 }
51
52 UnicodeString *TimeUtilTools::toUnicodeString(const std::string str)
53 {
54         UnicodeString *id = new UnicodeString(str.c_str());
55
56         return id;
57 }
58
59 std::string TimeUtilTools::toString(UnicodeString uniStr)
60 {
61         int bufferLen = sizeof(UChar)*static_cast<int>(uniStr.length()) + 1;
62         char *resultBuffer = (char *)malloc(bufferLen);
63         if (!resultBuffer)
64                 ThrowMsg(Commons::PlatformException, "memory allocation error");
65         memset(resultBuffer, 0, bufferLen);
66         CheckedArrayByteSink sink(resultBuffer, bufferLen);
67         uniStr.toUTF8(sink);
68         if (sink.Overflowed())
69                 ThrowMsg(Commons::PlatformException, "Converting error");
70         
71         std::string str(resultBuffer);
72         free(resultBuffer);
73         LoggerD(str);
74         return str;
75 }
76
77 TimeZone *TimeUtilTools::makeTimeZone(const std::string &name)
78 {
79         LoggerD("entered timezone name : " << name);
80
81         UnicodeString *id = toUnicodeString(name);
82
83         TimeZone *tz = TimeZone::createTimeZone(*id);
84         delete id;
85
86         return tz;
87 }
88
89 bool TimeUtilTools::compareTimeZoneName(Calendar *cal, const std::string &name) {
90         UnicodeString id;
91         cal->getTimeZone().getID(id);
92         std::string timezone = toString(id);
93
94         if ((timezone != name) && (timezone == "Etc/Unknown"))
95                 return false;
96
97         return true;
98 }
99
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);
103         if (U_SUCCESS(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);
110                 else {
111                         int ret = 0;
112                         int value = 0;
113                         ret = vconf_get_int(VCONFKEY_REGIONFORMAT_TIME1224, &value);
114                         // if failed, set default time format
115                         if (-1 == ret) {
116                                 value = VCONFKEY_TIME_FORMAT_12;
117                         }
118
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";
124                         else
125                                 skeletone += "HHmmss";
126
127                         UnicodeString *skeletoneUniStr = toUnicodeString(skeletone);
128                         patten = dateTimepatten->getBestPattern(*skeletoneUniStr, ec);
129                         delete skeletoneUniStr;
130
131                         if (!bLocale)
132                                 patten += " 'GMT'Z v'";
133                 }
134         LoggerD("pattern : " << toString(patten));
135         return patten;
136
137         }
138
139         return "";
140 }
141
142 void TimeUtilTools::printDate(Calendar *cal)
143 {
144         UErrorCode ec = U_ZERO_ERROR;
145         
146         if (cal == NULL)
147                 return;
148         
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)));
162 }
163
164 }
165 }