wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Calendar / CalendarUtility.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18
19 #include "CalendarUtility.h"
20 #include <unicode/ucal.h>
21 #include <unicode/ustring.h>
22 #include <unicode/ustdio.h>
23 #include <unicode/udat.h>
24 #include <Logger.h>
25
26 namespace DeviceAPI {
27 namespace Calendar {
28
29 #define ms2sec(ms) (long long int)(ms / 1000.0)
30 #define sec2ms(s) (s * 1000.0)
31
32 CalendarUtility::CalendarUtility()
33 {
34 }
35
36 CalendarUtility::~CalendarUtility()
37 {
38 }
39
40 calendar_time_s CalendarUtility::LLIToCalTime(const char* tzid, const long long int lli)
41 {
42         int y, mon, d;
43         UCalendar *ucal;
44         UErrorCode status = U_ZERO_ERROR;
45
46     calendar_time_s ct = {CALENDAR_TIME_LOCALTIME, {0}};
47
48         UChar *_tzid = NULL;
49
50         if (tzid == NULL)
51         {
52                 tzid = "Etc/Unknown";
53         }
54         _tzid = (UChar*)calloc(strlen(tzid) + 1, sizeof(UChar));
55         if (_tzid == NULL)
56         {
57                 LoggerE("Failed to calloc");
58                 return ct;
59         }
60         u_uastrcpy(_tzid, tzid);
61
62         LoggerD("tzid: "<<tzid);
63
64         ucal = ucal_open(_tzid, u_strlen(_tzid), "en_US", UCAL_TRADITIONAL, &status);
65         if (U_FAILURE(status)) {
66                 LoggerE("ucal_open failed with "<<u_errorName(status));
67                 return ct;
68         }
69
70         ucal_setMillis(ucal, sec2ms(lli), &status);
71         if (U_FAILURE(status)) {
72                 LoggerE("ucal_setMillis failed with "<<u_errorName(status));
73                 return ct;
74         }
75
76         y = ucal_get(ucal, UCAL_YEAR, &status);
77         mon = ucal_get(ucal, UCAL_MONTH, &status) + 1;
78         d = ucal_get(ucal, UCAL_DATE, &status);
79
80         ct.time.date = {y, mon, d};
81
82         LoggerD("y: "<<y<<", mon: "<<mon<<", d: "<<d);
83
84         ucal_close(ucal);
85         if (_tzid) free(_tzid);
86
87         return ct;
88 }
89
90 long long int CalendarUtility::calTimeToLLI(const char* tzid, const calendar_time_s ct)
91 {
92         long long int lli;
93         UCalendar *ucal;
94         UErrorCode status = U_ZERO_ERROR;
95
96         UChar *_tzid = NULL;
97
98         if (tzid == NULL)
99         {
100                 tzid = "Etc/GMT";
101         }
102         _tzid = (UChar*)calloc(strlen(tzid) + 1, sizeof(UChar));
103         if (_tzid == NULL)
104         {
105                 LoggerE("Failed to calloc");
106                 return -1;
107         }
108         u_uastrcpy(_tzid, tzid);
109
110         LoggerD("tzid: "<<tzid);
111
112         ucal = ucal_open(_tzid, u_strlen(_tzid), "en_US", UCAL_TRADITIONAL, &status);
113         if (U_FAILURE(status)) {
114                 LoggerE("ucal_open failed with "<<u_errorName(status));
115                 return -1;
116         }
117
118         ucal_set(ucal, UCAL_YEAR, ct.time.date.year);
119         ucal_set(ucal, UCAL_MONTH, ct.time.date.month -1);
120         ucal_set(ucal, UCAL_DATE, ct.time.date.mday);
121         lli = ms2sec(ucal_getMillis(ucal, &status));
122         ucal_close(ucal);
123         if (_tzid) free(_tzid);
124
125         LoggerD("converted time: "<<lli);
126
127         return lli;
128 }
129
130 } // Calendar
131 } // DeviceAPI