upload codes for TIZEN 2.0
[apps/home/clock.git] / worldclock / src / worldclock_gmt.c
1 /*
2 *
3 * Copyright 2012  Samsung Electronics Co., Ltd
4 *
5 * Licensed under the Flora License, Version 1.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *    http://www.tizenopensource.org/license
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <stdio.h>
20 #include <Elementary.h>
21 #include <string.h>
22
23 #include <vconf.h>
24
25 #include "worldclock.h"
26 #include "worldclock_gmt.h"
27 #include "worldclock_dlog.h"
28 //#include "clock_fwk_widget.h"
29
30 /**
31  * Get time zone value from timezone record
32  *
33  * @param[in]  str  given timezone record
34  *
35  * @return    timezone value
36  */
37 static float __worldclock_gmt_get_timezone_value(const char *str)
38 {
39         CLK_FUN_DEBUG_BEG();
40         retv_if(!str, 0.0);
41
42         char buf_hour[BUF_MIN_SIZE] = { 0, };
43         char buf_minute[BUF_MIN_SIZE] = { 0, };
44
45         const char *pbegin = NULL;
46         const char *ppos = NULL;
47         double result = 0.0;
48
49         if (str != strstr(str, "GMT")) {
50                 pbegin = str;
51         } else {
52                 pbegin = str + 3;
53         }
54         ppos = strstr(pbegin, ":");
55         if (ppos) {
56                 g_strlcpy(buf_hour, pbegin, BUF_MIN_SIZE);
57                 result = atof(buf_hour);
58
59                 g_strlcpy(buf_minute, ppos + 1, BUF_MIN_SIZE);
60                 result += atof(buf_minute) / 60;
61         } else {
62                 g_strlcpy(buf_hour, pbegin, BUF_MIN_SIZE);
63                 result = atof(buf_hour);
64         }
65
66         CLK_FUN_DEBUG_END();
67         return result;
68 }
69
70 /**
71  * Get time about given city according to home city & current time
72  *
73  * @param[in]  home_cs    home city
74  * @param[in]  input_cs   given city
75  *
76  * @return    gmt time
77  */
78 time_t worldclock_gmt_get_local_time(const Wcl_CitySet *home_cs,
79                                      const Wcl_CitySet *input_cs)
80 {
81         CLK_FUN_DEBUG_BEG();
82         time_t local_time = 0, out_time = 0;
83         float local_gmt = 0.0, input_gmt = 0.0, dif_gmt = 0.0;
84         time_t dif_sec = 0;
85
86         // get timezone
87         local_gmt = __worldclock_gmt_get_timezone_value(home_cs->timezone);
88         if (0.0 == local_gmt) {
89                 CLK_INFO
90                     ("\n\thome_cs->city(%s); home_cs->timezone(%s); home_cs->dst(%d)\n",
91                      home_cs->city, home_cs->timezone, home_cs->dst);
92         }
93         input_gmt = __worldclock_gmt_get_timezone_value(input_cs->timezone);
94         if (0.0 == input_gmt) {
95                 CLK_INFO
96                     ("\n\tinput_cs->city(%s); input_cs->timezone(%s); input_cs->dst(%d)\n",
97                      input_cs->city, input_cs->timezone, input_cs->dst);
98         }
99         // get diff between given city & home city
100         dif_gmt = input_gmt - local_gmt;
101
102         // get local time
103         local_time = time(NULL);
104
105         // calculate time difference between the two
106         dif_sec = (int)(dif_gmt * 60) * 60;
107         if (home_cs->dst != input_cs->dst) {
108                 dif_sec += 60 * 60 * (input_cs->dst - home_cs->dst);
109         }
110         //CLK_INFO("\n\tlocal_gmt(%f); input_gmt(%f); dif_sec(%ld)\n", local_gmt, input_gmt, dif_sec);
111
112         out_time = (dif_sec + local_time);
113         //CLK_INFO("local_time(%s)", worldclock_icu_get_time_str(local_time));
114         //CLK_INFO("out_time(%s)", worldclock_icu_get_time_str(out_time));
115         //CLK_INFO("local_time(%s)", worldclock_gmt_get_time_str(local_time, -1));
116         //CLK_INFO("out_time(%s)", worldclock_gmt_get_time_str(out_time, -1));
117         CLK_FUN_DEBUG_END();
118         return out_time;
119 }
120
121 /**
122  * Get time string according to given time
123  *
124  * @param[in]  input_time   given time
125  *
126  * @return     NULL if meet error
127  *             string about time with format
128  */
129 char *worldclock_gmt_get_time_str(time_t input_time, int time_format)
130 {
131         struct tm cur_tm;
132         static char time_str[BUF_SIZE] = { 0, };
133         memset(time_str, 0x0, BUF_SIZE);
134         localtime_r(&input_time, &cur_tm);
135         int value = -1;
136         int ret = -1;
137
138         if (-1 == time_format) {
139                 ret = vconf_get_int(VCONFKEY_REGIONFORMAT_TIME1224, &value);
140                 // if failed, set default date format
141                 if (-1 == ret) {
142                         value = VCONFKEY_TIME_FORMAT_12;
143                 }
144         } else {
145                 value = time_format;
146         }
147
148         switch (value) {
149         case VCONFKEY_TIME_FORMAT_12:
150                 strftime(time_str, BUF_SIZE - 1, "%I:%M %p ", &cur_tm);
151                 break;
152         case VCONFKEY_TIME_FORMAT_24:
153                 strftime(time_str, BUF_SIZE - 1, "%H:%M", &cur_tm);
154                 break;
155         default:
156                 return NULL;
157         }
158         return time_str;
159 }
160
161 /**
162  * Get string of date according to given time
163  *
164  * @param[in]  input_time   given time
165  *
166  * @return     NULL if meet error
167  *             String of data
168  */
169 char *worldclock_gmt_get_date_str(time_t input_time, int date_format)
170 {
171         struct tm cur_tm;
172         static char date_str[BUF_SIZE] = { 0, };
173         memset(date_str, 0x0, BUF_SIZE);
174         localtime_r(&input_time, &cur_tm);
175         int value = -1;
176         int ret = -1;
177
178         if (-1 == date_format) {
179                 ret = vconf_get_int(VCONFKEY_SETAPPL_DATE_FORMAT_INT, &value);
180                 if (-1 == ret) {
181                         value = SETTING_DATE_FORMAT_DD_MM_YYYY;
182                 }
183         } else {
184                 value = date_format;
185         }
186
187         switch (value) {
188         case SETTING_DATE_FORMAT_DD_MM_YYYY:
189                 strftime(date_str, BUF_SIZE - 1, "%d %b", &cur_tm);
190                 break;
191         case SETTING_DATE_FORMAT_MM_DD_YYYY:
192                 strftime(date_str, BUF_SIZE - 1, "%b %d", &cur_tm);
193                 break;
194         case SETTING_DATE_FORMAT_YYYY_MM_DD:
195                 strftime(date_str, BUF_SIZE - 1, "%b %d", &cur_tm);
196                 break;
197         case SETTING_DATE_FORMAT_YYYY_DD_MM:
198                 strftime(date_str, BUF_SIZE - 1, "%d %b", &cur_tm);
199                 break;
200         default:
201                 return NULL;
202         }
203
204         return date_str;
205 }
206
207 /**
208  * Get date & time string according to given time
209  *
210  * @param[in]  input_time   given time
211  *
212  * @return     NULL if meet error
213  *             string about time with format
214  */
215 char *worldclock_gmt_get_time_date_str(time_t input_time)
216 {
217         //CLK_FUN_BEG();
218         static char time_date_string[BUF_SIZE] = { 0 };
219         struct tm cur_tm;
220         char time_str[BUF_SIZE] = { 0, };
221         char date_str[BUF_SIZE] = { 0, };
222         // reset string
223         memset(time_date_string, 0x0, BUF_SIZE);
224         // get local time
225         localtime_r(&input_time, &cur_tm);
226         int value = -1;
227         int ret = -1;
228         // get system date format
229         ret = vconf_get_int(VCONFKEY_SETAPPL_DATE_FORMAT_INT, &value);
230         // if failed, set default date format
231         if (-1 == ret) {
232                 value = SETTING_DATE_FORMAT_DD_MM_YYYY;
233         }
234         // get string of date according the format of date
235         switch (value) {
236         case SETTING_DATE_FORMAT_DD_MM_YYYY:
237                 strftime(date_str, BUF_SIZE - 1, "%d.%b.%Y", &cur_tm);
238                 break;
239         case SETTING_DATE_FORMAT_MM_DD_YYYY:
240                 strftime(date_str, BUF_SIZE - 1, "%b.%d.%Y", &cur_tm);
241                 break;
242         case SETTING_DATE_FORMAT_YYYY_MM_DD:
243                 strftime(date_str, BUF_SIZE - 1, "%Y.%b.%d", &cur_tm);
244                 break;
245         case SETTING_DATE_FORMAT_YYYY_DD_MM:
246                 strftime(date_str, BUF_SIZE - 1, "%Y.%d.%b", &cur_tm);
247                 break;
248         default:
249                 return NULL;
250         }
251
252         // get system time format
253         ret = vconf_get_int(VCONFKEY_REGIONFORMAT_TIME1224, &value);
254         // if failed, set default time format
255         if (-1 == ret) {
256                 value = VCONFKEY_TIME_FORMAT_12;
257         }
258         // get string of time according the format of time
259         switch (value) {
260         case VCONFKEY_TIME_FORMAT_12:
261                 strftime(time_str, BUF_SIZE - 1, "%l:%M %p ", &cur_tm);
262                 break;
263         case VCONFKEY_TIME_FORMAT_24:
264                 strftime(time_str, BUF_SIZE - 1, "%H:%M", &cur_tm);
265                 break;
266         default:
267                 return NULL;
268         }
269         // splice date string & time string to result string
270         snprintf(time_date_string, BUF_SIZE, "%s\t%s", date_str, time_str);
271
272         return time_date_string;
273 }