diff options
Diffstat (limited to 'mobile_src/Calendar/CalendarUtility.cpp')
-rw-r--r-- | mobile_src/Calendar/CalendarUtility.cpp | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/mobile_src/Calendar/CalendarUtility.cpp b/mobile_src/Calendar/CalendarUtility.cpp new file mode 100644 index 0000000..b1c4c70 --- /dev/null +++ b/mobile_src/Calendar/CalendarUtility.cpp @@ -0,0 +1,131 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + + +#include "CalendarUtility.h" +#include <unicode/ucal.h> +#include <unicode/ustring.h> +#include <unicode/ustdio.h> +#include <unicode/udat.h> +#include <Logger.h> + +namespace DeviceAPI { +namespace Calendar { + +#define ms2sec(ms) (long long int)(ms / 1000.0) +#define sec2ms(s) (s * 1000.0) + +CalendarUtility::CalendarUtility() +{ +} + +CalendarUtility::~CalendarUtility() +{ +} + +calendar_time_s CalendarUtility::LLIToCalTime(const char* tzid, const long long int lli) +{ + int y, mon, d; + UCalendar *ucal; + UErrorCode status = U_ZERO_ERROR; + + calendar_time_s ct = {CALENDAR_TIME_LOCALTIME, {0}}; + + UChar *_tzid = NULL; + + if (tzid == NULL) + { + tzid = "Etc/Unknown"; + } + _tzid = (UChar*)calloc(strlen(tzid) + 1, sizeof(UChar)); + if (_tzid == NULL) + { + LoggerE("Failed to calloc"); + return ct; + } + u_uastrcpy(_tzid, tzid); + + LoggerD("tzid: "<<tzid); + + ucal = ucal_open(_tzid, u_strlen(_tzid), "en_US", UCAL_TRADITIONAL, &status); + if (U_FAILURE(status)) { + LoggerE("ucal_open failed with "<<u_errorName(status)); + return ct; + } + + ucal_setMillis(ucal, sec2ms(lli), &status); + if (U_FAILURE(status)) { + LoggerE("ucal_setMillis failed with "<<u_errorName(status)); + return ct; + } + + y = ucal_get(ucal, UCAL_YEAR, &status); + mon = ucal_get(ucal, UCAL_MONTH, &status) + 1; + d = ucal_get(ucal, UCAL_DATE, &status); + + ct.time.date = {y, mon, d}; + + LoggerD("y: "<<y<<", mon: "<<mon<<", d: "<<d); + + ucal_close(ucal); + if (_tzid) free(_tzid); + + return ct; +} + +long long int CalendarUtility::calTimeToLLI(const char* tzid, const calendar_time_s ct) +{ + long long int lli; + UCalendar *ucal; + UErrorCode status = U_ZERO_ERROR; + + UChar *_tzid = NULL; + + if (tzid == NULL) + { + tzid = "Etc/GMT"; + } + _tzid = (UChar*)calloc(strlen(tzid) + 1, sizeof(UChar)); + if (_tzid == NULL) + { + LoggerE("Failed to calloc"); + return -1; + } + u_uastrcpy(_tzid, tzid); + + LoggerD("tzid: "<<tzid); + + ucal = ucal_open(_tzid, u_strlen(_tzid), "en_US", UCAL_TRADITIONAL, &status); + if (U_FAILURE(status)) { + LoggerE("ucal_open failed with "<<u_errorName(status)); + return -1; + } + + ucal_set(ucal, UCAL_YEAR, ct.time.date.year); + ucal_set(ucal, UCAL_MONTH, ct.time.date.month -1); + ucal_set(ucal, UCAL_DATE, ct.time.date.mday); + lli = ms2sec(ucal_getMillis(ucal, &status)); + ucal_close(ucal); + if (_tzid) free(_tzid); + + LoggerD("converted time: "<<lli); + + return lli; +} + +} // Calendar +} // DeviceAPI |