2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.tizenopensource.org/license
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @file download-manager-dateTime.cpp
19 * @author Jungki Kwak (jungki.kwak@samsung.com)
20 * @brief data and utility APIs for Date and Time
23 #include "runtime_info.h"
24 #include "download-manager-dateTime.h"
26 #define MAX_SKELETON_BUFFER_LEN 8
27 #define MAX_PATTERN_BUFFER_LEN 128
29 DateGroup::DateGroup()
31 , type(DATETIME::DATE_TYPE_NONE)
36 DateGroup::~DateGroup()
40 void DateGroup::initData()
47 : m_todayStandardTime(0)
48 , dateShortFormat(NULL)
49 , dateMediumFormat(NULL)
50 , dateFullFormat(NULL)
61 void DateUtil::deinitLocaleData()
64 udat_close(dateShortFormat);
66 udat_close(dateMediumFormat);
68 udat_close(dateFullFormat);
70 udat_close(timeFormat12H);
72 udat_close(timeFormat24H);
73 dateShortFormat = NULL;
74 dateMediumFormat = NULL;
75 dateFullFormat = NULL;
80 int DateUtil::getDiffDaysFromToday()
82 time_t now = time(NULL);
83 DP_LOGD("todayStandardTime[%ld]", m_todayStandardTime);
84 if (m_todayStandardTime == 0)
87 return getDiffDays(now, m_todayStandardTime);
90 int DateUtil::getDiffDays(time_t nowTime,time_t refTime)
97 struct tm *nowDate = localtime(&nowTime);
98 nowYday = nowDate->tm_yday;
99 nowYear = nowDate->tm_year;
100 struct tm *finishedDate = localtime(&refTime);
101 refYday = finishedDate->tm_yday;
102 refYear = finishedDate->tm_year;
103 diffDays = nowYday - refYday;
104 /*DP_LOGD("refDate[%d/%d/%d]refTime[%ld]yday[%d]",
105 (finishedDate->tm_year + 1900), (finishedDate->tm_mon + 1),
106 finishedDate->tm_mday, refTime, refYday);*/
107 if ((nowYear-refYear)>0 && diffDays < 0) {
108 int year = nowDate->tm_year;
109 diffDays = diffDays + 365;
111 if ((year%4 == 0 && year%100 != 0) || year%400 == 0)
114 DP_LOGD("diffDays[%d]",diffDays);
118 UDateFormat *DateUtil::getBestPattern(const char *patternStr,
119 UDateTimePatternGenerator *generator, const char *locale)
121 UDateFormat *format = NULL;
122 UChar customSkeleton[MAX_SKELETON_BUFFER_LEN] = {0,};
123 UChar bestPattern[MAX_PATTERN_BUFFER_LEN] = {0,};
124 UErrorCode status = U_ZERO_ERROR;
125 int32_t patternLen = 0;
128 u_uastrncpy(customSkeleton, patternStr, strlen(patternStr));
129 patternLen = udatpg_getBestPattern(generator, customSkeleton,
130 u_strlen(customSkeleton), bestPattern, MAX_PATTERN_BUFFER_LEN,
132 DP_LOGD("udatpg_getBestPattern status[%d] bestPattern[%s]", status,
134 if (patternLen < 1) {
135 format = udat_open(UDAT_SHORT, UDAT_NONE, locale, NULL, -1,
140 format = udat_open(UDAT_IGNORE, UDAT_NONE, locale, NULL, -1,
141 bestPattern, -1, &status);
145 void DateUtil::updateLocale()
147 UDateTimePatternGenerator *generator = NULL;
148 UErrorCode status = U_ZERO_ERROR;
149 const char *locale = NULL;
155 uloc_setDefault(getenv("LC_TIME"), &status);
156 DP_LOGD("uloc_setDefault status[%d]",status);
158 locale = uloc_getDefault();
159 generator = udatpg_open(locale,&status);
160 DP_LOGD("udatpg_open status[%d]",status);
162 timeFormat12H = getBestPattern("hm", generator, locale);
163 timeFormat24H = getBestPattern("Hm", generator, locale);
165 dateShortFormat = udat_open(UDAT_NONE, UDAT_SHORT, locale, NULL, -1, NULL,
167 dateMediumFormat = udat_open(UDAT_NONE, UDAT_MEDIUM, locale, NULL, -1, NULL,
169 dateFullFormat = getBestPattern("yMMMEEEd", generator, locale);
170 udatpg_close(generator);
173 void DateUtil::getDateStr(int style, double time, string &outBuf)
175 UDateFormat *format = NULL;
176 UErrorCode status = U_ZERO_ERROR;
177 UChar str[MAX_BUF_LEN] = {0,};
182 case LOCALE_STYLE::TIME:
183 if (runtime_info_get_value_bool(
184 RUNTIME_INFO_KEY_24HOUR_CLOCK_FORMAT_ENABLED,&value) != 0) {
185 DP_LOGE("runtime_info_get_value_bool is failed");
186 format = timeFormat12H;
189 format = timeFormat24H;
191 format = timeFormat12H;
194 case LOCALE_STYLE::SHORT_DATE:
195 format = dateShortFormat;
197 case LOCALE_STYLE::MEDIUM_DATE:
198 format = dateMediumFormat;
200 case LOCALE_STYLE::FULL_DATE:
201 format = dateFullFormat;
204 DP_LOGE("Critical: cannot enter here");
205 format = timeFormat12H;
209 char tempBuf[MAX_BUF_LEN] = {0,};
210 udat_format(format, time, str, MAX_BUF_LEN - 1, NULL, &status);
211 DP_LOGD("udat_format : status[%d]", status);
212 u_austrncpy(tempBuf, str, MAX_BUF_LEN-1);
213 outBuf = string(tempBuf);
215 DP_LOGE("Critical: fail to get time value");
216 outBuf = string(S_("IDS_COM_POP_ERROR"));