Git Init
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / TimeUtil / TimeUtil.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. 
15  */
16
17
18
19
20 #include <dpl/log.h>
21 #include <ctime>
22 #include <vconf.h>
23
24 #include <Commons/Exception.h>
25
26 #include <API/TimeUtil/TZDateProperties.h>
27
28 #include <unicode/ustring.h>
29 #include <unicode/timezone.h>
30 #include <unicode/calendar.h>
31 #include <unicode/strenum.h>
32 #include <unicode/datefmt.h>
33
34 #include "TimeUtil.h"
35 #include "TimeUtilTools.h"
36
37 using namespace TizenApis::Api::TimeUtil;
38 using namespace WrtDeviceApis;
39
40 namespace TizenApis {
41 namespace Platform {
42 namespace TimeUtil {
43
44 TimeUtil::TimeUtil()
45 {
46     LogDebug("entered");
47 }
48
49 TimeUtil::~TimeUtil()
50 {
51     LogDebug("entered");
52         
53 }
54
55 void TimeUtil::setCurrentTZDate(const TZDateProperties &tzdate){
56         ThrowMsg(Commons::UnsupportedException, "Not support to set current DateTime");
57 }
58
59 std::string TimeUtil::getLocalTimezone() {
60         LogDebug("entered");
61         UnicodeString idResult;
62         UChar *result = (UChar*)malloc(sizeof(UChar)*1024);
63         int32_t result_capacity = 1024;
64         int32_t len;
65         UErrorCode ec = U_ZERO_ERROR;
66
67         TimeZone* zone = TimeZone::createDefault();
68         UnicodeString ID;
69         zone->getID(ID);
70         len = ID.extract(result, result_capacity, ec);
71
72         delete zone;
73         
74         if (U_SUCCESS(ec)) {
75                 try {
76                         TimeUtilTools Util;
77                          std::string s_result = Util.strtoutf8(result);
78                          LogDebug("result : " << s_result);
79                         
80                         if (result) {
81                                 free(result);
82                                 result= NULL;
83                         }
84                         
85                         return s_result;
86                 } catch (Commons::PlatformException) {
87                         LogError("can't get the local timezone's name");
88                 }
89         }
90         
91         if (result) {
92                 free(result);
93                 result= NULL;
94         }
95
96         ThrowMsg(Commons::PlatformException, "Can't get Local Timezone");
97         return "";
98 }
99
100
101 std::string TimeUtil::getUTCTimezone() {
102         LogDebug("entered");
103         UnicodeString idResult;
104         UChar *result = (UChar*)malloc(sizeof(UChar)*1024);
105         int32_t result_capacity = 1024;
106         int32_t len;
107         UErrorCode ec = U_ZERO_ERROR;
108         UnicodeString ID;
109         
110         const TimeZone *UTCTimezone = TimeZone::getGMT();
111         UTCTimezone->getID(ID);
112          len = ID.extract(result, result_capacity, ec);
113
114         if (U_SUCCESS(ec)) {
115                 try {
116                         TimeUtilTools Util;
117                         std::string s_result = Util.strtoutf8(result);
118                         if (result) {
119                                 free(result);
120                                 result= NULL;
121                         }
122
123                          LogDebug("result : " << s_result);
124                         
125                         return s_result;
126                 } catch(Commons::PlatformException) {
127                         LogError("can't get the UTC timezone's name");
128                 }
129         }
130         if (result) {
131                 free(result);
132                 result= NULL;
133         }
134         ThrowMsg(Commons::PlatformException, "Can't get UTC Timezone");
135         return "";
136 }
137         
138 std::vector<std::string> TimeUtil::getAvailableTimezones(){
139         UErrorCode ec = U_ZERO_ERROR;
140         StringEnumeration * tzen = TimeZone::createEnumeration();
141         const char *str = NULL;
142         int32_t count = tzen->count(ec);
143         std::vector<std::string> timezones;
144         
145         LogDebug("count: " << count);
146
147         if (U_SUCCESS(ec)) {
148                 int i = 0;
149                 
150                 do {
151                         int32_t resultLen = 0;
152                         
153                         str = tzen->next(&resultLen, ec);
154                         if(U_SUCCESS(ec)) {
155                                 std::string timezone = str;
156                                 timezones.push_back(timezone);
157                                 i++;
158                         }
159                 }while((str!=NULL) && (i < count));     
160         } else {
161                 ThrowMsg(Commons::PlatformException, "Can't get timezones list");
162         }
163         return timezones;;
164 }
165
166 std::string TimeUtil::getDateFormat(const bool b_shortFormat) {
167         LogDebug("entered");
168
169         if (b_shortFormat)
170                 return "d/m/y";
171         else 
172                 return "D, M d y";
173 }
174
175 std::string TimeUtil::getTimeFormat(){
176         LogDebug("entered");
177
178         return "h:m:s ap";
179
180 }
181
182 }
183 }
184 }