Tizen souce update
[apps/home/calendar.git] / common / util.c
1 /*
2
3 Copyright (c) 2000-2012 Samsung Electronics Co., Ltd All Rights Reserved
4
5 This file is part of org.tizen.efl-calendar
6 Written by Taeho Kang <taeho84.kang@samsung.com>
7
8 PROPRIETARY/CONFIDENTIAL
9
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.
15
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.
22
23 */
24
25
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <vconf.h>
29
30 #include "cld.h"
31
32 #define LINEMAX 256
33 #define LOGFILE "/tmp/calendar.log"
34 #define SIN_TBL_S (sizeof(SIN_TBL)/sizeof(SIN_TBL[0]) - 1)
35
36 static time_t cal_max;
37 static time_t cal_min;
38
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,
58         1.0f
59 };
60
61
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 }
65 };
66 int cal_util_get_max_days(int tm_year, int tm_mon)
67 {
68         while (tm_mon < 0) {
69                 tm_year -= 1;
70                 tm_mon += 12;
71         }
72
73         while (tm_mon > 11) {
74                 tm_year += 1;
75                 tm_mon -= 12;
76         }
77
78         tm_year = tm_year + 1900;
79
80         return __cal_util_max_days[(!(tm_year & 0x3) && (!(tm_year % 400)
81                                 || (tm_year % 100)))][tm_mon];
82 }
83
84 static int __cal_util_get_cal_min(void)
85 {
86         struct tm tm;
87         time_t t;
88
89         if (cal_min)
90                 return cal_min;
91
92         t = time(NULL);
93         localtime_r(&t, &tm);
94
95         // 1970-1-1 00:00:00
96         tm.tm_year = 70;
97         tm.tm_mon = 0;
98         tm.tm_mday = 1;
99         tm.tm_hour = 0;
100         tm.tm_min = 0;
101         tm.tm_sec = 0;
102
103         cal_min = mktime(&tm);
104         if(cal_min == -1)
105                 cal_min = 0;
106         c_retv_if(!cal_min, -1);
107
108         return cal_min;
109 }
110
111 static int __cal_util_get_cal_max(void)
112 {
113         struct tm tm;
114         time_t t;
115
116         if (cal_max)
117                 return cal_max;
118
119         t = time(NULL);
120         localtime_r(&t, &tm);
121
122         // 2037-12-31 23:59:59
123         tm.tm_year = 137;
124         tm.tm_mon = 11;
125         tm.tm_mday = 31;
126         tm.tm_hour = 23;
127         tm.tm_min = 59;
128         tm.tm_sec = 59;
129
130         cal_max = mktime(&tm);
131         if(cal_max == -1)
132                 cal_max = 0;
133         CAL_ASSERT(cal_max);
134
135         return cal_max;
136 }
137
138 time_t cal_util_get_max_time(void)
139 {
140         return __cal_util_get_cal_max();
141 }
142
143 time_t cal_util_get_min_time(void)
144 {
145         return __cal_util_get_cal_min();
146 }
147
148 int cal_util_update_tm_year(struct tm *t, int delta)
149 {
150         time_t r;
151         struct tm tmp;
152
153         tmp = *t;
154
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);
158
159         r = mktime(&tmp);
160         if (r == (time_t)-1)
161                 return -1;
162
163         if (r < __cal_util_get_cal_min() || r > __cal_util_get_cal_max())
164                 return -1;
165
166         *t = tmp;
167         return 0;
168 }
169
170 int cal_util_update_tm_month(struct tm *t, int delta)
171 {
172         time_t r;
173         int max;
174         struct tm tmp;
175
176         tmp = *t;
177         tmp.tm_mon += delta;
178
179         while (tmp.tm_mon < 0) {
180                 tmp.tm_year -= 1;
181                 tmp.tm_mon += 12;
182         }
183
184         while (tmp.tm_mon > 11) {
185                 tmp.tm_year += 1;
186                 tmp.tm_mon -= 12;
187         }
188
189         max = cal_util_get_max_days(tmp.tm_year, tmp.tm_mon);
190         if (max < tmp.tm_mday)
191                 tmp.tm_mday = max;
192
193         r = mktime(&tmp);
194         if (r == (time_t)-1)
195                 return -1;
196
197         if (r < __cal_util_get_cal_min() || r > __cal_util_get_cal_max())
198                 return -1;
199
200         *t = tmp;
201         return 0;
202 }
203
204 int cal_util_update_tm_day(struct tm *t, int delta)
205 {
206         time_t r;
207         struct tm tmp;
208
209         tmp = *t;
210         tmp.tm_mday += delta;
211
212         r = mktime(&tmp);
213         if (r == (time_t)-1)
214                 return -1;
215
216         if (r < __cal_util_get_cal_min() || r > __cal_util_get_cal_max())
217                 return -1;
218
219         *t = tmp;
220         return 0;
221 }
222
223 int cal_util_update_tm_hour(struct tm* tm, int delta)
224 {
225         struct tm t;
226         struct tm* returned_tm = NULL;
227         time_t time = 0;
228
229         time = mktime(tm);
230         time = time + (delta * 60*60);
231
232         returned_tm = localtime_r(&time, &t);
233         if(!returned_tm)
234         {
235                 ERR("localtime is failed.");
236                 return -1;
237         }
238         CAL_MEMCPY(tm, &t, struct tm);
239
240         return 0;
241 }
242
243 int cal_util_get_day_time_t(struct tm *t, time_t *st, time_t *et)
244 {
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");
248
249         time_t _t;
250         struct tm tm = *t;
251
252         tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
253
254         _t = mktime(&tm);
255         if (_t == (time_t) -1)
256                 return -1;
257
258         *st = _t;
259         *et = _t + (24 * 60 * 60) - 1;
260
261         return 0;
262 }
263
264 int cal_util_get_week_time_t(struct tm *t, time_t *st, time_t *et, int start)
265 {
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");
269
270         time_t _t;
271         struct tm tm = *t;
272
273         tm.tm_mday -= CAL_UTIL_GET_WDAY(tm.tm_wday - start);
274
275         tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
276
277         _t = mktime(&tm);
278         if (_t == (time_t) -1)
279                 return -1;
280
281         *st = _t;
282         *et = _t + (7 * 24 * 60 * 60) - 1;
283
284         return 0;
285 }
286
287 int cal_util_get_month_time_t(struct tm *t, time_t *st, time_t *et)
288 {
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");
292
293         time_t s, e;
294         struct tm tm = *t;
295
296         tm.tm_mday = 1;
297         tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
298
299         s = mktime(&tm);
300         if (s == (time_t) -1)
301                 return -1;
302
303         tm.tm_mon++;
304         e = mktime(&tm);
305         if (e == (time_t) -1)
306                 return -1;
307
308         *st = s;
309         *et = e - 3600;
310         return 0;
311 }
312
313 int cal_util_get_year_time_t(struct tm *t, time_t *st, time_t *et)
314 {
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");
318
319         time_t s, e;
320         struct tm tm = *t;
321
322         tm.tm_mon = 0;
323         tm.tm_mday = 1;
324         tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
325
326         s = mktime(&tm);
327         if (s == (time_t) -1)
328                 return -1;
329
330         tm.tm_year++;
331         e = mktime(&tm);
332         if (e == (time_t) -1)
333                 return -1;
334
335         *st = s;
336         *et = e - 1;
337
338         return 0;
339 }
340
341 float cal_util_nsin(float f)
342 {
343         if(f < 0.0 && f > 1.0)
344                 return 0.0;
345
346         return SIN_TBL[(int)(SIN_TBL_S * f)];
347 }
348
349 int cal_util_get_week_flag(char *week_s)
350 {
351         int i;
352         int flag;
353
354         flag = 0;
355         if (6 < CAL_STRLEN(week_s)) {
356                 for (i = 0; i < 7; i++) {
357                         if (week_s[i] == '1')
358                                 flag |= (1 << i);
359                 }
360         }
361
362         return flag;
363 }
364
365 int cal_util_get_timezone_info(char **timezone_path, char **timezone_city, char **timezone_offset)
366 {
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");
370
371         int value = 0;
372         char *text = NULL;
373
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");
376
377         if (value) {
378
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");
381
382                 if (*timezone_path)
383                         free(*timezone_path);
384
385                 *timezone_path = strdup(text);
386                 c_retvm_if(!*timezone_path, -1, "timezone_path is null");
387
388
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");
391
392                 if (*timezone_city)
393                         free(*timezone_city);
394
395                 *timezone_city = strdup(text);
396                 c_retvm_if(!*timezone_city, -1, "timezone_city is null");
397
398
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");
401
402                 if (*timezone_offset)
403                         free(*timezone_offset);
404
405                 *timezone_offset = g_strdup_printf("GMT%s",text);
406                 c_retvm_if(!*timezone_offset, -1, "timezone_offset is null");
407         }
408         else {
409
410                 text = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID);
411                 c_retvm_if(!text, -1, "vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID) is failed");
412
413                 if (*timezone_path)
414                         free(*timezone_path);
415
416                 *timezone_path = strdup(text);
417                 c_retvm_if(!*timezone_path, -1, "timezone_path is null");
418
419
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");
422
423                 if (*timezone_city)
424                         free(*timezone_city);
425
426                 *timezone_city = strdup(text);
427                 c_retvm_if(!*timezone_city, -1, "timezone_city is null");
428
429                 text = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_INT);
430                 if (!text) {
431                         ERR("vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_INT) is failed");
432                         text = "+9";
433                 }
434
435                 if (*timezone_offset)
436                         free(*timezone_offset);
437
438                 *timezone_offset = g_strdup_printf("GMT%s",text);
439                 c_retvm_if(!*timezone_offset, -1, "timezone_offset is null");
440         }
441
442         return 0;
443 }