The download UI application is added.
[apps/web/download-manager.git] / src / download-manager-dateTime.cpp
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
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
7  *
8  *    http://www.tizenopensource.org/license
9  *
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.
15  */
16
17 /**
18  * @file        download-manager-dateTime.cpp
19  * @author      Jungki Kwak (jungki.kwak@samsung.com)
20  * @brief       data and utility APIs for Date and Time
21  */
22
23 #include "runtime_info.h"
24 #include "download-manager-dateTime.h"
25
26 #define MAX_SKELETON_BUFFER_LEN 8
27 #define MAX_PATTERN_BUFFER_LEN 128
28
29 DateGroup::DateGroup()
30         : count(0)
31         , type(DATETIME::DATE_TYPE_NONE)
32         , m_glGroupItem(NULL)
33 {
34 }
35
36 DateGroup::~DateGroup()
37 {
38 }
39
40 void DateGroup::initData()
41 {
42         count = 0;
43         m_glGroupItem = NULL;
44 }
45
46 DateUtil::DateUtil()
47         : m_todayStandardTime(0)
48         , dateShortFormat(NULL)
49         , dateMediumFormat(NULL)
50         , dateFullFormat(NULL)
51         , timeFormat12H(NULL)
52         , timeFormat24H(NULL)
53 {
54 }
55
56 DateUtil::~DateUtil()
57 {
58         deinitLocaleData();
59 }
60
61 void DateUtil::deinitLocaleData()
62 {
63         if (dateShortFormat)
64                 udat_close(dateShortFormat);
65         if (dateMediumFormat)
66                 udat_close(dateMediumFormat);
67         if (dateFullFormat)
68                 udat_close(dateFullFormat);
69         if (timeFormat12H)
70                 udat_close(timeFormat12H);
71         if (timeFormat24H)
72                 udat_close(timeFormat24H);
73         dateShortFormat = NULL;
74         dateMediumFormat = NULL;
75         dateFullFormat = NULL;
76         timeFormat12H = NULL;
77         timeFormat24H = NULL;
78 }
79
80 int DateUtil::getDiffDaysFromToday()
81 {
82         time_t now = time(NULL);
83         DP_LOGD("todayStandardTime[%ld]", m_todayStandardTime);
84         if (m_todayStandardTime == 0)
85                 return 0;
86         else
87                 return getDiffDays(now, m_todayStandardTime);
88 }
89
90 int DateUtil::getDiffDays(time_t nowTime,time_t refTime)
91 {
92         int diffDays = 0;
93         int nowYear = 0;
94         int nowYday = 0;
95         int refYday = 0;
96         int refYear = 0;
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;
110                 /* leap year */
111                 if ((year%4 == 0 && year%100 != 0) || year%400 == 0)
112                         diffDays++;
113         }
114         DP_LOGD("diffDays[%d]",diffDays);
115         return diffDays;
116 }
117
118 UDateFormat *DateUtil::getBestPattern(const char *patternStr,
119         UDateTimePatternGenerator *generator, const char *locale)
120 {
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;
126
127         if (patternStr) {
128                 u_uastrncpy(customSkeleton, patternStr, strlen(patternStr));
129                 patternLen = udatpg_getBestPattern(generator, customSkeleton,
130                 u_strlen(customSkeleton), bestPattern, MAX_PATTERN_BUFFER_LEN,
131                         &status);
132                 DP_LOGD("udatpg_getBestPattern status[%d] bestPattern[%s]", status,
133                         bestPattern);
134                 if (patternLen < 1) {
135                         format = udat_open(UDAT_SHORT, UDAT_NONE, locale, NULL, -1,
136                                 NULL, -1, &status);
137                         return format;
138                 }
139         }
140         format = udat_open(UDAT_IGNORE, UDAT_NONE, locale, NULL, -1,
141                 bestPattern, -1, &status);
142         return format;
143 }
144
145 void DateUtil::updateLocale()
146 {
147         UDateTimePatternGenerator *generator = NULL;
148         UErrorCode status = U_ZERO_ERROR;
149         const char *locale = NULL;
150
151         DP_LOGD_FUNC();
152
153         deinitLocaleData();
154
155         uloc_setDefault(getenv("LC_TIME"), &status);
156         DP_LOGD("uloc_setDefault status[%d]",status);
157
158         locale = uloc_getDefault();
159         generator = udatpg_open(locale,&status);
160         DP_LOGD("udatpg_open status[%d]",status);
161
162         timeFormat12H = getBestPattern("hm", generator, locale);
163         timeFormat24H = getBestPattern("Hm", generator, locale);
164
165         dateShortFormat = udat_open(UDAT_NONE, UDAT_SHORT, locale, NULL, -1, NULL,
166                 -1, &status);
167         dateMediumFormat = udat_open(UDAT_NONE, UDAT_MEDIUM, locale, NULL, -1, NULL,
168                 -1, &status);
169         dateFullFormat = getBestPattern("yMMMEEEd", generator, locale);
170         udatpg_close(generator);
171 }
172
173 void DateUtil::getDateStr(int style, double time, string &outBuf)
174 {
175         UDateFormat *format = NULL;
176         UErrorCode status = U_ZERO_ERROR;
177         UChar str[MAX_BUF_LEN] = {0,};
178         bool value = false;
179
180         DP_LOGD_FUNC();
181         switch (style) {
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;
187                 } else {
188                         if (value)
189                                 format = timeFormat24H;
190                         else
191                                 format = timeFormat12H;
192                 }
193                 break;
194         case LOCALE_STYLE::SHORT_DATE:
195                 format = dateShortFormat;
196                 break;
197         case LOCALE_STYLE::MEDIUM_DATE:
198                 format = dateMediumFormat;
199                 break;
200         case LOCALE_STYLE::FULL_DATE:
201                 format = dateFullFormat;
202                 break;
203         default :
204                 DP_LOGE("Critical: cannot enter here");
205                 format = timeFormat12H;
206                 break;
207         }
208         if (format) {
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);
214         } else {
215                 DP_LOGE("Critical: fail to get time value");
216                 outBuf = string(S_("IDS_COM_POP_ERROR"));
217         }
218 }
219