2 * Copyright 2012 Samsung Electronics Co., Ltd
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
8 * http://floralicense.org/license/
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.
19 #include <Elementary.h>
24 #include "worldclock.h"
25 #include "worldclock_gmt.h"
26 #include "worldclock_dlog.h"
27 //#include "clock_fwk_widget.h"
30 * Get time zone value from timezone record
32 * @param[in] str given timezone record
34 * @return timezone value
36 static float __worldclock_gmt_get_timezone_value(const char *str)
41 char buf_hour[BUF_MIN_SIZE] = { 0, };
42 char buf_minute[BUF_MIN_SIZE] = { 0, };
44 const char *pbegin = NULL;
45 const char *ppos = NULL;
48 if (str != strstr(str, "GMT")) {
53 ppos = strstr(pbegin, ":");
55 g_strlcpy(buf_hour, pbegin, BUF_MIN_SIZE);
56 result = atof(buf_hour);
58 g_strlcpy(buf_minute, ppos + 1, BUF_MIN_SIZE);
59 result += atof(buf_minute) / 60;
61 g_strlcpy(buf_hour, pbegin, BUF_MIN_SIZE);
62 result = atof(buf_hour);
70 * Get time about given city according to home city & current time
72 * @param[in] home_cs home city
73 * @param[in] input_cs given city
77 time_t worldclock_gmt_get_local_time(const Wcl_CitySet *home_cs,
78 const Wcl_CitySet *input_cs)
81 time_t local_time = 0, out_time = 0;
82 float local_gmt = 0.0, input_gmt = 0.0, dif_gmt = 0.0;
86 local_gmt = __worldclock_gmt_get_timezone_value(home_cs->timezone);
87 if (0.0 == local_gmt) {
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);
92 input_gmt = __worldclock_gmt_get_timezone_value(input_cs->timezone);
93 if (0.0 == input_gmt) {
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);
98 // get diff between given city & home city
99 dif_gmt = input_gmt - local_gmt;
102 local_time = time(NULL);
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);
109 //CLK_INFO("\n\tlocal_gmt(%f); input_gmt(%f); dif_sec(%ld)\n", local_gmt, input_gmt, dif_sec);
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));
121 * Get time string according to given time
123 * @param[in] input_time given time
125 * @return NULL if meet error
126 * string about time with format
128 char *worldclock_gmt_get_time_str(time_t input_time, int time_format)
131 static char time_str[BUF_SIZE] = { 0, };
132 memset(time_str, 0x0, BUF_SIZE);
133 localtime_r(&input_time, &cur_tm);
137 if (-1 == time_format) {
138 ret = vconf_get_int(VCONFKEY_REGIONFORMAT_TIME1224, &value);
139 // if failed, set default date format
141 value = VCONFKEY_TIME_FORMAT_12;
148 case VCONFKEY_TIME_FORMAT_12:
149 strftime(time_str, BUF_SIZE - 1, "%I:%M %p ", &cur_tm);
151 case VCONFKEY_TIME_FORMAT_24:
152 strftime(time_str, BUF_SIZE - 1, "%H:%M", &cur_tm);
161 * Get string of date according to given time
163 * @param[in] input_time given time
165 * @return NULL if meet error
168 char *worldclock_gmt_get_date_str(time_t input_time, int date_format)
171 static char date_str[BUF_SIZE] = { 0, };
172 memset(date_str, 0x0, BUF_SIZE);
173 localtime_r(&input_time, &cur_tm);
177 if (-1 == date_format) {
178 ret = vconf_get_int(VCONFKEY_SETAPPL_DATE_FORMAT_INT, &value);
180 value = SETTING_DATE_FORMAT_DD_MM_YYYY;
187 case SETTING_DATE_FORMAT_DD_MM_YYYY:
188 strftime(date_str, BUF_SIZE - 1, "%d %b", &cur_tm);
190 case SETTING_DATE_FORMAT_MM_DD_YYYY:
191 strftime(date_str, BUF_SIZE - 1, "%b %d", &cur_tm);
193 case SETTING_DATE_FORMAT_YYYY_MM_DD:
194 strftime(date_str, BUF_SIZE - 1, "%b %d", &cur_tm);
196 case SETTING_DATE_FORMAT_YYYY_DD_MM:
197 strftime(date_str, BUF_SIZE - 1, "%d %b", &cur_tm);
207 * Get date & time string according to given time
209 * @param[in] input_time given time
211 * @return NULL if meet error
212 * string about time with format
214 char *worldclock_gmt_get_time_date_str(time_t input_time)
217 static char time_date_string[BUF_SIZE] = { 0 };
219 char time_str[BUF_SIZE] = { 0, };
220 char date_str[BUF_SIZE] = { 0, };
222 memset(time_date_string, 0x0, BUF_SIZE);
224 localtime_r(&input_time, &cur_tm);
227 // get system date format
228 ret = vconf_get_int(VCONFKEY_SETAPPL_DATE_FORMAT_INT, &value);
229 // if failed, set default date format
231 value = SETTING_DATE_FORMAT_DD_MM_YYYY;
233 // get string of date according the format of date
235 case SETTING_DATE_FORMAT_DD_MM_YYYY:
236 strftime(date_str, BUF_SIZE - 1, "%d.%b.%Y", &cur_tm);
238 case SETTING_DATE_FORMAT_MM_DD_YYYY:
239 strftime(date_str, BUF_SIZE - 1, "%b.%d.%Y", &cur_tm);
241 case SETTING_DATE_FORMAT_YYYY_MM_DD:
242 strftime(date_str, BUF_SIZE - 1, "%Y.%b.%d", &cur_tm);
244 case SETTING_DATE_FORMAT_YYYY_DD_MM:
245 strftime(date_str, BUF_SIZE - 1, "%Y.%d.%b", &cur_tm);
251 // get system time format
252 ret = vconf_get_int(VCONFKEY_REGIONFORMAT_TIME1224, &value);
253 // if failed, set default time format
255 value = VCONFKEY_TIME_FORMAT_12;
257 // get string of time according the format of time
259 case VCONFKEY_TIME_FORMAT_12:
260 strftime(time_str, BUF_SIZE - 1, "%l:%M %p ", &cur_tm);
262 case VCONFKEY_TIME_FORMAT_24:
263 strftime(time_str, BUF_SIZE - 1, "%H:%M", &cur_tm);
268 // splice date string & time string to result string
269 snprintf(time_date_string, BUF_SIZE, "%s\t%s", date_str, time_str);
271 return time_date_string;