merge with master
[platform/framework/web/wrt-plugins-tizen.git] / src / TimeUtil / TimeUtil.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
19
20
21 #include <dpl/log/log.h>
22 #include <ctime>
23 #include <vconf.h>
24
25 #include <Commons/Exception.h>
26
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>
32
33 #include "TZDateProperties.h"
34 #include "TimeUtil.h"
35 #include "TimeUtilTools.h"
36
37 using namespace WrtDeviceApis;
38
39 namespace DeviceAPI  {
40 namespace Time {
41
42 namespace {
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);
49                                 TimeUtilTools util;
50                                 UnicodeString *id = util.toUnicodeString(timezoneID);
51                                 TimeZone *tz = TimeZone::createTimeZone(*id);
52                                 delete id;
53
54                                 TimeZone::setDefault(*tz);
55                         }
56                 }
57         }
58 }//private namespace
59
60 TimeUtil::TimeUtil()
61 {
62         LogDebug("entered");
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;
67         }
68 }
69
70 TimeUtil::~TimeUtil()
71 {
72         LogDebug("entered");
73         if (bRegisteredChangedTimezone) {
74                 vconf_ignore_key_changed(VCONFKEY_SETAPPL_TIMEZONE_ID, changedTimezoneCallback);
75         }
76 }
77
78 std::string TimeUtil::getLocalTimezone() {
79         LogDebug("entered");
80         UnicodeString idResult;
81
82         TimeZone* zone = TimeZone::createDefault();
83         UnicodeString id;
84         zone->getID(id);
85
86         delete zone;
87
88         try {
89                 TimeUtilTools util;
90                  std::string s_result = util.toString(id);
91                  LogDebug("result : " << s_result);
92
93                 return s_result;
94         } catch (Commons::PlatformException) {
95                 LogError("can't get the local timezone's name");
96         }
97
98         ThrowMsg(Commons::PlatformException, "Can't get Local Timezone");
99 }
100
101
102 std::string TimeUtil::getUTCTimezone() {
103         LogDebug("entered");
104         UnicodeString id;
105
106         const TimeZone *utcTimezone = TimeZone::getGMT();
107         utcTimezone->getID(id);
108
109         try {
110                 TimeUtilTools util;
111                 std::string s_result = util.toString(id);
112
113                  LogDebug("result : " << s_result);
114                 
115                 return s_result;
116         } catch(Commons::PlatformException) {
117                 LogError("can't get the UTC timezone's name");
118         }
119
120         ThrowMsg(Commons::PlatformException, "Can't get UTC Timezone");
121 }
122         
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;
129         
130         LogDebug("count: " << count);
131
132         if (U_SUCCESS(ec)) {
133                 int i = 0;
134                 
135                 do {
136                         int32_t resultLen = 0;
137                         
138                         str = tzen->next(&resultLen, ec);
139                         if(U_SUCCESS(ec)) {
140                                 std::string timezone = str;
141                                 timezones.push_back(timezone);
142                                 i++;
143                         }
144                 }while((str!=NULL) && (i < count));     
145         } else {
146                 ThrowMsg(Commons::PlatformException, "Can't get timezones list");
147         }
148         return timezones;;
149 }
150
151 std::string TimeUtil::getDateFormat(const bool b_shortFormat) {
152         LogDebug("entered");
153         TimeUtilTools util;
154         UnicodeString timeFormat = util.getDateTimeFormat((b_shortFormat ? TimeUtilTools::DATE_SHORT_FORMAT : TimeUtilTools::DATE_FORMAT), true);
155         timeFormat = timeFormat.findAndReplace("E", "D");
156
157         if (timeFormat.indexOf("MMM") > 0) {
158                 if (timeFormat.indexOf("MMMM") > 0)
159                         timeFormat = timeFormat.findAndReplace("MMMM", "M");
160                 else
161                         timeFormat = timeFormat.findAndReplace("MMM", "M");
162         } else {
163                 timeFormat = timeFormat.findAndReplace("M", "m");
164         }
165
166         int32_t i = 0;
167         
168         while (i < timeFormat.length()) {
169                 if (timeFormat[i] == timeFormat[i+1])
170                         timeFormat.remove(i, 1);
171                 else
172                         i++;
173         }
174         LogDebug("result : " << util.toString(timeFormat));
175         return util.toString(timeFormat);
176 }
177
178 std::string TimeUtil::getTimeFormat(){
179         LogDebug("entered");
180         TimeUtilTools util;
181         UnicodeString timeFormat = util.getDateTimeFormat(TimeUtilTools::TIME_FORMAT, true);
182         timeFormat = timeFormat.findAndReplace("H", "h");
183         timeFormat = timeFormat.findAndReplace("a", "ap");
184
185         int32_t i = 0;
186         
187         while (i < timeFormat.length()) {
188                 if (timeFormat[i] == timeFormat[i+1])
189                         timeFormat.remove(i, 1);
190                 else
191                         i++;
192         }
193         LogDebug("result : " << util.toString(timeFormat));
194         return util.toString(timeFormat);
195 }
196
197 }
198 }