2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "CalendarUtility.h"
20 #include <unicode/ucal.h>
21 #include <unicode/ustring.h>
22 #include <unicode/ustdio.h>
23 #include <unicode/udat.h>
29 #define ms2sec(ms) (long long int)(ms / 1000.0)
30 #define sec2ms(s) (s * 1000.0)
32 CalendarUtility::CalendarUtility()
36 CalendarUtility::~CalendarUtility()
40 calendar_time_s CalendarUtility::LLIToCalTime(const char* tzid, const long long int lli)
44 UErrorCode status = U_ZERO_ERROR;
46 calendar_time_s ct = {CALENDAR_TIME_LOCALTIME, {0}};
54 _tzid = (UChar*)calloc(strlen(tzid) + 1, sizeof(UChar));
57 LoggerE("Failed to calloc");
60 u_uastrcpy(_tzid, tzid);
62 LoggerD("tzid: "<<tzid);
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));
70 ucal_setMillis(ucal, sec2ms(lli), &status);
71 if (U_FAILURE(status)) {
72 LoggerE("ucal_setMillis failed with "<<u_errorName(status));
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);
80 ct.time.date = {y, mon, d};
82 LoggerD("y: "<<y<<", mon: "<<mon<<", d: "<<d);
85 if (_tzid) free(_tzid);
90 long long int CalendarUtility::calTimeToLLI(const char* tzid, const calendar_time_s ct)
94 UErrorCode status = U_ZERO_ERROR;
102 _tzid = (UChar*)calloc(strlen(tzid) + 1, sizeof(UChar));
105 LoggerE("Failed to calloc");
108 u_uastrcpy(_tzid, tzid);
110 LoggerD("tzid: "<<tzid);
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));
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));
123 if (_tzid) free(_tzid);
125 LoggerD("converted time: "<<lli);