Changed indicator bg color.
[platform/framework/native/uifw.git] / src / ui / controls / FUiCtrl_DateTimeUtils.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0/
10 //
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.
16 //
17
18 /**
19  * @file                FUiCtrl_DateTimeUtils.cpp
20  * @brief               This is the implementation file for the _DateTimeUtils class.
21  */
22
23 #include <FBaseDateTime.h>
24 #include <FLclDateTimeSymbols.h>
25 #include <FLclLocaleManager.h>
26 #include <FLclDateTimeFormatter.h>
27 #include <FSysSettingInfo.h>
28 #include "FUiCtrl_DateTimeUtils.h"
29 #include "FUiCtrl_DateTimeDefine.h"
30 #include "FUi_ResourceManager.h"
31
32 using namespace Tizen::Base;
33 using namespace Tizen::Locales;
34 using namespace Tizen::Base::Collection;
35 using namespace Tizen::System;
36
37 namespace Tizen { namespace Ui { namespace Controls
38 {
39
40 _DateTimeUtils::~_DateTimeUtils(void)
41 {
42 }
43
44 _DateTimeUtils::_DateTimeUtils(void)
45 {
46 }
47
48 const String
49 _DateTimeUtils::GetMonthString(int month) const
50 {
51         result r = E_SUCCESS;
52         String text;
53         text.Clear();
54
55         DateTimeSymbols dateSymbols;
56         r = dateSymbols.Construct(CALENDAR_GREGORIAN);
57
58         if (r != E_SUCCESS)
59         {
60                 text.Append(GetEnglishMonthString(month));
61                 return text;
62         }
63
64         const IList* pListOfShortMonths = dateSymbols.GetShortMonths();
65         SysTryReturn(NID_UI_CTRL, (pListOfShortMonths != null), text, E_SYSTEM, "[E_SYSTEM] A system error has occurred. pListOfShortMonths is null.");
66
67         String* pObj = null;
68         pObj = static_cast<String*>(const_cast<Object*>(pListOfShortMonths->GetAt(month - 1)));
69
70         if (pObj)
71         {
72                 text.Append(*pObj);
73         }
74
75         return text;
76 }
77
78 const String
79 _DateTimeUtils::GetEnglishMonthString(int month) const
80 {
81         String text;
82         text.Clear();
83         switch (month)
84         {
85         case 1:
86                 text.Append("Jan");
87                 break;
88
89         case 2:
90                 text.Append("Feb");
91                 break;
92
93         case 3:
94                 text.Append("Mar");
95                 break;
96
97         case 4:
98                 text.Append("Apr");
99                 break;
100
101         case 5:
102                 text.Append("May");
103                 break;
104
105         case 6:
106                 text.Append("Jun");
107                 break;
108
109         case 7:
110                 text.Append("Jul");
111                 break;
112
113         case 8:
114                 text.Append("Aug");
115                 break;
116
117         case 9:
118                 text.Append("Sep");
119                 break;
120
121         case 10:
122                 text.Append("Oct");
123                 break;
124
125         case 11:
126                 text.Append("Nov");
127                 break;
128
129         case 12:
130                 text.Append("Dec");
131                 break;
132
133         default:
134                 break;
135         }
136         return text;
137 }
138
139 int
140 _DateTimeUtils::CalculateMaxDay(int displayYear, int month) const
141 {
142         int maxDay = 0;
143         switch (month)
144         {
145         case 1:
146                 // fall through
147         case 3:
148                 // fall through
149         case 5:
150                 // fall through
151         case 7:
152                 // fall through
153         case 8:
154                 // fall through
155         case 10:
156                 // fall through
157         case 12:
158                 maxDay = 31;
159                 break;
160
161         case 2:
162                 if (IsLeapYear(displayYear))
163                 {
164                         maxDay = 29;
165                 }
166                 else
167                 {
168                         maxDay = 28;
169                 }
170                 break;
171
172         default:
173                 maxDay = 30;
174                 break;
175         }
176         return maxDay;
177 }
178
179 bool
180 _DateTimeUtils::IsLeapYear(int year) const
181 {
182         return DateTime::IsLeapYear(year);
183 }
184
185 void
186 _DateTimeUtils::GetAmPm(String& timeFormat, _AmPmType amPmType) const
187 {
188         result r = E_SUCCESS;
189         DateTimeSymbols dateSymbols;
190
191         r = dateSymbols.Construct(CALENDAR_GREGORIAN);
192
193         if (r != E_SUCCESS)
194         {
195                 if (amPmType == AM_TYPE)
196                 {
197                         timeFormat.Append("AM");
198                 }
199                 else
200                 {
201                         timeFormat.Append("PM");
202                 }
203                 return;
204         }
205
206         const IList* pListOfAmPm = dateSymbols.GetAmPm();
207         SysTryReturnVoidResult(NID_UI_CTRL, (pListOfAmPm != null), E_SYSTEM, "[E_SYSTEM] A system error has occurred. pListOfAmPm is null.");
208
209         String* pObj = null;
210         pObj = static_cast<String*>(const_cast<Object*>(pListOfAmPm->GetAt(amPmType)));
211
212         if (pObj)
213         {
214                 timeFormat.Append(*pObj);
215         }
216
217         return;
218 }
219
220 const String
221 _DateTimeUtils::GetFullMonthString(int month) const
222 {
223         String text;
224         text.Clear();
225
226         DateTimeSymbols dateSymbols;
227         dateSymbols.Construct(CALENDAR_GREGORIAN);
228
229         const IList* pListOfFullMonths = dateSymbols.GetMonths();
230         SysTryReturn(NID_LCL, (pListOfFullMonths != null), text, E_SYSTEM, "[E_SYSTEM] A system error has occurred. pListOfFullMonths is null.");
231
232         String* pObj = null;
233         pObj = static_cast<String*>(const_cast<Object*>(pListOfFullMonths->GetAt(month - 1)));
234
235         if (pObj)
236         {
237                 text.Append(*pObj);
238         }
239
240         return text;
241 }
242
243 int
244 _DateTimeUtils::GetLocaleDateFormat(void) const
245 {
246         result r = E_SUCCESS;
247
248         LocaleManager localeManager;
249         int localeDateFormat = 0;
250
251         r = localeManager.Construct();
252         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, localeDateFormat, E_SYSTEM, "[E_SYSTEM] A system error has occurred.");
253
254         Locale systemLocale = localeManager.GetSystemLocale();
255
256         DateTimeFormatter* pDateFormatter = DateTimeFormatter::CreateDateFormatterN(systemLocale, DATE_TIME_STYLE_LONG);
257         r = GetLastResult();
258         SysTryReturn(NID_UI_CTRL, pDateFormatter != null, localeDateFormat, r, "[%s] Propagating.", GetErrorMessage(r));
259
260         String formatString = pDateFormatter->GetPattern();
261         formatString.ToLowerCase();
262
263         delete pDateFormatter;
264         pDateFormatter = null;
265
266         int stringLength = formatString.GetLength();
267         String parsedString = L"";
268
269         for (int i = 0; i < stringLength; i++)
270         {
271                 wchar_t ch = '\0';
272                 r = formatString.GetCharAt(i, ch);
273                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, localeDateFormat, r, "[%s] Propagating.", GetErrorMessage(r));
274
275                 if (ch == 'd' && !parsedString.Contains(L"DD"))
276                 {
277                         parsedString.Append(L"DD");
278                 }
279                 else if (ch == 'm' && !parsedString.Contains(L"MM"))
280                 {
281                         parsedString.Append(L"MM");
282                 }
283                 else if (ch == 'y' && !parsedString.Contains(L"YYYY"))
284                 {
285                         parsedString.Append(L"YYYY");
286                 }
287
288                 if (parsedString.GetLength() == LOCALE_DATE_FORMAT_STRING_LENGTH)
289                 {
290                         break;
291                 }
292         }
293
294         if (parsedString.Equals(L"DDMMYYYY", false))
295         {
296                 localeDateFormat = DATE_FORMAT_DDMMYYYY;
297         }
298         else if (parsedString.Equals(L"MMDDYYYY", false))
299         {
300                 localeDateFormat = DATE_FORMAT_MMDDYYYY;
301         }
302         else if (parsedString.Equals(L"YYYYMMDD", false))
303         {
304                 localeDateFormat = DATE_FORMAT_YYYYMMDD;
305         }
306         else if (parsedString.Equals(L"YYYYDDMM", false))
307         {
308                 localeDateFormat = DATE_FORMAT_YYYYDDMM;
309         }
310
311         return localeDateFormat;
312 }
313
314 }}} // Tizen::Ui::Controls