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.
21 #include <dpl/log/log.h>
25 #include <Commons/Exception.h>
27 #include <unicode/ustring.h>
28 #include <unicode/timezone.h>
29 #include <unicode/calendar.h>
30 #include <unicode/strenum.h>
31 #include <unicode/datefmt.h>
33 #include "TZDateProperties.h"
35 #include "TimeUtilTools.h"
37 using namespace WrtDeviceApis;
43 static void changedTimezoneCallback(keynode_t* node, void* user_data) {
44 LogDebug("entered KeyName:" << vconf_keynode_get_name(node));
45 if (!strcmp(vconf_keynode_get_name(node), VCONFKEY_SETAPPL_TIMEZONE_ID) && (vconf_keynode_get_type(node) == VCONF_TYPE_STRING)) {
46 char *timezoneID = vconf_keynode_get_str(node);
47 if (timezoneID != NULL) {
48 LogDebug("Changed Timezone : " << timezoneID);
50 UnicodeString *id = util.toUnicodeString(timezoneID);
51 TimeZone *tz = TimeZone::createTimeZone(*id);
54 TimeZone::setDefault(*tz);
63 bRegisteredChangedTimezone = false;
64 if (vconf_notify_key_changed(VCONFKEY_SETAPPL_TIMEZONE_ID, changedTimezoneCallback, NULL) == 0) {
65 LogDebug("Success to register changedTimezoneCallback");
66 bRegisteredChangedTimezone = true;
73 if (bRegisteredChangedTimezone) {
74 vconf_ignore_key_changed(VCONFKEY_SETAPPL_TIMEZONE_ID, changedTimezoneCallback);
78 std::string TimeUtil::getLocalTimezone() {
80 UnicodeString idResult;
82 TimeZone* zone = TimeZone::createDefault();
90 std::string s_result = util.toString(id);
91 LogDebug("result : " << s_result);
94 } catch (Commons::PlatformException) {
95 LogError("can't get the local timezone's name");
98 ThrowMsg(Commons::PlatformException, "Can't get Local Timezone");
102 std::string TimeUtil::getUTCTimezone() {
106 const TimeZone *utcTimezone = TimeZone::getGMT();
107 utcTimezone->getID(id);
111 std::string s_result = util.toString(id);
113 LogDebug("result : " << s_result);
116 } catch(Commons::PlatformException) {
117 LogError("can't get the UTC timezone's name");
120 ThrowMsg(Commons::PlatformException, "Can't get UTC Timezone");
123 std::vector<std::string> TimeUtil::getAvailableTimezones(){
124 UErrorCode ec = U_ZERO_ERROR;
125 StringEnumeration * tzen = TimeZone::createEnumeration();
126 const char *str = NULL;
127 int32_t count = tzen->count(ec);
128 std::vector<std::string> timezones;
130 LogDebug("count: " << count);
136 int32_t resultLen = 0;
138 str = tzen->next(&resultLen, ec);
140 std::string timezone = str;
141 timezones.push_back(timezone);
144 }while((str!=NULL) && (i < count));
146 ThrowMsg(Commons::PlatformException, "Can't get timezones list");
151 std::string TimeUtil::getDateFormat(const bool b_shortFormat) {
154 UnicodeString timeFormat = util.getDateTimeFormat((b_shortFormat ? TimeUtilTools::DATE_SHORT_FORMAT : TimeUtilTools::DATE_FORMAT), true);
155 timeFormat = timeFormat.findAndReplace("E", "D");
157 if (timeFormat.indexOf("MMM") > 0) {
158 if (timeFormat.indexOf("MMMM") > 0)
159 timeFormat = timeFormat.findAndReplace("MMMM", "M");
161 timeFormat = timeFormat.findAndReplace("MMM", "M");
163 timeFormat = timeFormat.findAndReplace("M", "m");
168 while (i < timeFormat.length()) {
169 if (timeFormat[i] == timeFormat[i+1])
170 timeFormat.remove(i, 1);
174 LogDebug("result : " << util.toString(timeFormat));
175 return util.toString(timeFormat);
178 std::string TimeUtil::getTimeFormat(){
181 UnicodeString timeFormat = util.getDateTimeFormat(TimeUtilTools::TIME_FORMAT, true);
182 timeFormat = timeFormat.findAndReplace("H", "h");
183 timeFormat = timeFormat.findAndReplace("a", "ap");
187 while (i < timeFormat.length()) {
188 if (timeFormat[i] == timeFormat[i+1])
189 timeFormat.remove(i, 1);
193 LogDebug("result : " << util.toString(timeFormat));
194 return util.toString(timeFormat);