tizen 2.3.1 release
[framework/api/base-utils.git] / src / utils_i18n_timezone.cpp
1 /*
2  * Copyright (c) 2015 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 #define __STDC_LIMIT_MACROS
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <utils_i18n_timezone.h>
22 #include <utils_i18n_private.h>
23 #include <utils_i18n_ustring.h>
24
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>
32
33 int i18n_timezone_create_unknown ( i18n_timezone_h *timezone )
34 {
35     retv_if(timezone == NULL, I18N_ERROR_INVALID_PARAMETER);
36
37     *timezone = (TimeZone::getUnknown()).clone();
38     retv_if(timezone == NULL, I18N_ERROR_OUT_OF_MEMORY);
39
40     return I18N_ERROR_NONE;
41 }
42
43 int i18n_timezone_create_gmt ( i18n_timezone_h *timezone )
44 {
45     retv_if(timezone == NULL, I18N_ERROR_INVALID_PARAMETER);
46
47     const TimeZone* gmt = TimeZone::getGMT();
48     retv_if(gmt == NULL, I18N_ERROR_OUT_OF_MEMORY);
49
50     *timezone = gmt->clone();
51     if(gmt != NULL) {
52         delete gmt;
53     }
54     retv_if(timezone == NULL, I18N_ERROR_OUT_OF_MEMORY);
55
56     return I18N_ERROR_NONE;
57 }
58
59 int i18n_timezone_create ( i18n_timezone_h *timezone, const char *timezone_id )
60 {
61     retv_if(timezone == NULL, I18N_ERROR_INVALID_PARAMETER);
62
63     const UnicodeString id(timezone_id);
64     *timezone = TimeZone::createTimeZone(id);
65     retv_if(*timezone == NULL, I18N_ERROR_OUT_OF_MEMORY);
66
67     return I18N_ERROR_NONE;
68 }
69
70 int i18n_timezone_destroy(i18n_timezone_h timezone)
71 {
72     retv_if(timezone == NULL, I18N_ERROR_INVALID_PARAMETER);
73
74     delete((TimeZone*)timezone);
75
76     return I18N_ERROR_NONE;
77 }
78
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)
81 {
82     retv_if(cb == NULL, I18N_ERROR_INVALID_PARAMETER);
83     UErrorCode err = U_ZERO_ERROR;
84
85     StringEnumeration *s = TimeZone::createTimeZoneIDEnumeration((USystemTimeZoneType)timezone_type, region, raw_offset, err);
86
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 ) {
92             break;
93         }
94     }
95
96     delete s;
97     return _i18n_error_mapping(err);
98 }
99
100 int i18n_timezone_foreach_timezone_id(i18n_timezone_id_cb cb, void* user_data)
101 {
102     retv_if(cb == NULL, I18N_ERROR_INVALID_PARAMETER);
103     StringEnumeration *s = TimeZone::createEnumeration();
104
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 ) {
110             break;
111         }
112     }
113
114     delete s;
115     return I18N_ERROR_NONE;
116 }
117
118 int i18n_timezone_foreach_timezone_id_with_offset(int32_t raw_offset, i18n_timezone_id_cb cb, void* user_data)
119 {
120     retv_if(cb == NULL, I18N_ERROR_INVALID_PARAMETER);
121     StringEnumeration *s = TimeZone::createEnumeration(raw_offset);
122
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 ) {
128             break;
129         }
130     }
131
132     delete s;
133     return I18N_ERROR_NONE;
134 }
135
136 int i18n_timezone_foreach_timezone_id_by_country(const char *country, i18n_timezone_id_cb cb, void* user_data)
137 {
138     retv_if(cb == NULL, I18N_ERROR_INVALID_PARAMETER);
139     StringEnumeration *s = TimeZone::createEnumeration(country);
140
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 ) {
146             break;
147         }
148     }
149
150     delete s;
151     return I18N_ERROR_NONE;
152 }
153
154 int i18n_timezone_count_equivalent_ids(const char *timezone_id, int32_t *count)
155 {
156     retv_if(timezone_id == NULL || count == NULL, I18N_ERROR_INVALID_PARAMETER);
157     const UnicodeString id(timezone_id);
158     *count = TimeZone::countEquivalentIDs(id);
159
160     return I18N_ERROR_NONE;
161 }
162
163 int i18n_timezone_get_equivalent_id(const char *timezone_id, int32_t index, char **equivalent_timezone_id)
164 {
165     retv_if(timezone_id == NULL || equivalent_timezone_id == NULL, I18N_ERROR_INVALID_PARAMETER);
166
167     *equivalent_timezone_id = NULL;
168     const UnicodeString id(timezone_id);
169
170     UnicodeString equivalentTimezoneId = TimeZone::getEquivalentID(id, index);
171     const UChar * equivalent_tid = equivalentTimezoneId.getTerminatedBuffer();
172
173     retv_if(equivalent_tid == NULL, I18N_ERROR_INVALID_PARAMETER);
174     int32_t ulen = u_strlen(equivalent_tid);
175
176     retv_if(ulen <= 0, I18N_ERROR_INVALID_PARAMETER);
177     *equivalent_timezone_id = (char*)malloc(ulen + 1);
178
179     retv_if(*equivalent_timezone_id == NULL, I18N_ERROR_OUT_OF_MEMORY);
180
181     u_austrcpy(*equivalent_timezone_id, equivalent_tid);
182
183     return I18N_ERROR_NONE;
184 }
185
186 int i18n_timezone_create_default ( i18n_timezone_h *timezone )
187 {
188     retv_if(timezone == NULL, I18N_ERROR_INVALID_PARAMETER);
189     *timezone = TimeZone::createDefault();
190
191     return I18N_ERROR_NONE;
192 }
193
194 int i18n_timezone_set_default( i18n_timezone_h timezone )
195 {
196     retv_if(timezone == NULL, I18N_ERROR_INVALID_PARAMETER);
197
198     TimeZone::setDefault(*(TimeZone*)timezone);
199
200     return I18N_ERROR_NONE;
201 }
202
203 const char* i18n_timezone_get_tzdata_version(void)
204 {
205     UErrorCode status = U_ZERO_ERROR;
206     const char* tzver = TimeZone::getTZDataVersion(status);
207
208     set_last_result(_i18n_error_mapping(status));
209
210     return tzver;
211 }
212
213 int i18n_timezone_get_region(const char *timezone_id, char *region, int32_t *region_len, int32_t region_capacity)
214 {
215     retv_if(timezone_id == NULL || region == NULL || region_len == NULL, I18N_ERROR_INVALID_PARAMETER);
216
217     UErrorCode status = U_ZERO_ERROR;
218     const UnicodeString id(timezone_id);
219     *region_len = TimeZone::getRegion(id, region, region_capacity, status);
220
221     return _i18n_error_mapping(status);
222
223 }
224
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)
226 {
227     retv_if(timezone == NULL || raw_offset == NULL || dst_offset == NULL, I18N_ERROR_INVALID_PARAMETER);
228
229     UErrorCode status = U_ZERO_ERROR;
230     ((TimeZone*)timezone)->getOffset( date, local, *raw_offset, *dst_offset, status );
231
232     return _i18n_error_mapping(status);
233
234 }
235
236 int i18n_timezone_set_raw_offset(i18n_timezone_h timezone, int32_t offset_milliseconds)
237 {
238     retv_if(timezone == NULL, I18N_ERROR_INVALID_PARAMETER);
239     ((TimeZone*)timezone)->setRawOffset(offset_milliseconds);
240
241     return I18N_ERROR_NONE;
242 }
243
244 int i18n_timezone_get_raw_offset(i18n_timezone_h timezone, int32_t *offset_milliseconds)
245 {
246     retv_if(timezone == NULL || offset_milliseconds == NULL, I18N_ERROR_INVALID_PARAMETER);
247
248     *offset_milliseconds = ((TimeZone*)timezone)->getRawOffset();
249
250     return I18N_ERROR_NONE;
251 }
252
253 int i18n_timezone_get_id(i18n_timezone_h timezone, char **timezone_id)
254 {
255     retv_if(timezone == NULL || timezone_id == NULL, I18N_ERROR_INVALID_PARAMETER);
256
257     *timezone_id = NULL;
258     UnicodeString TimezoneID;
259
260     ((TimeZone*)timezone)->getID(TimezoneID);
261     const UChar * tid = TimezoneID.getTerminatedBuffer();
262
263     retv_if(tid == NULL, I18N_ERROR_INVALID_PARAMETER);
264     int32_t ulen = u_strlen(tid);
265
266     retv_if(ulen <= 0, I18N_ERROR_INVALID_PARAMETER);
267     *timezone_id = (char*)malloc(ulen+1);
268
269     retv_if(*timezone_id == NULL, I18N_ERROR_OUT_OF_MEMORY);
270     u_austrcpy(*timezone_id, tid);
271
272     return I18N_ERROR_NONE;
273 }
274
275 int i18n_timezone_set_id(i18n_timezone_h timezone, const char *timezone_id)
276 {
277     retv_if(timezone == NULL || timezone_id == NULL, I18N_ERROR_INVALID_PARAMETER);
278     const UnicodeString id(timezone_id);
279     ((TimeZone*)timezone)->setID(id);
280
281     return I18N_ERROR_NONE;
282 }
283
284 int i18n_timezone_get_display_name(i18n_timezone_h timezone, char **display_name)
285 {
286     retv_if(timezone == NULL || display_name == NULL, I18N_ERROR_INVALID_PARAMETER);
287
288     *display_name = NULL;
289     UnicodeString displayName;
290
291     ((TimeZone*)timezone)->getDisplayName(displayName);
292     const UChar * dn = displayName.getTerminatedBuffer();
293
294     retv_if(dn == NULL, I18N_ERROR_INVALID_PARAMETER);
295     int32_t ulen = u_strlen(dn);
296
297     retv_if(ulen <= 0, I18N_ERROR_INVALID_PARAMETER);
298     *display_name = (char*)malloc(ulen + 1);
299
300     retv_if(*display_name == NULL, I18N_ERROR_OUT_OF_MEMORY);
301
302     u_austrcpy(*display_name, dn);
303
304     return I18N_ERROR_NONE;
305 }
306
307 int i18n_timezone_get_display_name_with_locale(i18n_timezone_h timezone, const char *language, const char *country , char **display_name)
308 {
309     retv_if(timezone == NULL || display_name == NULL, I18N_ERROR_INVALID_PARAMETER);
310
311     Locale locale(language, country, 0, 0);
312     *display_name = NULL;
313     UnicodeString displayName;
314
315     ((TimeZone*)timezone)->getDisplayName(locale, displayName);
316     const UChar * dn = displayName.getTerminatedBuffer();
317
318     retv_if(dn == NULL, I18N_ERROR_INVALID_PARAMETER);
319     int32_t ulen = u_strlen(dn);
320
321     retv_if(ulen <= 0, I18N_ERROR_INVALID_PARAMETER);
322     *display_name = (char*)malloc(ulen + 1);
323
324     retv_if(*display_name == NULL, I18N_ERROR_OUT_OF_MEMORY);
325
326     u_austrcpy(*display_name, dn);
327
328     return I18N_ERROR_NONE;
329 }
330
331 int i18n_timezone_get_display_name_with_type(i18n_timezone_h timezone, i18n_ubool daylight, i18n_timezone_display_type_e style,
332     char **display_name)
333 {
334     retv_if(timezone == NULL || display_name == NULL, I18N_ERROR_INVALID_PARAMETER);
335
336     *display_name = NULL;
337     UnicodeString displayName;
338
339     ((TimeZone*)timezone)->getDisplayName((UBool)daylight, (TimeZone::EDisplayType)style, displayName);
340     const UChar * dn = displayName.getTerminatedBuffer();
341
342     retv_if(dn == NULL, I18N_ERROR_INVALID_PARAMETER);
343     int32_t ulen = u_strlen(dn);
344
345     retv_if(ulen <= 0, I18N_ERROR_INVALID_PARAMETER);
346     *display_name = (char*)malloc(ulen + 1);
347
348     retv_if(*display_name == NULL, I18N_ERROR_OUT_OF_MEMORY);
349
350     u_austrcpy(*display_name, dn);
351
352     return I18N_ERROR_NONE;
353 }
354
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)
357 {
358     retv_if(timezone == NULL || display_name == NULL, I18N_ERROR_INVALID_PARAMETER);
359
360     const Locale locale(language, country, 0, 0);
361     *display_name = NULL;
362     UnicodeString displayName;
363
364     ((TimeZone*)timezone)->getDisplayName((UBool)daylight, (TimeZone::EDisplayType)style, locale, displayName);
365     const UChar * dn = displayName.getTerminatedBuffer();
366
367     retv_if(dn == NULL, I18N_ERROR_INVALID_PARAMETER);
368     int32_t ulen = u_strlen(dn);
369
370     retv_if(ulen <= 0, I18N_ERROR_INVALID_PARAMETER);
371     *display_name = (char*)malloc(ulen + 1);
372
373     retv_if(*display_name == NULL, I18N_ERROR_OUT_OF_MEMORY);
374
375     u_austrcpy(*display_name, dn);
376
377     return I18N_ERROR_NONE;
378 }
379
380 int i18n_timezone_use_daylight_time(i18n_timezone_h timezone, i18n_ubool *daylight_time)
381 {
382     retv_if(timezone == NULL || daylight_time == NULL, I18N_ERROR_INVALID_PARAMETER);
383
384     *daylight_time = ((TimeZone*)timezone)->useDaylightTime();
385
386     return I18N_ERROR_NONE;
387 }
388
389 int i18n_timezone_in_daylight_time(i18n_timezone_h timezone, i18n_udate date, i18n_ubool *daylight_time)
390 {
391     retv_if(timezone == NULL || daylight_time == NULL, I18N_ERROR_INVALID_PARAMETER);
392
393     UErrorCode status;
394
395     *daylight_time = ((TimeZone*)timezone)->inDaylightTime(date, status);
396
397     return _i18n_error_mapping(status);
398
399 }
400
401 int i18n_timezone_has_same_rule(i18n_timezone_h timezone, i18n_timezone_h other, i18n_ubool *same_rule)
402 {
403     retv_if(timezone == NULL || same_rule == NULL, I18N_ERROR_INVALID_PARAMETER);
404
405     *same_rule = ((TimeZone*)timezone)->hasSameRules(*(TimeZone*)other);
406
407     return I18N_ERROR_NONE;
408 }
409
410 int i18n_timezone_clone(i18n_timezone_h timezone, i18n_timezone_h *clone)
411 {
412     retv_if(timezone == NULL || clone == NULL, I18N_ERROR_INVALID_PARAMETER);
413
414     *clone = ((TimeZone*)timezone)->clone();
415
416     return I18N_ERROR_NONE;
417 }
418
419 int i18n_timezone_get_dst_savings(i18n_timezone_h timezone, int32_t *dst_savings)
420 {
421     retv_if(timezone == NULL || dst_savings == NULL, I18N_ERROR_INVALID_PARAMETER);
422
423     *dst_savings = ((TimeZone*)timezone)->getDSTSavings();
424
425     return I18N_ERROR_NONE;
426 }