3 * Copyright 2012 Samsung Electronics Co., Ltd
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
9 * http://www.tizenopensource.org/license
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.
20 #include <Elementary.h>
25 #include "worldclock.h"
26 #include "worldclock_gmt.h"
27 #include "worldclock_dlog.h"
28 //#include "clock_fwk_widget.h"
31 * Get time zone value from timezone record
33 * @param[in] str given timezone record
35 * @return timezone value
37 static float __worldclock_gmt_get_timezone_value(const char *str)
42 char buf_hour[BUF_MIN_SIZE] = { 0, };
43 char buf_minute[BUF_MIN_SIZE] = { 0, };
45 const char *pbegin = NULL;
46 const char *ppos = NULL;
49 if (str != strstr(str, "GMT")) {
54 ppos = strstr(pbegin, ":");
56 g_strlcpy(buf_hour, pbegin, BUF_MIN_SIZE);
57 result = atof(buf_hour);
59 g_strlcpy(buf_minute, ppos + 1, BUF_MIN_SIZE);
60 result += atof(buf_minute) / 60;
62 g_strlcpy(buf_hour, pbegin, BUF_MIN_SIZE);
63 result = atof(buf_hour);
71 * Get time about given city according to home city & current time
73 * @param[in] home_cs home city
74 * @param[in] input_cs given city
78 time_t worldclock_gmt_get_local_time(const Wcl_CitySet *home_cs,
79 const Wcl_CitySet *input_cs)
82 time_t local_time = 0, out_time = 0;
83 float local_gmt = 0.0, input_gmt = 0.0, dif_gmt = 0.0;
87 local_gmt = __worldclock_gmt_get_timezone_value(home_cs->timezone);
88 if (0.0 == local_gmt) {
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);
93 input_gmt = __worldclock_gmt_get_timezone_value(input_cs->timezone);
94 if (0.0 == input_gmt) {
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);
99 // get diff between given city & home city
100 dif_gmt = input_gmt - local_gmt;
103 local_time = time(NULL);
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);
110 //CLK_INFO("\n\tlocal_gmt(%f); input_gmt(%f); dif_sec(%ld)\n", local_gmt, input_gmt, dif_sec);
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));
122 * Get time string according to given time
124 * @param[in] input_time given time
126 * @return NULL if meet error
127 * string about time with format
129 char *worldclock_gmt_get_time_str(time_t input_time, int time_format)
132 static char time_str[BUF_SIZE] = { 0, };
133 memset(time_str, 0x0, BUF_SIZE);
134 localtime_r(&input_time, &cur_tm);
138 if (-1 == time_format) {
139 ret = vconf_get_int(VCONFKEY_REGIONFORMAT_TIME1224, &value);
140 // if failed, set default date format
142 value = VCONFKEY_TIME_FORMAT_12;
149 case VCONFKEY_TIME_FORMAT_12:
150 strftime(time_str, BUF_SIZE - 1, "%I:%M %p ", &cur_tm);
152 case VCONFKEY_TIME_FORMAT_24:
153 strftime(time_str, BUF_SIZE - 1, "%H:%M", &cur_tm);
162 * Get string of date according to given time
164 * @param[in] input_time given time
166 * @return NULL if meet error
169 char *worldclock_gmt_get_date_str(time_t input_time, int date_format)
172 static char date_str[BUF_SIZE] = { 0, };
173 memset(date_str, 0x0, BUF_SIZE);
174 localtime_r(&input_time, &cur_tm);
178 if (-1 == date_format) {
179 ret = vconf_get_int(VCONFKEY_SETAPPL_DATE_FORMAT_INT, &value);
181 value = SETTING_DATE_FORMAT_DD_MM_YYYY;
188 case SETTING_DATE_FORMAT_DD_MM_YYYY:
189 strftime(date_str, BUF_SIZE - 1, "%d %b", &cur_tm);
191 case SETTING_DATE_FORMAT_MM_DD_YYYY:
192 strftime(date_str, BUF_SIZE - 1, "%b %d", &cur_tm);
194 case SETTING_DATE_FORMAT_YYYY_MM_DD:
195 strftime(date_str, BUF_SIZE - 1, "%b %d", &cur_tm);
197 case SETTING_DATE_FORMAT_YYYY_DD_MM:
198 strftime(date_str, BUF_SIZE - 1, "%d %b", &cur_tm);
208 * Get date & time string according to given time
210 * @param[in] input_time given time
212 * @return NULL if meet error
213 * string about time with format
215 char *worldclock_gmt_get_time_date_str(time_t input_time)
218 static char time_date_string[BUF_SIZE] = { 0 };
220 char time_str[BUF_SIZE] = { 0, };
221 char date_str[BUF_SIZE] = { 0, };
223 memset(time_date_string, 0x0, BUF_SIZE);
225 localtime_r(&input_time, &cur_tm);
228 // get system date format
229 ret = vconf_get_int(VCONFKEY_SETAPPL_DATE_FORMAT_INT, &value);
230 // if failed, set default date format
232 value = SETTING_DATE_FORMAT_DD_MM_YYYY;
234 // get string of date according the format of date
236 case SETTING_DATE_FORMAT_DD_MM_YYYY:
237 strftime(date_str, BUF_SIZE - 1, "%d.%b.%Y", &cur_tm);
239 case SETTING_DATE_FORMAT_MM_DD_YYYY:
240 strftime(date_str, BUF_SIZE - 1, "%b.%d.%Y", &cur_tm);
242 case SETTING_DATE_FORMAT_YYYY_MM_DD:
243 strftime(date_str, BUF_SIZE - 1, "%Y.%b.%d", &cur_tm);
245 case SETTING_DATE_FORMAT_YYYY_DD_MM:
246 strftime(date_str, BUF_SIZE - 1, "%Y.%d.%b", &cur_tm);
252 // get system time format
253 ret = vconf_get_int(VCONFKEY_REGIONFORMAT_TIME1224, &value);
254 // if failed, set default time format
256 value = VCONFKEY_TIME_FORMAT_12;
258 // get string of time according the format of time
260 case VCONFKEY_TIME_FORMAT_12:
261 strftime(time_str, BUF_SIZE - 1, "%l:%M %p ", &cur_tm);
263 case VCONFKEY_TIME_FORMAT_24:
264 strftime(time_str, BUF_SIZE - 1, "%H:%M", &cur_tm);
269 // splice date string & time string to result string
270 snprintf(time_date_string, BUF_SIZE, "%s\t%s", date_str, time_str);
272 return time_date_string;