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