3 Copyright (c) 2000-2012 Samsung Electronics Co., Ltd All Rights Reserved
5 This file is part of org.tizen.efl-calendar
6 Written by Taeho Kang <taeho84.kang@samsung.com>
8 PROPRIETARY/CONFIDENTIAL
10 This software is the confidential and proprietary information of
11 SAMSUNG ELECTRONICS ("Confidential Information"). You shall not
12 disclose such Confidential Information and shall use it only in
13 accordance with the terms of the license agreement you entered
14 into with SAMSUNG ELECTRONICS.
16 SAMSUNG make no representations or warranties about the suitability
17 of the software, either express or implied, including but not limited
18 to the implied warranties of merchantability, fitness for a particular
19 purpose, or non-infringement. SAMSUNG shall not be liable for any
20 damages suffered by licensee as a result of using, modifying or
21 distributing this software or its derivatives.
33 #define LOGFILE "/tmp/calendar.log"
34 #define SIN_TBL_S (sizeof(SIN_TBL)/sizeof(SIN_TBL[0]) - 1)
36 static time_t cal_max;
37 static time_t cal_min;
39 static const float const SIN_TBL[] = {
40 0.0000f, 0.0174f, 0.0349f, 0.0523f, 0.0698f,
41 0.0872f, 0.1045f, 0.1219f, 0.1392f, 0.1564f,
42 0.1736f, 0.1908f, 0.2079f, 0.2249f, 0.2419f,
43 0.2588f, 0.2756f, 0.2924f, 0.3090f, 0.3256f,
44 0.3420f, 0.3584f, 0.3746f, 0.3907f, 0.4067f,
45 0.4226f, 0.4384f, 0.4540f, 0.4695f, 0.4848f,
46 0.5000f, 0.5150f, 0.5299f, 0.5446f, 0.5592f,
47 0.5736f, 0.5878f, 0.6018f, 0.6157f, 0.6293f,
48 0.6528f, 0.6561f, 0.6691f, 0.6820f, 0.6947f,
49 0.7071f, 0.7193f, 0.7314f, 0.7431f, 0.7547f,
50 0.7660f, 0.7772f, 0.7880f, 0.7986f, 0.8090f,
51 0.8191f, 0.8290f, 0.8387f, 0.8480f, 0.8571f,
52 0.8660f, 0.8746f, 0.8829f, 0.8910f, 0.8988f,
53 0.9063f, 0.9135f, 0.9205f, 0.9272f, 0.9336f,
54 0.9397f, 0.9455f, 0.9511f, 0.9563f, 0.9613f,
55 0.9659f, 0.9703f, 0.9744f, 0.9781f, 0.9816f,
56 0.9848f, 0.9877f, 0.9903f, 0.9926f, 0.9945f,
57 0.9962f, 0.9976f, 0.9986f, 0.9994f, 0.9998f,
62 static int __cal_util_max_days[2][12] = {
63 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
64 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
66 int cal_util_get_max_days(int tm_year, int tm_mon)
78 tm_year = tm_year + 1900;
80 return __cal_util_max_days[(!(tm_year & 0x3) && (!(tm_year % 400)
81 || (tm_year % 100)))][tm_mon];
84 static int __cal_util_get_cal_min(void)
103 cal_min = mktime(&tm);
106 c_retv_if(!cal_min, -1);
111 static int __cal_util_get_cal_max(void)
120 localtime_r(&t, &tm);
122 // 2037-12-31 23:59:59
130 cal_max = mktime(&tm);
138 time_t cal_util_get_max_time(void)
140 return __cal_util_get_cal_max();
143 time_t cal_util_get_min_time(void)
145 return __cal_util_get_cal_min();
148 int cal_util_update_tm_year(struct tm *t, int delta)
155 tmp.tm_year += delta;
156 if (tmp.tm_mon == 1 && tmp.tm_mday == 29)
157 tmp.tm_mday = cal_util_get_max_days(tmp.tm_year, tmp.tm_mon);
163 if (r < __cal_util_get_cal_min() || r > __cal_util_get_cal_max())
170 int cal_util_update_tm_month(struct tm *t, int delta)
179 while (tmp.tm_mon < 0) {
184 while (tmp.tm_mon > 11) {
189 max = cal_util_get_max_days(tmp.tm_year, tmp.tm_mon);
190 if (max < tmp.tm_mday)
197 if (r < __cal_util_get_cal_min() || r > __cal_util_get_cal_max())
204 int cal_util_update_tm_day(struct tm *t, int delta)
210 tmp.tm_mday += delta;
216 if (r < __cal_util_get_cal_min() || r > __cal_util_get_cal_max())
223 int cal_util_update_tm_hour(struct tm* tm, int delta)
226 struct tm* returned_tm = NULL;
230 time = time + (delta * 60*60);
232 returned_tm = localtime_r(&time, &t);
235 ERR("localtime is failed.");
238 CAL_MEMCPY(tm, &t, struct tm);
243 int cal_util_get_day_time_t(struct tm *t, time_t *st, time_t *et)
245 c_retvm_if(!t, -1 , "t is null");
246 c_retvm_if(!st, -1 , "st is null");
247 c_retvm_if(!et, -1 , "et is null");
252 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
255 if (_t == (time_t) -1)
259 *et = _t + (24 * 60 * 60) - 1;
264 int cal_util_get_week_time_t(struct tm *t, time_t *st, time_t *et, int start)
266 c_retvm_if(!t, -1 , "t is null");
267 c_retvm_if(!st, -1 , "st is null");
268 c_retvm_if(!et, -1 , "et is null");
273 tm.tm_mday -= CAL_UTIL_GET_WDAY(tm.tm_wday - start);
275 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
278 if (_t == (time_t) -1)
282 *et = _t + (7 * 24 * 60 * 60) - 1;
287 int cal_util_get_month_time_t(struct tm *t, time_t *st, time_t *et)
289 c_retvm_if(!t, -1 , "t is null");
290 c_retvm_if(!st, -1 , "st is null");
291 c_retvm_if(!et, -1 , "et is null");
297 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
300 if (s == (time_t) -1)
305 if (e == (time_t) -1)
313 int cal_util_get_year_time_t(struct tm *t, time_t *st, time_t *et)
315 c_retvm_if(!t, -1 , "t is null");
316 c_retvm_if(!st, -1 , "st is null");
317 c_retvm_if(!et, -1 , "et is null");
324 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
327 if (s == (time_t) -1)
332 if (e == (time_t) -1)
341 float cal_util_nsin(float f)
343 if(f < 0.0 && f > 1.0)
346 return SIN_TBL[(int)(SIN_TBL_S * f)];
349 int cal_util_get_week_flag(char *week_s)
355 if (6 < CAL_STRLEN(week_s)) {
356 for (i = 0; i < 7; i++) {
357 if (week_s[i] == '1')
365 int cal_util_get_timezone_info(char **timezone_path, char **timezone_city, char **timezone_offset)
367 c_retvm_if(!timezone_path, -1, "timezone_path is null");
368 c_retvm_if(!timezone_city, -1, "timezone_city is null");
369 c_retvm_if(!timezone_offset, -1, "timezone_offset is null");
374 int ret = vconf_get_int(CAL_VCONFKEY_LOCK_TIMEZONE_ON_OFF, &value);
375 c_warn_if(ret, "vconf_get_int(CAL_VCONFKEY_LOCK_TIMEZONE_ON_OFF, &value) is failed");
379 text = vconf_get_str(CAL_VCONFKEY_LOCK_TIMEZONE_PATH);
380 c_retvm_if(!text, -1, "vconf_get_str(CAL_VCONFKEY_LOCK_TIMEZONE_PATH) is failed");
383 free(*timezone_path);
385 *timezone_path = strdup(text);
386 c_retvm_if(!*timezone_path, -1, "timezone_path is null");
389 text = vconf_get_str(CAL_VCONFKEY_LOCK_TIMEZONE_CITY);
390 c_retvm_if(!text, -1, "vconf_get_str(CAL_VCONFKEY_LOCK_TIMEZONE_CITY) is failed");
393 free(*timezone_city);
395 *timezone_city = strdup(text);
396 c_retvm_if(!*timezone_city, -1, "timezone_city is null");
399 text = vconf_get_str(CAL_VCONFKEY_LOCK_TIMEZONE_OFFSET);
400 c_retvm_if(!text, -1, "vconf_get_str(CAL_VCONFKEY_LOCK_TIMEZONE_OFFSET) is failed");
402 if (*timezone_offset)
403 free(*timezone_offset);
405 *timezone_offset = g_strdup_printf("GMT%s",text);
406 c_retvm_if(!*timezone_offset, -1, "timezone_offset is null");
410 text = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID);
411 c_retvm_if(!text, -1, "vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID) is failed");
414 free(*timezone_path);
416 *timezone_path = strdup(text);
417 c_retvm_if(!*timezone_path, -1, "timezone_path is null");
420 text = vconf_get_str(VCONFKEY_SETAPPL_CITYNAME_INDEX_INT);
421 c_retvm_if(!text, -1, "vconf_get_str(VCONFKEY_SETAPPL_CITYNAME_INDEX_INT) is failed");
424 free(*timezone_city);
426 *timezone_city = strdup(text);
427 c_retvm_if(!*timezone_city, -1, "timezone_city is null");
429 text = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_INT);
431 ERR("vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_INT) is failed");
435 if (*timezone_offset)
436 free(*timezone_offset);
438 *timezone_offset = g_strdup_printf("GMT%s",text);
439 c_retvm_if(!*timezone_offset, -1, "timezone_offset is null");