2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define __STDC_LIMIT_MACROS
21 #include <utils_i18n_timezone.h>
22 #include <utils_i18n_private.h>
23 #include <utils_i18n_ustring.h>
25 #include <unicode/ustdio.h>
26 #include <unicode/ucal.h>
27 #include <unicode/timezone.h>
28 #include <unicode/strenum.h>
29 #include <unicode/ustring.h>
30 #include <unicode/uchar.h>
31 #include <unicode/locid.h>
33 int i18n_timezone_create_unknown ( i18n_timezone_h *timezone )
35 retv_if(timezone == NULL, I18N_ERROR_INVALID_PARAMETER);
37 *timezone = (TimeZone::getUnknown()).clone();
38 retv_if(timezone == NULL, I18N_ERROR_OUT_OF_MEMORY);
40 return I18N_ERROR_NONE;
43 int i18n_timezone_create_gmt ( i18n_timezone_h *timezone )
45 retv_if(timezone == NULL, I18N_ERROR_INVALID_PARAMETER);
47 const TimeZone* gmt = TimeZone::getGMT();
48 retv_if(gmt == NULL, I18N_ERROR_OUT_OF_MEMORY);
50 *timezone = gmt->clone();
54 retv_if(timezone == NULL, I18N_ERROR_OUT_OF_MEMORY);
56 return I18N_ERROR_NONE;
59 int i18n_timezone_create ( i18n_timezone_h *timezone, const char *timezone_id )
61 retv_if(timezone == NULL, I18N_ERROR_INVALID_PARAMETER);
63 const UnicodeString id(timezone_id);
64 *timezone = TimeZone::createTimeZone(id);
65 retv_if(*timezone == NULL, I18N_ERROR_OUT_OF_MEMORY);
67 return I18N_ERROR_NONE;
70 int i18n_timezone_destroy(i18n_timezone_h timezone)
72 retv_if(timezone == NULL, I18N_ERROR_INVALID_PARAMETER);
74 delete((TimeZone*)timezone);
76 return I18N_ERROR_NONE;
79 int i18n_timezone_foreach_timezone_id_by_region(i18n_system_timezone_type_e timezone_type, const char *region, const int32_t *raw_offset,
80 i18n_timezone_id_cb cb, void* user_data)
82 retv_if(cb == NULL, I18N_ERROR_INVALID_PARAMETER);
83 UErrorCode err = U_ZERO_ERROR;
85 StringEnumeration *s = TimeZone::createTimeZoneIDEnumeration((USystemTimeZoneType)timezone_type, region, raw_offset, err);
87 UErrorCode status=U_ZERO_ERROR;
88 int32_t count = s->count(status);
89 for (int i = 0; i < count; i++) {
90 int32_t resultLength = 0;
91 if( cb(s->next(&resultLength, status), user_data) == false ) {
97 return _i18n_error_mapping(err);
100 int i18n_timezone_foreach_timezone_id(i18n_timezone_id_cb cb, void* user_data)
102 retv_if(cb == NULL, I18N_ERROR_INVALID_PARAMETER);
103 StringEnumeration *s = TimeZone::createEnumeration();
105 UErrorCode status = U_ZERO_ERROR;
106 int32_t count = s->count(status);
107 for (int i = 0; i < count; i++) {
108 int32_t resultLength = 0;
109 if( cb(s->next(&resultLength, status), user_data) == false ) {
115 return I18N_ERROR_NONE;
118 int i18n_timezone_foreach_timezone_id_with_offset(int32_t raw_offset, i18n_timezone_id_cb cb, void* user_data)
120 retv_if(cb == NULL, I18N_ERROR_INVALID_PARAMETER);
121 StringEnumeration *s = TimeZone::createEnumeration(raw_offset);
123 UErrorCode status = U_ZERO_ERROR;
124 int32_t count = s->count(status);
125 for (int i = 0; i < count; i++) {
126 int32_t resultLength = 0;
127 if( cb(s->next(&resultLength, status), user_data) == false ) {
133 return I18N_ERROR_NONE;
136 int i18n_timezone_foreach_timezone_id_by_country(const char *country, i18n_timezone_id_cb cb, void* user_data)
138 retv_if(cb == NULL, I18N_ERROR_INVALID_PARAMETER);
139 StringEnumeration *s = TimeZone::createEnumeration(country);
141 UErrorCode status = U_ZERO_ERROR;
142 int32_t count = s->count(status);
143 for (int i = 0; i < count; i++) {
144 int32_t resultLength = 0;
145 if( cb(s->next(&resultLength, status), user_data) == false ) {
151 return I18N_ERROR_NONE;
154 int i18n_timezone_count_equivalent_ids(const char *timezone_id, int32_t *count)
156 retv_if(timezone_id == NULL || count == NULL, I18N_ERROR_INVALID_PARAMETER);
157 const UnicodeString id(timezone_id);
158 *count = TimeZone::countEquivalentIDs(id);
160 return I18N_ERROR_NONE;
163 int i18n_timezone_get_equivalent_id(const char *timezone_id, int32_t index, char **equivalent_timezone_id)
165 retv_if(timezone_id == NULL || equivalent_timezone_id == NULL, I18N_ERROR_INVALID_PARAMETER);
167 *equivalent_timezone_id = NULL;
168 const UnicodeString id(timezone_id);
170 UnicodeString equivalentTimezoneId = TimeZone::getEquivalentID(id, index);
171 const UChar * equivalent_tid = equivalentTimezoneId.getTerminatedBuffer();
173 retv_if(equivalent_tid == NULL, I18N_ERROR_INVALID_PARAMETER);
174 int32_t ulen = u_strlen(equivalent_tid);
176 retv_if(ulen <= 0, I18N_ERROR_INVALID_PARAMETER);
177 *equivalent_timezone_id = (char*)malloc(ulen + 1);
179 retv_if(*equivalent_timezone_id == NULL, I18N_ERROR_OUT_OF_MEMORY);
181 u_austrcpy(*equivalent_timezone_id, equivalent_tid);
183 return I18N_ERROR_NONE;
186 int i18n_timezone_create_default ( i18n_timezone_h *timezone )
188 retv_if(timezone == NULL, I18N_ERROR_INVALID_PARAMETER);
189 *timezone = TimeZone::createDefault();
191 return I18N_ERROR_NONE;
194 int i18n_timezone_set_default( i18n_timezone_h timezone )
196 retv_if(timezone == NULL, I18N_ERROR_INVALID_PARAMETER);
198 TimeZone::setDefault(*(TimeZone*)timezone);
200 return I18N_ERROR_NONE;
203 const char* i18n_timezone_get_tzdata_version(void)
205 UErrorCode status = U_ZERO_ERROR;
206 const char* tzver = TimeZone::getTZDataVersion(status);
208 set_last_result(_i18n_error_mapping(status));
213 int i18n_timezone_get_region(const char *timezone_id, char *region, int32_t *region_len, int32_t region_capacity)
215 retv_if(timezone_id == NULL || region == NULL || region_len == NULL, I18N_ERROR_INVALID_PARAMETER);
217 UErrorCode status = U_ZERO_ERROR;
218 const UnicodeString id(timezone_id);
219 *region_len = TimeZone::getRegion(id, region, region_capacity, status);
221 return _i18n_error_mapping(status);
225 int i18n_timezone_get_offset_with_date(i18n_timezone_h timezone, i18n_udate date, i18n_ubool local, int32_t *raw_offset, int32_t *dst_offset)
227 retv_if(timezone == NULL || raw_offset == NULL || dst_offset == NULL, I18N_ERROR_INVALID_PARAMETER);
229 UErrorCode status = U_ZERO_ERROR;
230 ((TimeZone*)timezone)->getOffset( date, local, *raw_offset, *dst_offset, status );
232 return _i18n_error_mapping(status);
236 int i18n_timezone_set_raw_offset(i18n_timezone_h timezone, int32_t offset_milliseconds)
238 retv_if(timezone == NULL, I18N_ERROR_INVALID_PARAMETER);
239 ((TimeZone*)timezone)->setRawOffset(offset_milliseconds);
241 return I18N_ERROR_NONE;
244 int i18n_timezone_get_raw_offset(i18n_timezone_h timezone, int32_t *offset_milliseconds)
246 retv_if(timezone == NULL || offset_milliseconds == NULL, I18N_ERROR_INVALID_PARAMETER);
248 *offset_milliseconds = ((TimeZone*)timezone)->getRawOffset();
250 return I18N_ERROR_NONE;
253 int i18n_timezone_get_id(i18n_timezone_h timezone, char **timezone_id)
255 retv_if(timezone == NULL || timezone_id == NULL, I18N_ERROR_INVALID_PARAMETER);
258 UnicodeString TimezoneID;
260 ((TimeZone*)timezone)->getID(TimezoneID);
261 const UChar * tid = TimezoneID.getTerminatedBuffer();
263 retv_if(tid == NULL, I18N_ERROR_INVALID_PARAMETER);
264 int32_t ulen = u_strlen(tid);
266 retv_if(ulen <= 0, I18N_ERROR_INVALID_PARAMETER);
267 *timezone_id = (char*)malloc(ulen+1);
269 retv_if(*timezone_id == NULL, I18N_ERROR_OUT_OF_MEMORY);
270 u_austrcpy(*timezone_id, tid);
272 return I18N_ERROR_NONE;
275 int i18n_timezone_set_id(i18n_timezone_h timezone, const char *timezone_id)
277 retv_if(timezone == NULL || timezone_id == NULL, I18N_ERROR_INVALID_PARAMETER);
278 const UnicodeString id(timezone_id);
279 ((TimeZone*)timezone)->setID(id);
281 return I18N_ERROR_NONE;
284 int i18n_timezone_get_display_name(i18n_timezone_h timezone, char **display_name)
286 retv_if(timezone == NULL || display_name == NULL, I18N_ERROR_INVALID_PARAMETER);
288 *display_name = NULL;
289 UnicodeString displayName;
291 ((TimeZone*)timezone)->getDisplayName(displayName);
292 const UChar * dn = displayName.getTerminatedBuffer();
294 retv_if(dn == NULL, I18N_ERROR_INVALID_PARAMETER);
295 int32_t ulen = u_strlen(dn);
297 retv_if(ulen <= 0, I18N_ERROR_INVALID_PARAMETER);
298 *display_name = (char*)malloc(ulen + 1);
300 retv_if(*display_name == NULL, I18N_ERROR_OUT_OF_MEMORY);
302 u_austrcpy(*display_name, dn);
304 return I18N_ERROR_NONE;
307 int i18n_timezone_get_display_name_with_locale(i18n_timezone_h timezone, const char *language, const char *country , char **display_name)
309 retv_if(timezone == NULL || display_name == NULL, I18N_ERROR_INVALID_PARAMETER);
311 Locale locale(language, country, 0, 0);
312 *display_name = NULL;
313 UnicodeString displayName;
315 ((TimeZone*)timezone)->getDisplayName(locale, displayName);
316 const UChar * dn = displayName.getTerminatedBuffer();
318 retv_if(dn == NULL, I18N_ERROR_INVALID_PARAMETER);
319 int32_t ulen = u_strlen(dn);
321 retv_if(ulen <= 0, I18N_ERROR_INVALID_PARAMETER);
322 *display_name = (char*)malloc(ulen + 1);
324 retv_if(*display_name == NULL, I18N_ERROR_OUT_OF_MEMORY);
326 u_austrcpy(*display_name, dn);
328 return I18N_ERROR_NONE;
331 int i18n_timezone_get_display_name_with_type(i18n_timezone_h timezone, i18n_ubool daylight, i18n_timezone_display_type_e style,
334 retv_if(timezone == NULL || display_name == NULL, I18N_ERROR_INVALID_PARAMETER);
336 *display_name = NULL;
337 UnicodeString displayName;
339 ((TimeZone*)timezone)->getDisplayName((UBool)daylight, (TimeZone::EDisplayType)style, displayName);
340 const UChar * dn = displayName.getTerminatedBuffer();
342 retv_if(dn == NULL, I18N_ERROR_INVALID_PARAMETER);
343 int32_t ulen = u_strlen(dn);
345 retv_if(ulen <= 0, I18N_ERROR_INVALID_PARAMETER);
346 *display_name = (char*)malloc(ulen + 1);
348 retv_if(*display_name == NULL, I18N_ERROR_OUT_OF_MEMORY);
350 u_austrcpy(*display_name, dn);
352 return I18N_ERROR_NONE;
355 int i18n_timezone_get_display_name_with_type_locale(i18n_timezone_h timezone, i18n_ubool daylight, i18n_timezone_display_type_e style,
356 const char *language, const char *country, char **display_name)
358 retv_if(timezone == NULL || display_name == NULL, I18N_ERROR_INVALID_PARAMETER);
360 const Locale locale(language, country, 0, 0);
361 *display_name = NULL;
362 UnicodeString displayName;
364 ((TimeZone*)timezone)->getDisplayName((UBool)daylight, (TimeZone::EDisplayType)style, locale, displayName);
365 const UChar * dn = displayName.getTerminatedBuffer();
367 retv_if(dn == NULL, I18N_ERROR_INVALID_PARAMETER);
368 int32_t ulen = u_strlen(dn);
370 retv_if(ulen <= 0, I18N_ERROR_INVALID_PARAMETER);
371 *display_name = (char*)malloc(ulen + 1);
373 retv_if(*display_name == NULL, I18N_ERROR_OUT_OF_MEMORY);
375 u_austrcpy(*display_name, dn);
377 return I18N_ERROR_NONE;
380 int i18n_timezone_use_daylight_time(i18n_timezone_h timezone, i18n_ubool *daylight_time)
382 retv_if(timezone == NULL || daylight_time == NULL, I18N_ERROR_INVALID_PARAMETER);
384 *daylight_time = ((TimeZone*)timezone)->useDaylightTime();
386 return I18N_ERROR_NONE;
389 int i18n_timezone_in_daylight_time(i18n_timezone_h timezone, i18n_udate date, i18n_ubool *daylight_time)
391 retv_if(timezone == NULL || daylight_time == NULL, I18N_ERROR_INVALID_PARAMETER);
395 *daylight_time = ((TimeZone*)timezone)->inDaylightTime(date, status);
397 return _i18n_error_mapping(status);
401 int i18n_timezone_has_same_rule(i18n_timezone_h timezone, i18n_timezone_h other, i18n_ubool *same_rule)
403 retv_if(timezone == NULL || same_rule == NULL, I18N_ERROR_INVALID_PARAMETER);
405 *same_rule = ((TimeZone*)timezone)->hasSameRules(*(TimeZone*)other);
407 return I18N_ERROR_NONE;
410 int i18n_timezone_clone(i18n_timezone_h timezone, i18n_timezone_h *clone)
412 retv_if(timezone == NULL || clone == NULL, I18N_ERROR_INVALID_PARAMETER);
414 *clone = ((TimeZone*)timezone)->clone();
416 return I18N_ERROR_NONE;
419 int i18n_timezone_get_dst_savings(i18n_timezone_h timezone, int32_t *dst_savings)
421 retv_if(timezone == NULL || dst_savings == NULL, I18N_ERROR_INVALID_PARAMETER);
423 *dst_savings = ((TimeZone*)timezone)->getDSTSavings();
425 return I18N_ERROR_NONE;