apply FSL(Flora Software License)
[apps/home/libug-worldclock-efl.git] / clock-common / src / clock_fwk_icu.c
1 /*
2   * Copyright 2012  Samsung Electronics Co., Ltd
3   * 
4   * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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 __CLK_FWK_ICU_C__
18 #include "clock_fwk_icu.h"
19 #include <vconf.h>
20 #include <unicode/udatpg.h>
21 #include <unicode/ucnv.h>
22 #include <unistd.h>
23 //
24 CLK_DATE_FORMAT clk_fwk_icu_date_format_get()
25 {
26         CLK_DATE_FORMAT dateStyle = CLK_DATE_FORMAT_DD_MM_YYYY;
27         int value = 0;
28         int ret = -1;
29         ret = vconf_get_int(VCONFKEY_SETAPPL_DATE_FORMAT_INT, &value);
30         if (-1 == ret) {
31                 value = SETTING_DATE_FORMAT_DD_MM_YYYY;
32         }
33         switch (value) {
34         case SETTING_DATE_FORMAT_DD_MM_YYYY:
35                 dateStyle = CLK_DATE_FORMAT_DD_MM_YYYY;
36                 break;
37         case SETTING_DATE_FORMAT_MM_DD_YYYY:
38                 dateStyle = CLK_DATE_FORMAT_MM_DD_YYYY;
39                 break;
40         case SETTING_DATE_FORMAT_YYYY_MM_DD:
41                 dateStyle = CLK_DATE_FORMAT_YYYY_MM_DD;
42                 break;
43         case SETTING_DATE_FORMAT_YYYY_DD_MM:
44                 dateStyle = CLK_DATE_FORMAT_YYYY_DD_MM;
45                 break;
46         default:
47                 break;
48         }
49         return dateStyle;
50 }
51
52 //
53 CLK_TIME_FORMAT clk_fwk_icu_time_format_get()
54 {
55         CLK_TIME_FORMAT time_format = CLK_TIME_FORMAT_12HOUR;
56         int ret = 0;
57         int value = 0;
58         ret = vconf_get_int(VCONFKEY_REGIONFORMAT_TIME1224, &value);
59         // if failed, set default time format
60         if (-1 == ret) {
61                 value = VCONFKEY_TIME_FORMAT_12;
62         }
63         switch (value) {
64         case VCONFKEY_TIME_FORMAT_12:
65                 time_format = CLK_TIME_FORMAT_12HOUR;
66                 break;
67         case VCONFKEY_TIME_FORMAT_24:
68                 time_format = CLK_TIME_FORMAT_24HOUR;
69                 break;
70         default:
71                 break;
72         }
73         return time_format;
74 }
75
76 //
77 int clk_fwk_icu_get_data(time_t intime, char *dstStr, int dstStr_num, char *loc,
78                          char *pattern, int pattern_num, const char *timezone)
79 {
80         int ret = SUCCESS;
81         int nErr = SUCCESS;
82         UDateFormat *date_fmt = NULL;
83         UErrorCode status = U_ZERO_ERROR;
84         const char *locale = timezone;
85         UDate tmpdate;
86         UChar *tmpstr = NULL;
87         int32_t tmplen = 0;
88         UChar bestPattern[MAX_PATTERN_LENGHT] = { 0, };
89         int32_t bestPatternCapacity;
90         int32_t bestPatternLength;
91         UDateTimePatternGenerator *generator = NULL;
92
93         /*CLK_FUN_BEG(); */
94         CLK_RETVM_IF(!dstStr, FAILED, "str NULL");
95         CLK_RETVM_IF(dstStr_num <= 0, FAILED, "dstStr_num<=0");
96         CLK_RETVM_IF(!pattern, FAILED, "pattern NULL");
97         CLK_RETVM_IF(pattern_num <= 0, FAILED, "pattern_num<=0");
98         //
99         //date = ucal_getNow();
100         tmpdate = (double)1000 *intime;
101         uloc_setDefault((const char *)__secure_getenv("LC_TIME"), &status);
102         locale = loc ? loc : uloc_getDefault();
103         CLK_RETVM_IF(!locale, FAILED, "locale NULL");
104
105         UChar timezoneID[MAX_TIMEZONE_LENGHT] = { 0 };
106         if (timezone) {
107                 u_uastrncpy(timezoneID, timezone, MAX_TIMEZONE_LENGHT);
108         }
109         //set pattern
110         UChar upattern[MAX_PATTERN_LENGHT] = { 0 };
111         u_uastrncpy(upattern, pattern, MAX_PATTERN_LENGHT);
112         // generate best pattern for locale region
113         generator = udatpg_open(locale, &status);
114         bestPatternCapacity =
115             (int32_t) (sizeof(bestPattern) / sizeof((bestPattern)[0]));
116         bestPatternLength =
117             udatpg_getBestPattern(generator, upattern, u_strlen(upattern),
118                                   bestPattern, bestPatternCapacity, &status);
119
120         if (timezone) {
121                 date_fmt =
122                     udat_open(UDAT_IGNORE, UDAT_DEFAULT, locale, timezoneID, -1,
123                               bestPattern, bestPatternLength, &status);
124         } else {
125                 date_fmt =
126                     udat_open(UDAT_IGNORE, UDAT_DEFAULT, locale, NULL, -1,
127                               bestPattern, bestPatternLength, &status);
128         }
129         CLK_RETVM_IF(!date_fmt, FAILED, "udat_open failed");
130         //frist, get len
131         tmplen = udat_format(date_fmt, tmpdate, NULL, tmplen, NULL, &status);
132         CLK_RETVM_IF(IS_EQUAL(0, tmplen), FAILED, "tmplen=0");
133         //then,malloc
134         if (IS_EQUAL(status, U_BUFFER_OVERFLOW_ERROR)) {
135                 status = U_ZERO_ERROR;
136                 tmpstr = (UChar *) malloc(sizeof(UChar) * (tmplen + 1));
137                 CLK_RETVM_IF(NULL_CHECK(tmpstr), FAILED, "malloc failed");
138
139                 udat_format(date_fmt, tmpdate, tmpstr, tmplen + 1, NULL,
140                             &status);
141         }
142         //
143         dstStr = u_austrncpy(dstStr, tmpstr, dstStr_num);
144         CLK_INFO_PURPLE("result:%s", dstStr);
145         ret = status;
146  End:
147         if (generator) {
148                 udatpg_close(generator);
149         }
150         if (date_fmt) {
151                 udat_close(date_fmt);
152         }
153         FREEIF(tmpstr);
154         /*CLK_FUN_END(); */
155         return ret;
156 }
157
158 int clk_timezone_get(char *dst, size_t number)
159 {
160         retv_if(!dst || number < 1, FAILED);
161         UChar timezoneID[MAX_TIMEZONE_LENGHT] = { 0 };
162         UErrorCode status = U_ZERO_ERROR;
163         ucal_getDefaultTimeZone(timezoneID, MAX_TIMEZONE_LENGHT, &status);
164         retv_if(status != SUCCESS, FAILED);
165
166         dst = u_austrncpy(dst, timezoneID, number);
167         CLK_INFO_PURPLE("default timezone is %s: ", dst);
168         return SUCCESS;
169 }
170
171 void clk_timezone_update()
172 {
173         UErrorCode status = U_ZERO_ERROR;
174         UChar uOlsonID[BUF_SIZE_64] = { 0 };
175
176         char *timezone = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID);
177         ret_if(!timezone);
178         CLK_INFO_PURPLE("current timezone is :  %s", timezone);
179         u_uastrcpy(uOlsonID, timezone);
180         ucal_setDefaultTimeZone(uOlsonID, &status);
181
182         free(timezone);
183         retm_if(U_FAILURE(status), "error value is %s", u_errorName(status));
184 }