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.
23 #include "TimeUtilTools.h"
24 #include <unicode/unistr.h>
25 #include <unicode/timezone.h>
26 #include <unicode/calendar.h>
27 #include <unicode/ucal.h>
28 #include <unicode/vtzone.h>
29 #include <unicode/smpdtfmt.h>
31 #ifdef IMPL_BACKWARD_COMPATIBLE
32 #include <Commons/Exception.h>
34 using namespace WrtDeviceApis;
35 #endif // IMPL_BACKWARD_COMPATIBLE
42 UErrorCode ec = U_ZERO_ERROR;
44 m_calendar = Calendar::createInstance(ec);
47 LOGD("Calendar created successfully");
49 LOGE("Failed to create calendar: %d, %s", ec, u_errorName(ec));
53 TZDate::TZDate(time_t datetime, std::string timezone)
55 UErrorCode ec = U_ZERO_ERROR;
56 long local_offset = 0;
57 std::string local_timezone = TimeUtilTools::getLocalTimeZone();
58 UnicodeString* usTimezone = TimeUtilTools::toUnicodeString(local_timezone);
59 TimeZone* tz = TimeZone::createTimeZone(*usTimezone);
61 local_offset = tz->getRawOffset();
62 if (timezone.empty() || !TimeUtilTools::isInTimezonesArray(timezone)) {
63 timezone = TimeUtilTools::getLocalTimeZone();
66 usTimezone = TimeUtilTools::toUnicodeString(timezone);
67 tz = TimeZone::createTimeZone(*usTimezone);
71 datetime = time(NULL);
73 datetime += local_offset / 1000;
75 gettimeofday(&tp, NULL);
76 struct tm *date = gmtime(&datetime);
77 m_calendar = Calendar::createInstance(*tz, ec);
80 m_calendar->set(TimeUtilTools::toint32_t(date->tm_year + 1900),
81 TimeUtilTools::toint32_t(date->tm_mon),
82 TimeUtilTools::toint32_t(date->tm_mday), TimeUtilTools::toint32_t(date->tm_hour),
83 TimeUtilTools::toint32_t(date->tm_min), TimeUtilTools::toint32_t(date->tm_sec));
84 m_calendar->set(UCAL_MILLISECOND, TimeUtilTools::toint32_t(tp.tv_usec / 1000));
88 LOGE("Calendar doesn't created: %d, %s", ec, u_errorName(ec));
93 TZDate::TZDate(long year,
100 std::string timezone)
102 UErrorCode ec = U_ZERO_ERROR;
103 if (timezone.empty() || !TimeUtilTools::isInTimezonesArray(timezone)) {
104 timezone = TimeUtilTools::getLocalTimeZone();
106 std::shared_ptr<UnicodeString> usTimezone(TimeUtilTools::toUnicodeString(timezone));
107 TimeZone* tz = TimeZone::createTimeZone(*usTimezone);
108 m_calendar = Calendar::createInstance(*tz, ec);
111 m_calendar->set(TimeUtilTools::toint32_t(year), TimeUtilTools::toint32_t(month),
112 TimeUtilTools::toint32_t(day), TimeUtilTools::toint32_t(hours),
113 TimeUtilTools::toint32_t(minutes), TimeUtilTools::toint32_t(seconds));
114 m_calendar->set(UCAL_MILLISECOND, TimeUtilTools::toint32_t(milliseconds));
118 LOGE("Calendar doesn't created: %d, %s", ec, u_errorName(ec));
122 TZDate::TZDate(const TZDate& tzdate) {
123 m_calendar = tzdate.m_calendar->clone();
126 TZDate::TZDate(Calendar* cal) {
130 TZDate& TZDate::operator =( const TZDate& tzdate)
132 if (this != &tzdate) {
134 m_calendar = tzdate.m_calendar->clone();
148 long TZDate::getDate() const {
149 UErrorCode ec = U_ZERO_ERROR;
150 return TimeUtilTools::tolong(m_calendar->get(UCAL_DATE, ec));
153 void TZDate::setDate(long date) {
154 m_calendar->set(UCAL_DATE, TimeUtilTools::toint32_t(date));
157 long TZDate::getDay() const {
158 UErrorCode ec = U_ZERO_ERROR;
159 return TimeUtilTools::tolong(m_calendar->get(UCAL_DAY_OF_WEEK, ec)) - 1; // -1 because enums values begins from 1
162 long TZDate::getFullYear() const {
163 UErrorCode ec = U_ZERO_ERROR;
164 return TimeUtilTools::tolong(m_calendar->get(UCAL_YEAR, ec));
167 void TZDate::setFullYear(long year) {
171 m_calendar->set(UCAL_YEAR, TimeUtilTools::toint32_t(year));
174 long TZDate::getHours() const {
175 UErrorCode ec = U_ZERO_ERROR;
176 return TimeUtilTools::tolong(m_calendar->get(UCAL_HOUR_OF_DAY, ec));
179 void TZDate::setHours(long hours) {
180 m_calendar->set(UCAL_HOUR_OF_DAY, TimeUtilTools::toint32_t(hours));
183 long TZDate::getMilliseconds() const {
184 UErrorCode ec = U_ZERO_ERROR;
185 return TimeUtilTools::tolong(m_calendar->get(UCAL_MILLISECOND, ec));
188 void TZDate::setMilliseconds(long ms) {
189 m_calendar->set(UCAL_MILLISECOND, TimeUtilTools::toint32_t(ms));
192 long TZDate::getMinutes() const {
193 UErrorCode ec = U_ZERO_ERROR;
194 return TimeUtilTools::tolong(m_calendar->get(UCAL_MINUTE, ec));
197 void TZDate::setMinutes(long minutes) {
198 m_calendar->set(UCAL_MINUTE, TimeUtilTools::toint32_t(minutes));
201 long TZDate::getMonth() const {
202 UErrorCode ec = U_ZERO_ERROR;
203 return TimeUtilTools::tolong(m_calendar->get(UCAL_MONTH, ec));
206 void TZDate::setMonth(long month) {
207 m_calendar->set(UCAL_MONTH, TimeUtilTools::toint32_t(month));
210 long TZDate::getSeconds() const {
211 UErrorCode ec = U_ZERO_ERROR;
212 return m_calendar->get(UCAL_SECOND, ec);
215 void TZDate::setSeconds(long seconds) {
216 m_calendar->set(UCAL_SECOND, TimeUtilTools::toint32_t(seconds));
219 long TZDate::getUTCDate() const {
220 UErrorCode ec = U_ZERO_ERROR;
221 Calendar* cal = getUTCCalendar();
222 int32_t value = cal->get(UCAL_DATE, ec);
223 if (!U_SUCCESS(ec)) {
224 LOGE("Can't get date from cal: %d, %s", ec, u_errorName(ec));
225 throw Common::UnknownException("Can't get date from cal");
231 return TimeUtilTools::tolong(value);
234 void TZDate::setUTCDate(long date) {
235 long val = getUTCDate();
236 setDate(getDate() + date - val);
239 long TZDate::getUTCDay() const {
240 UErrorCode ec = U_ZERO_ERROR;
241 Calendar* cal = getUTCCalendar();
242 int32_t value = cal->get(UCAL_DAY_OF_WEEK, ec);
243 if (!U_SUCCESS(ec)) {
244 LOGE("Can't get day from cal: %d, %s", ec, u_errorName(ec));
245 throw Common::UnknownException("Can't get day from cal");
251 return TimeUtilTools::tolong(value-1); // -1 because enums values begins from 1
254 long TZDate::getUTCFullYear() const {
255 UErrorCode ec = U_ZERO_ERROR;
256 Calendar* cal = getUTCCalendar();
257 int32_t value = cal->get(UCAL_YEAR, ec);
258 if (!U_SUCCESS(ec)) {
259 LOGE("Can't get year from cal: %d, %s", ec, u_errorName(ec));
260 throw Common::UnknownException("Can't get year from cal");
266 return TimeUtilTools::tolong(value);
269 void TZDate::setUTCFullYear(long year) {
270 long val = getUTCFullYear();
271 setFullYear(getFullYear() + year - val);
274 long TZDate::getUTCHours() const {
275 UErrorCode ec = U_ZERO_ERROR;
276 Calendar* cal = getUTCCalendar();
277 int32_t value = cal->get(UCAL_HOUR_OF_DAY, ec);
278 if (!U_SUCCESS(ec)) {
279 LOGE("Can't get hours from cal: %d, %s", ec, u_errorName(ec));
280 throw Common::UnknownException("Can't get hours from cal");
286 return TimeUtilTools::tolong(value);
289 void TZDate::setUTCHours(long hours) {
290 long val = getUTCHours();
291 setHours(getHours() + hours - val);
294 long TZDate::getUTCMilliseconds() const {
295 return getMilliseconds();
298 void TZDate::setUTCMilliseconds(long ms) {
302 long TZDate::getUTCMinutes() const {
306 void TZDate::setUTCMinutes(long minutes) {
310 long TZDate::getUTCMonth() const {
311 UErrorCode ec = U_ZERO_ERROR;
312 Calendar* cal = getUTCCalendar();
313 int32_t value = cal->get(UCAL_MONTH, ec);
314 if (!U_SUCCESS(ec)) {
315 LOGE("Can't get month from cal: %d, %s", ec, u_errorName(ec));
316 throw Common::UnknownException("Can't get month from cal");
322 return TimeUtilTools::tolong(value);
325 void TZDate::setUTCMonth(long month) {
326 long val = getUTCMonth();
327 setMonth(getMonth() + month - val);
330 long TZDate::getUTCSeconds() const {
334 void TZDate::setUTCSeconds(long seconds) {
338 std::string TZDate::getTimezone() const {
341 m_calendar->getTimeZone().getID(id);
342 return TimeUtilTools::toUTF8String(id);
345 TZDatePtr TZDate::toTimezone(const std::string& tzid) const {
347 if (TimeUtilTools::isInTimezonesArray(tzid)) {
348 newObj = TZDatePtr(new (std::nothrow) TZDate(getCalendar(tzid)));
350 LOGE("Object not created");
351 throw Common::UnknownException("Object not created");
354 LOGE("Invalid timezone");
355 throw Common::InvalidValuesException("Invalid timezone");
360 TZDatePtr TZDate::toLocalTimezone() const {
361 std::string localTimezone = TimeUtilTools::getLocalTimeZone();
362 return toTimezone(localTimezone);
365 TZDatePtr TZDate::toUTC() const {
366 TZDatePtr newObj = TZDatePtr(new (std::nothrow) TZDate(getUTCCalendar()));
368 LOGE("Object not created");
369 throw Common::UnknownException("Object not created");
374 TimeDurationPtr TZDate::difference(TZDatePtr other) const {
375 TimeDurationPtr dur = TimeDurationPtr(new (std::nothrow) TimeDuration());
376 UDate thisTime, otherTime, diffTime;
378 UErrorCode ec = U_ZERO_ERROR;
379 TZDatePtr newObj = other;
381 diffTime = m_calendar->getTime(ec) - newObj->m_calendar->getTime(ec);
383 if (fabs(diffTime) > LLONG_MAX) {
384 LOGE("limit value reached");
385 throw Common::UnknownException("limit value reached");
387 len = static_cast<long long>(diffTime);
389 if (len % TIME_DAYS_UNIT == 0) {
390 len = len / TIME_DAYS_UNIT;
398 bool TZDate::equalsTo(TZDatePtr other) const {
399 TimeDurationPtr dur = difference(other);
400 return !dur->getLength();
403 bool TZDate::earlierThan(TZDatePtr other) const {
404 TimeDurationPtr dur = difference(other);
405 return dur->getLength() < 0;
408 bool TZDate::laterThan(TZDatePtr other) const {
409 TimeDurationPtr dur = difference(other);
410 return dur->getLength() > 0;
413 TZDatePtr TZDate::addDuration(TimeDurationPtr duration) const {
414 TZDatePtr newObj = TZDatePtr(new (std::nothrow) TZDate());
415 TimeDuration::findTheBiggestPossibleUnit(duration);
416 long long len = duration->getLength();
417 std::string unit = duration->getUnit();
419 UErrorCode ec = U_ZERO_ERROR;
420 newObj->m_calendar = m_calendar->clone();
423 newObj->m_calendar->add(UCAL_MILLISECOND,
424 TimeUtilTools::toint32_t(static_cast<long>(len)), ec);
425 } else if (SECS == unit) {
426 newObj->m_calendar->add(UCAL_SECOND,
427 TimeUtilTools::toint32_t(static_cast<long>(len)), ec);
428 } else if (MINS == unit) {
429 newObj->m_calendar->add(UCAL_MINUTE,
430 TimeUtilTools::toint32_t(static_cast<long>(len)), ec);
431 } else if (HOURS == unit) {
432 newObj->m_calendar->add(UCAL_HOUR_OF_DAY,
433 TimeUtilTools::toint32_t(static_cast<long>(len)), ec);
434 } else if (DAYS == unit) {
435 newObj->m_calendar->add(UCAL_DATE,
436 TimeUtilTools::toint32_t(static_cast<long>(len)), ec);
441 std::string TZDate::toLocaleDateString() const {
442 return toString(true, TimeUtilTools::DATE_FORMAT);
445 std::string TZDate::toLocaleTimeString() const {
446 return toString(true, TimeUtilTools::TIME_FORMAT);
449 std::string TZDate::toLocaleString() const {
450 return toString(true);
453 std::string TZDate::toDateString() const {
454 return toString(false, TimeUtilTools::DATE_FORMAT);
457 std::string TZDate::toTimeString() const {
458 return toString(false, TimeUtilTools::TIME_FORMAT);
461 std::string TZDate::toString(bool bLocale,
462 TimeUtilTools::DateTimeFormatType type) const {
463 UErrorCode ec = U_ZERO_ERROR;
466 Locale* defaultLocale = TimeUtilTools::getDefaultLocale();
467 DateFormat* fmt = new SimpleDateFormat(TimeUtilTools::getDateTimeFormat(type, bLocale),
468 ((bLocale && defaultLocale != NULL) ? *defaultLocale : Locale::getEnglish()), ec);
471 LOGE("new SimpleDateFormat returned null");
472 throw Common::UnknownException("new SimpleDateFormat returned null");
476 fmt->setCalendar(*m_calendar);
477 fmt->format(m_calendar->getTime(ec), str);
481 std::string result = TimeUtilTools::toUTF8String(str);
485 LOGE("Failed to get time: %d, %s", ec, u_errorName(ec));
489 LOGE("can't make SimpleDateFormat or can't get time: %d, %s", ec, u_errorName(ec));
490 throw Common::UnknownException("can't make SimpleDateFormat or can't get time");
493 std::string TZDate::getTimezoneAbbreviation() const {
495 if (m_calendar == NULL) {
496 throw Common::UnknownException("can't make SimpleDateFormat or can't get time");
501 m_calendar->getTimeZone().getDisplayName(isDST(), TimeZone::SHORT, Locale::getEnglish(), str);
502 if ((str != "GMT") && (str.length() > 3) && !str.compare(0, 3, "GMT")) {
503 m_calendar->getTimeZone().getDisplayName(isDST(), TimeZone::LONG_GMT, Locale::getEnglish(), str);
505 std::string result = TimeUtilTools::toUTF8String(str);
507 LOGD ("%s", result.c_str());
511 long TZDate::secondsFromUTC() const {
512 UErrorCode ec = U_ZERO_ERROR;
513 return (m_calendar->get(UCAL_ZONE_OFFSET, ec) + m_calendar->get(UCAL_DST_OFFSET, ec)) * (-1) / 1000;
516 bool TZDate::isDST() const {
517 UErrorCode ec = U_ZERO_ERROR;
518 UBool result = m_calendar->inDaylightTime(ec);
519 return static_cast<bool>(result);
522 TZDatePtr TZDate::getPreviousDSTTransition() const {
523 return getDSTTransition(PREVDST);
526 TZDatePtr TZDate::getNextDSTTransition() const {
527 return getDSTTransition(NEXTDST);
530 TZDatePtr TZDate::getDSTTransition(DSTTransition tr_type) const
532 UErrorCode ec = U_ZERO_ERROR;
533 UBool result = false;
534 UDate dstTransitionDate = m_calendar->getTime(ec);
536 UnicodeString *id = TimeUtilTools::toUnicodeString(getTimezone());
537 VTimeZone *vtz = VTimeZone::createVTimeZoneByID(*id);
541 TimeZoneTransition tzTrans;
542 if (vtz->useDaylightTime()) {
543 if (NEXTDST == tr_type) {
544 result = vtz->getNextTransition(dstTransitionDate, FALSE, tzTrans);
546 else if (PREVDST == tr_type) {
547 result = vtz->getPreviousTransition(dstTransitionDate, FALSE, tzTrans);
550 LOGD("invalid comparison value");
554 dstTransitionDate = tzTrans.getTime();
560 TZDatePtr newObj = TZDatePtr(new (std::nothrow) TZDate(*this));
564 newObj->m_calendar->setTime(dstTransitionDate, ec);
568 LOGE("Failed to set time: %d, %s", ec, u_errorName(ec));
571 LOGE("can't getDSTTransition value from ICU: %d, %s", ec, u_errorName(ec));
572 throw Common::UnknownException("can't getDSTTransition value from ICU");
575 icu::Calendar* TZDate::getCalendar(const std::string& timezone) const {
576 UErrorCode ec = U_ZERO_ERROR;
577 std::shared_ptr<UnicodeString> usTimezone (TimeUtilTools::toUnicodeString(timezone));
578 icu::Calendar *cal = Calendar::createInstance(*(TimeZone::createTimeZone(*usTimezone)), ec);
580 if (!U_SUCCESS(ec)) {
581 LOGE("Can't create cal: %d, %s", ec, u_errorName(ec));
582 throw Common::UnknownException("Can't create cal");
584 UDate date = m_calendar->getTime(ec);
585 if (!U_SUCCESS(ec)) {
586 LOGE("Can't get time of m_calendar: %d, %s", ec, u_errorName(ec));
587 throw Common::UnknownException("Can't get time of m_calendar");
589 cal->setTime(date, ec);
590 if (!U_SUCCESS(ec)) {
591 LOGE("Can't get time of cal: %d, %s", ec, u_errorName(ec));
592 throw Common::UnknownException("Can't get time of cal");
597 icu::Calendar* TZDate::getUTCCalendar() const {
598 UErrorCode ec = U_ZERO_ERROR;
599 icu::Calendar *cal = Calendar::createInstance(*(TimeZone::getGMT()), ec);
601 if (!U_SUCCESS(ec)) {
602 LOGE("Can't create cal: %d, %s", ec, u_errorName(ec));
603 throw Common::UnknownException("Can't create cal");
605 UDate date = m_calendar->getTime(ec);
606 if (!U_SUCCESS(ec)) {
607 LOGE("Can't get time of m_calendar: %d, %s", ec, u_errorName(ec));
608 throw Common::UnknownException("Can't get time of m_calendar");
610 cal->setTime(date, ec);
611 if (!U_SUCCESS(ec)) {
612 LOGE("Can't get time of cal: %d, %s", ec, u_errorName(ec));
613 throw Common::UnknownException("Can't get time of cal");
618 void TZDate::setTimezone(const std::string& timezone, bool timezone_is_certain) {
619 if (!timezone_is_certain) {
620 timezone_is_certain = TimeUtilTools::isInTimezonesArray(timezone);
623 if (timezone_is_certain) {
624 std::shared_ptr<UnicodeString> usTimezone(TimeUtilTools::toUnicodeString(timezone));
625 TimeZone* tz = TimeZone::createTimeZone(*usTimezone);
626 m_calendar->adoptTimeZone(tz);
629 LOGD("Invalid timezone");
633 #ifdef IMPL_BACKWARD_COMPATIBLE
634 TZDate::TZDate(const std::string &timezone)
638 UErrorCode ec = U_ZERO_ERROR;
640 TimeZone *tz = util.makeTimeZone(timezone);
641 if (isAvailableTimezone(tz)) {
642 m_calendar= Calendar::createInstance(tz ,ec);
647 m_calendar = Calendar::createInstance(ec);
651 util.printDate(m_calendar);
653 LOGE("Failed to create calendar: %d, %s", ec, u_errorName(ec));
659 TZDate::TZDate(const TZDateProperties &properties)
664 m_calendar= _makeCalendar(properties);
667 TZDate::TZDate(const std::string &dateString, const double milliseconds, const std::string &timezone)
671 UErrorCode ec = U_ZERO_ERROR;
672 Calendar *dateCalender = Calendar::createInstance(ec);
673 DateFormat *df = NULL;
675 if (dateCalender == NULL || U_FAILURE(ec)) {
676 ThrowMsg(Commons::UnknownException, "Can't make calendar");
678 df = new SimpleDateFormat("EEE MMM d uuuuuu HH:mm:ss", Locale::getEnglish(), ec);
680 if (df == NULL || U_FAILURE(ec)) {
681 ThrowMsg(Commons::UnknownException, "Can't make DateFormat");
686 UnicodeString text(dateString.c_str());
687 UDate date = df->parse(text, ec);
690 ThrowMsg(Commons::UnknownException, "parse fail");
693 dateCalender->setTime(date, ec);
696 ThrowMsg(Commons::UnknownException, "setTime fail");
699 util.printDate(dateCalender);
700 TZDateProperties properties;
701 properties.year = _get(TZDATE_YEAR, dateCalender);
702 properties.month = _get(TZDATE_MONTH, dateCalender);
703 properties.day = _get(TZDATE_DATE, dateCalender);
704 properties.hours = _get(TZDATE_HOUR_OF_DAY, dateCalender);
705 properties.minutes = _get(TZDATE_MINUTE, dateCalender);
706 properties.seconds = _get(TZDATE_SECOND, dateCalender);
707 properties.milliseconds = milliseconds;
708 properties.timezone = timezone;
709 m_calendar = _makeCalendar(properties);
710 } catch (const WrtDeviceApis::Commons::Exception& err) {
711 LOGE("%s : %s", err.GetClassName().c_str(), err.GetMessage().c_str());
727 bool TZDate::isAvailableTimezone(const std::string &timezone)
730 TimeZone *tz = util.makeTimeZone(timezone);
732 bool result = isAvailableTimezone(tz);
740 bool TZDate::isAvailableTimezone(TimeZone *tz) {
746 if (util.toString(id) == "Etc/Unknown") {
753 std::string TZDate::_getTimezoneName(Calendar *cal)
756 ThrowMsg(Commons::UnknownException, "Invalid Date");
762 cal->getTimeZone().getID(id);
763 std::string s_result = util.toString(id);
764 LOGD("%s", s_result.c_str());
768 double TZDate::getTime() {
770 if (m_calendar== NULL) {
771 LOGE("calendar is NULL");
772 ThrowMsg(Commons::UnknownException, "Invalid Date");
775 UErrorCode ec = U_ZERO_ERROR;
777 UDate date = m_calendar->getTime(ec);
779 return static_cast<double>(date);
781 LOGE("Failed to get time: %d, %s", ec, u_errorName(ec));
784 ThrowMsg(Commons::PlatformException, "can't get time");
787 bool TZDate::setTime(const double time) {
789 if (m_calendar == NULL)
790 ThrowMsg(Commons::UnknownException, "Invalid Date");
792 UErrorCode ec = U_ZERO_ERROR;
794 m_calendar->setTime(static_cast<UDate>(time), ec);
798 LOGE("Failed to set time: %d, %s", ec, u_errorName(ec));
804 Calendar *TZDate::_makeCalendar(const TZDateProperties &properties)
807 UErrorCode ec = U_ZERO_ERROR;
810 Calendar *cal = NULL;
811 if ((properties.timezone == "") || !isAvailableTimezone(properties.timezone)) {
812 cal = Calendar::createInstance(ec);
814 cal = Calendar::createInstance(util.makeTimeZone(properties.timezone) ,ec);
817 if ((cal != NULL) && U_SUCCESS(ec)) {
819 cal->set(UCAL_DATE, 10); //set specific date because first date(1) or last date(31) can make changing of month because of timezone
821 _set(TZDATE_YEAR, properties.year, cal);
822 _set(TZDATE_MONTH, properties.month, cal);
823 _set(TZDATE_DATE, properties.day, cal);
824 _set(TZDATE_HOUR_OF_DAY, properties.hours, cal);
825 _set(TZDATE_MINUTE, properties.minutes, cal);
826 _set(TZDATE_SECOND, properties.seconds, cal);
827 _set(TZDATE_MILLISECOND, properties.milliseconds, cal);
828 } catch (const WrtDeviceApis::Commons::Exception& err) {
829 LOGE("%s : %s ", err.GetClassName().c_str(), err.GetMessage().c_str());
837 LOGE("Failed to create calendar: %d, %s", ec, u_errorName(ec));
847 long TZDate::_get(const TZDateFields field, Calendar *cal)
852 ThrowMsg(Commons::UnknownException, "Invalid Date");
855 if (_convertDateField(field) == UCAL_FIELD_COUNT) {
856 LOGD(">>> UCAL_FIELD_COUNT");
860 UErrorCode ec = U_ZERO_ERROR;
862 int32_t value = cal->get(_convertDateField(field), ec);
864 long result = util.tolong(value);
866 LOGD(">>> result: %ld", result);
869 LOGE("Failed to get calendar: %d, %s", ec, u_errorName(ec));
870 ThrowMsg(Commons::PlatformException, "Can't get Calendar value");
873 long TZDate::get(const TZDateFields field)
877 long result = _get(field, m_calendar);
878 if (field == TZDATE_DAY_OF_WEEK)
884 void TZDate::_set(const TZDateFields field, const long value, Calendar *cal)
886 if (_convertDateField(field) == UCAL_FIELD_COUNT) {
891 ThrowMsg(Commons::UnknownException, "Invalid Date");
895 Calendar *originCal = cal->clone();
896 cal->set(_convertDateField(field), util.toint32_t(value));
898 LOGD("Field : %d, value : %ld, result: %d", field, value, _get(field, cal));
900 if (field == TZDATE_YEAR) {
901 if (value != _get(field, cal))
902 ThrowMsg(Commons::PlatformException, "Out of range");
903 } else if (field == TZDATE_MONTH) {
904 long yearDiff = value / 12;
905 long month = value % 12;
911 long originYear = _get(TZDATE_YEAR, originCal);
912 if (((originYear + yearDiff) != _get(TZDATE_YEAR, cal))
913 || (month != _get(TZDATE_MONTH, cal))) {
914 LOGD("originYear: %ld, yearDiff: %ld, TZDATE_YEAR: %ld", originYear, yearDiff, _get(TZDATE_YEAR, cal));
915 LOGD(" month: %ld, TZDATE_MONTH: %ld", month, _get(TZDATE_MONTH, cal));
916 ThrowMsg(Commons::PlatformException, "Out of range");
919 UErrorCode ec = U_ZERO_ERROR;
920 double diff = value - _get(field, originCal);
921 if (field == TZDATE_DATE)
922 diff *= U_MILLIS_PER_DAY;
923 else if (field == TZDATE_HOUR_OF_DAY)
924 diff *= U_MILLIS_PER_HOUR;
925 else if (field == TZDATE_MINUTE)
926 diff *= U_MILLIS_PER_MINUTE;
927 else if (field == TZDATE_SECOND)
928 diff *= U_MILLIS_PER_SECOND;
930 UDate originUDate = originCal->getTime(ec);
931 if (!U_SUCCESS(ec)) {
932 LOGE("Failed to get time: %d, %s", ec, u_errorName(ec));
933 ThrowMsg(Commons::PlatformException, "ICU Error");
936 LOGD("originUDate : %f, diff : %ld", (double)originUDate, diff);
938 if ((U_DATE_MAX - originUDate) < diff)
939 ThrowMsg(Commons::PlatformException, "Out of range");
941 } catch (Commons::PlatformException& err) {
942 LOGE("%s", err.GetMessage().c_str());
952 void TZDate::set(const TZDateFields field, const long value)
955 _set(field, value, m_calendar);
956 } catch (Commons::PlatformException& err) {
957 LOGE("%s", err.GetMessage().c_str());
965 TZDateProperties TZDate::_makeProperties(Calendar *cal)
967 TZDateProperties result;
970 result.year = _get(TZDATE_YEAR, cal);
971 result.month = _get(TZDATE_MONTH,cal);
972 result.day = _get(TZDATE_DATE, cal);
973 result.hours = _get(TZDATE_HOUR_OF_DAY, cal);
974 result.minutes = _get(TZDATE_MINUTE, cal);
975 result.seconds = _get(TZDATE_SECOND, cal);
976 result.milliseconds = _get(TZDATE_MILLISECOND, cal);
977 result.timezone= _getTimezoneName(cal);
982 TZDateProperties TZDate::makeProperties()
984 return _makeProperties(m_calendar);
989 const UCalendarDateFields TZDate::_convertDateField(const TZDateFields field)
996 return UCAL_EXTENDED_YEAR;
1001 case TZDATE_WEEK_OF_YEAR:
1002 return UCAL_WEEK_OF_YEAR;
1004 case TZDATE_WEEK_OF_MONTH:
1005 return UCAL_WEEK_OF_MONTH;
1010 case TZDATE_DAY_OF_YEAR:
1011 return UCAL_DAY_OF_YEAR;
1013 case TZDATE_DAY_OF_WEEK:
1014 return UCAL_DAY_OF_WEEK;
1016 case TZDATE_DAY_OF_WEEK_IN_MONTH:
1017 return UCAL_DAY_OF_WEEK_IN_MONTH;
1025 case TZDATE_HOUR_OF_DAY:
1026 return UCAL_HOUR_OF_DAY;
1034 case TZDATE_MILLISECOND:
1035 return UCAL_MILLISECOND;
1037 case TZDATE_ZONE_OFFSET:
1038 return UCAL_ZONE_OFFSET;
1040 case TZDATE_DST_OFFSET:
1041 return UCAL_DST_OFFSET;
1044 return UCAL_FIELD_COUNT;
1048 #endif // IMPL_BACKWARD_COMPATIBLE