apply FSL(Flora Software License)
[apps/home/libug-worldclock-efl.git] / src / worldclock_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 "worldclock_fwk_icu.h"
19 #include "worldclock_types.h"
20 #include "worldclock_dlog.h"
21
22 //char *data_pattern[4] =
23 //{
24 //    "d.MMM.y",
25 //    "MMM.d.y",
26 //    "y.MMM.d",
27 //    "y.d.MMM",
28 //};
29 //char *data_pattern_without_year[4] =
30 //{
31 //    "d MMM",
32 //    "MMM d",
33 //    "MMM d",
34 //    "d MMM",
35 //};
36 char *time_pattern[2] = {
37         "hh:mm a",
38         "HH:mm",
39 };
40
41 //
42 static int __get_pattern_time(char *pattern, int size,
43                               CLK_TIME_FORMAT timeStyle)
44 {
45         retvm_if(!pattern, FAILED, "pattern null");
46         retvm_if(size <= 0, FAILED, "size<=0");
47         snprintf(pattern, size, "%s", time_pattern[timeStyle]);
48         return SUCCESS;
49 }
50
51 //
52 static int __get_pattern_time_and_date(char *pattern, int size,
53                                        CLK_TIME_FORMAT timeStyle,
54                                        CLK_DATE_FORMAT dateStyle)
55 {
56         retvm_if(!pattern, FAILED, "pattern null");
57         retvm_if(size <= 0, FAILED, "size<=0");
58         snprintf(pattern, size, "%s\t%s", "yMMMd", time_pattern[timeStyle]);
59         return SUCCESS;
60 }
61
62 //
63 char *worldclock_icu_get_time_str(time_t input_time, const char *timezone)
64 {
65         static char string_icu_time[BUF_SIZE] = { 0 };
66         char pattern[MAX_PATTERN_LENGHT] = { 0 };
67         CLK_TIME_FORMAT time_format;
68         time_format = clk_fwk_icu_time_format_get();
69         __get_pattern_time(pattern, MAX_PATTERN_LENGHT, time_format);
70         //CLK_INFO_PURPLE("pattern=%s",pattern);
71         clk_fwk_icu_get_data(input_time, string_icu_time, BUF_SIZE, NULL,
72                              pattern, MAX_PATTERN_LENGHT, timezone);
73         // splice time string to result string
74         return string_icu_time;
75 }
76
77 //
78 char *worldclock_icu_get_date_str(time_t input_time, const char *timezone)
79 {
80         static char string_icu_date[BUF_SIZE] = { 0 };
81         // get date string to result string by icu module
82         clk_fwk_icu_get_data(input_time, string_icu_date, BUF_SIZE, NULL,
83                              "MMMd", MAX_PATTERN_LENGHT, timezone);
84         return string_icu_date;
85 }
86
87 //
88 char *worldclock_icu_get_time_date_str(time_t input_time, const char *timezone)
89 {
90         static char string_icu_time_and_date[BUF_SIZE] = { 0 };
91         char pattern[MAX_PATTERN_LENGHT] = { 0 };
92         __get_pattern_time_and_date(pattern, MAX_PATTERN_LENGHT,
93                                     clk_fwk_icu_time_format_get(),
94                                     clk_fwk_icu_date_format_get());
95         clk_fwk_icu_get_data(input_time, string_icu_time_and_date, BUF_SIZE,
96                              NULL, pattern, MAX_PATTERN_LENGHT, timezone);
97         // splice date string & time string to result string
98         return string_icu_time_and_date;
99 }
100
101 /*BEG: Gong Xingsheng<x536.gong@samsung.com> add in Mon Nov 21 01:04:37 PST 2011
102  * this function is used to get worldclock_time_s type date
103  * from icu, get the current hour, minute, second and current am/pm
104  */
105 worldclock_time_s *worldclock_icu_get_time_int(time_t input_time,
106                                                const char *timezone)
107 {
108         static worldclock_time_s worldclock_time;
109         char string[BUF_SIZE] = { 0 };
110         char tmp[3] = { 0 };
111
112         clk_fwk_icu_get_data(input_time, string, BUF_SIZE,
113                              NULL, "HHmmss", MAX_PATTERN_LENGHT, timezone);
114         tmp[0] = string[0];
115         tmp[1] = string[1];
116         worldclock_time.hour = atoi(tmp);
117
118         tmp[0] = string[3];
119         tmp[1] = string[4];
120         worldclock_time.min = atoi(tmp);
121
122         tmp[0] = string[6];
123         tmp[1] = string[7];
124         worldclock_time.sec = atoi(tmp);
125
126         CLK_TIME_FORMAT time_format = clk_fwk_icu_time_format_get();
127         worldclock_time.bAm =
128             IS_EQUAL(time_format, CLK_TIME_FORMAT_12HOUR) ? true : false;
129         return &worldclock_time;
130 }
131
132 /*END: Gong Xingsheng<x536.gong@samsung.com> add in Mon Nov 21 01:04:37 PST 2011 */
133
134 /*BEG: Gong Xingsheng<x536.gong@samsung.com> add in Mon Nov 21 01:04:37 PST 2011
135  * this function is used to get dst value from tz_path by icu
136  */
137 int worldclock_icu_dst_get(const char *tz_path)
138 {
139         retv_if(!tz_path, -1);
140         int dst = 0;
141         UErrorCode status = U_ZERO_ERROR;
142         UChar UPath[BUF_SIZE_64] = { 0 };
143
144         u_uastrcpy(UPath, tz_path);
145
146         dst = ucal_getDSTSavings(UPath, &status);
147         retvm_if(U_FAILURE(status), -1, "error value is %s",
148                  u_errorName(status));
149         dst = dst / 60 / 60 / 1000;
150         return dst;
151 }
152
153 /*END: Gong Xingsheng<x536.gong@samsung.com> add in Mon Nov 21 01:04:37 PST 2011 */