Enable build with iniparser v 3.1
[platform/framework/native/appfw.git] / src / locales / FLcl_IcuCalendarImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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     FLcl_IcuCalendarImpl.cpp
19  * @brief    This is the implementation file for _IcuCalendarImpl class.
20  */
21
22 #include <unique_ptr.h>
23 #include <unicode/errorcode.h>
24 #include <unicode/simpletz.h>
25 #include <unicode/calendar.h>
26 #include <unicode/gregocal.h>
27
28 #include <FLclLocale.h>
29 #include <FLclTimeZone.h>
30 #include <FLclCalendar.h>
31
32 #include <FBase_StringConverter.h>
33 #include <FBaseSysLog.h>
34
35 #include "FLcl_LocaleData.h"
36 #include "FLcl_IcuCalendarImpl.h"
37 #include "FLcl_TimeZoneImpl.h"
38
39
40 typedef U_ICU_NAMESPACE::ErrorCode IcuErrorCode;
41 typedef U_ICU_NAMESPACE::Locale IcuLocale;
42 typedef U_ICU_NAMESPACE::Calendar IcuCalendar;
43 typedef U_ICU_NAMESPACE::GregorianCalendar IcuGregorianCalendar;
44 typedef U_ICU_NAMESPACE::TimeZone IcuTimeZone;
45 typedef U_ICU_NAMESPACE::SimpleTimeZone IcuSimpleTimeZone;
46
47 using namespace Tizen::Base;
48
49 static const long long _EPOCH_OFFSET_IN_MILLISEC = 62135596800000LL;
50 static const int _GREGORIAN_DEFAULT_YEAR = 1970;
51
52 namespace Tizen { namespace Locales
53 {
54
55 _IcuCalendarImpl::_IcuCalendarImpl()
56         : __pIcuCalendar(null)
57         , __isEraFieldOverFlowed(false)
58 {
59 }
60
61 _IcuCalendarImpl::~_IcuCalendarImpl()
62 {
63         delete __pIcuCalendar;
64         __pIcuCalendar = null;
65 }
66
67 result
68 _IcuCalendarImpl::Construct(const TimeZone& timeZone, const Locale& locale, int calendarType)
69 {
70         // Object is not allowed to construct twice
71         SysAssertf(__pIcuCalendar == null,
72                            "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
73
74         IcuLocale icuLocale = GetIcuLocale(locale, calendarType);
75         result r = GetLastResult();
76         if (!IsFailed(r))
77         {
78                 const _TimeZoneImpl* pTimeZoneImpl = _TimeZoneImpl::GetTimeZoneImpl(timeZone);
79                 if (pTimeZoneImpl)
80                 {
81                         IcuErrorCode err;
82                         __pIcuCalendar = IcuCalendar::createInstance(pTimeZoneImpl->GetIcuTimeZone()->clone(), icuLocale, err);
83                         r = GetOspException(err);
84                 }
85         }
86         return r;
87 }
88
89 _IcuCalendarImpl*
90 _IcuCalendarImpl::CloneN(void)
91 {
92         std::unique_ptr< _IcuCalendarImpl > pIcuCalendar(new (std::nothrow) _IcuCalendarImpl);
93         SysTryReturn(NID_LCL, pIcuCalendar != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
94
95         if (__pIcuCalendar != null)
96         {
97                 pIcuCalendar->__pIcuCalendar = __pIcuCalendar->clone();
98                 if (pIcuCalendar->__pIcuCalendar != null)
99                 {
100                         pIcuCalendar->__isEraFieldOverFlowed = __isEraFieldOverFlowed;
101                 }
102         }
103
104         return pIcuCalendar.release();
105 }
106
107 result
108 _IcuCalendarImpl::AddTimeField(int field, int amount)
109 {
110         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
111
112 // Uncomment these lines if OSP values are set to be in minutes
113 //      if (field == TIME_FIELD_ZONE_OFFSET || field == TIME_FIELD_DST_OFFSET)
114 //      {
115 //              amount *= Calendar::ONE_MINUTE_IN_MILLISEC;
116 //      }
117
118         IcuErrorCode err;
119         __pIcuCalendar->add(static_cast< UCalendarDateFields >(GetIcuTimeField(field)), amount, err);
120
121         return GetOspException(err);
122 }
123
124 result
125 _IcuCalendarImpl::After(const _IcuCalendarImpl& otherInstance, bool& after)
126 {
127         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
128         SysTryReturnResult(NID_LCL, otherInstance.__pIcuCalendar, E_INVALID_ARG, "otherInstance is not constructed");
129
130         result r = ValidateFieldsForOverFlow();
131         SysTryReturn(NID_LCL, !IsFailed(r), r, r, "[%s] Fields values are not valid", GetErrorMessage(r));
132
133         r = otherInstance.ValidateFieldsForOverFlow();
134         SysTryReturn(NID_LCL, !IsFailed(r), r, r, "[%s] otherInstance's Fields values are not valid", GetErrorMessage(r));
135
136         IcuErrorCode err;
137         after = __pIcuCalendar->after(*(otherInstance.__pIcuCalendar), err);
138         return GetOspException(err);
139 }
140
141 result
142 _IcuCalendarImpl::Before(const _IcuCalendarImpl& otherInstance, bool& before)
143 {
144         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
145         SysTryReturnResult(NID_LCL, otherInstance.__pIcuCalendar, E_INVALID_ARG, "otherInstance is not constructed");
146
147         result r = ValidateFieldsForOverFlow();
148         SysTryReturn(NID_LCL, !IsFailed(r), r, r, "[%s] Fields values are not valid", GetErrorMessage(r));
149
150         r = otherInstance.ValidateFieldsForOverFlow();
151         SysTryReturn(NID_LCL, !IsFailed(r), r, r, "[%s] otherInstance's Fields values are not valid", GetErrorMessage(r));
152
153         IcuErrorCode err;
154         before = __pIcuCalendar->before(*(otherInstance.__pIcuCalendar), err);
155         return GetOspException(err);
156 }
157
158 result
159 _IcuCalendarImpl::Clear(void)
160 {
161         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
162
163         __pIcuCalendar->clear();
164         return E_SUCCESS;
165 }
166
167 result
168 _IcuCalendarImpl::Clear(int field)
169 {
170         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
171
172         UCalendarDateFields icuTimeField = static_cast< UCalendarDateFields >(GetIcuTimeField(field));
173         SysTryReturnResult(NID_LCL, icuTimeField != UCAL_FIELD_COUNT,
174                         E_INVALID_ARG, "Invalid argument is used. Input field value is not supported");
175
176         // Hack to reset year field to 1970 while clearing TIME_FIELD_YEAR in GregorianCalendar
177         if (icuTimeField == UCAL_YEAR)
178         {
179                 IcuGregorianCalendar* pCal = dynamic_cast< IcuGregorianCalendar* >(__pIcuCalendar);
180                 if (pCal != null)
181                 {
182                         IcuErrorCode err;
183                         pCal->set(UCAL_YEAR, _GREGORIAN_DEFAULT_YEAR);
184                         pCal->getTime(err);
185                 }
186         }
187
188         __pIcuCalendar->clear(icuTimeField);
189         return E_SUCCESS;
190 }
191
192 bool
193 _IcuCalendarImpl::Equals(const _IcuCalendarImpl& otherInstance)
194 {
195         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
196         if (otherInstance.__pIcuCalendar != null)
197         {
198                 return *__pIcuCalendar == *otherInstance.__pIcuCalendar;
199         }
200         return false;
201 }
202
203 int
204 _IcuCalendarImpl::GetActualMaxTimeField(int field) const
205 {
206         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
207
208         UCalendarDateFields icuTimeField = static_cast< UCalendarDateFields >(GetIcuTimeField(field));
209         if (icuTimeField != UCAL_FIELD_COUNT)
210         {
211                 IcuErrorCode err;
212                 int value = __pIcuCalendar->getActualMaximum(icuTimeField, err);
213                 if (err.isSuccess())
214                 {
215                         SetLastResult(E_SUCCESS);
216                         return ConvertToOspTimeFieldValue(field, value);
217                 }
218         }
219
220         SetLastResult(E_INVALID_ARG);
221         return -1;
222 }
223
224 int
225 _IcuCalendarImpl::GetActualMinTimeField(int field) const
226 {
227         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
228
229         UCalendarDateFields icuTimeField = static_cast< UCalendarDateFields >(GetIcuTimeField(field));
230         if (icuTimeField != UCAL_FIELD_COUNT)
231         {
232                 IcuErrorCode err;
233                 int value = __pIcuCalendar->getActualMinimum(icuTimeField, err);
234                 if (err.isSuccess())
235                 {
236                         SetLastResult(E_SUCCESS);
237                         return ConvertToOspTimeFieldValue(field, value);
238                 }
239                 SetLastResult(E_SYSTEM);
240         }
241
242         SetLastResult(E_INVALID_ARG);
243         return -1;
244 }
245
246 int
247 _IcuCalendarImpl::GetFirstDayOfWeek(void) const
248 {
249         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
250
251         IcuErrorCode err;
252         int value = __pIcuCalendar->getFirstDayOfWeek(err); // ICU DAY of week and OSP day of week are similar
253         if (err.isSuccess())
254         {
255                 return value;
256         }
257
258         return -1;
259 }
260
261 int
262 _IcuCalendarImpl::GetGreatestMinTimeField(int field) const
263 {
264         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
265
266         UCalendarDateFields icuTimeField = static_cast< UCalendarDateFields >(GetIcuTimeField(field));
267         if (icuTimeField != UCAL_FIELD_COUNT)
268         {
269                 SetLastResult(E_SUCCESS);
270                 int value = __pIcuCalendar->getGreatestMinimum(icuTimeField);
271                 return ConvertToOspTimeFieldValue(field, value);
272         }
273
274         SetLastResult(E_INVALID_ARG);
275         return -1;
276 }
277
278 int
279 _IcuCalendarImpl::GetHashCode(void) const
280 {
281         return (int) E_UNSUPPORTED_OPERATION;
282 }
283
284 int
285 _IcuCalendarImpl::GetLeastMaxTimeField(int field) const
286 {
287         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
288
289         UCalendarDateFields icuTimeField = static_cast< UCalendarDateFields >(GetIcuTimeField(field));
290         if (icuTimeField != UCAL_FIELD_COUNT)
291         {
292                 SetLastResult(E_SUCCESS);
293                 int value = __pIcuCalendar->getLeastMaximum(icuTimeField);
294                 return ConvertToOspTimeFieldValue(field, value);
295         }
296
297         SetLastResult(E_INVALID_ARG);
298         return -1;
299 }
300
301 int
302 _IcuCalendarImpl::GetMaxTimeField(int field) const
303 {
304         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
305
306         UCalendarDateFields icuTimeField = static_cast< UCalendarDateFields >(GetIcuTimeField(field));
307         if (icuTimeField != UCAL_FIELD_COUNT)
308         {
309                 SetLastResult(E_SUCCESS);
310                 int value = __pIcuCalendar->getMaximum(icuTimeField);
311                 return ConvertToOspTimeFieldValue(field, value);
312         }
313
314         SetLastResult(E_INVALID_ARG);
315         return -1;
316 }
317
318 int
319 _IcuCalendarImpl::GetMinDaysInFirstWeek(void) const
320 {
321         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
322         return __pIcuCalendar->getMinimalDaysInFirstWeek();
323 }
324
325 int
326 _IcuCalendarImpl::GetMinTimeField(int field) const
327 {
328         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
329
330         UCalendarDateFields icuTimeField = static_cast< UCalendarDateFields >(GetIcuTimeField(field));
331         if (icuTimeField != UCAL_FIELD_COUNT)
332         {
333                 SetLastResult(E_SUCCESS);
334                 int value = __pIcuCalendar->getMinimum(icuTimeField);
335                 return ConvertToOspTimeFieldValue(field, value);
336         }
337
338         SetLastResult(E_INVALID_ARG);
339         return -1;
340 }
341
342 int
343 _IcuCalendarImpl::GetTimeField(int field) const
344 {
345         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
346
347         UCalendarDateFields icuTimeField = static_cast< UCalendarDateFields >(GetIcuTimeField(field));
348         if (icuTimeField != UCAL_FIELD_COUNT)
349         {
350                 IcuErrorCode err;
351                 int value = __pIcuCalendar->get(icuTimeField, err);
352                 if (err.isSuccess())
353                 {
354                         return ConvertToOspTimeFieldValue(field, value);
355                 }
356                 SetLastResult(GetOspException(err));
357         }
358
359         return -1;
360 }
361
362 result
363 _IcuCalendarImpl::GetTimeInMillisec(long long& millisec) const
364 {
365         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
366
367         result r = ValidateFieldsForOverFlow();
368         SysTryReturn(NID_LCL, !IsFailed(r), r, r, "[%s]Instance is not constructed", GetErrorMessage(r));
369
370         IcuErrorCode err;
371         millisec = __pIcuCalendar->getTime(err);
372         if (err.isSuccess())
373         {
374                 millisec += _EPOCH_OFFSET_IN_MILLISEC;
375                 return E_SUCCESS;
376         }
377         return GetOspException(err);
378 }
379
380 result
381 _IcuCalendarImpl::IsInDst(bool& isInDst) const
382 {
383         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
384
385         result r = ValidateFieldsForOverFlow();
386         SysTryReturn(NID_LCL, !IsFailed(r), r, r, "[%s] Fields values are not valid", GetErrorMessage(r));
387
388         IcuErrorCode err;
389         isInDst = __pIcuCalendar->inDaylightTime(err);
390         return GetOspException(err);
391 }
392
393 bool
394 _IcuCalendarImpl::IsLeapYear(int year) const
395 {
396         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
397
398         IcuGregorianCalendar* pCal = dynamic_cast< IcuGregorianCalendar* >(__pIcuCalendar);
399         if (pCal != null)
400         {
401                 if (year <= 0)
402                 {
403                         year = 1 - year; // to adjust year as year 0 to 1 BC, -1 to 2 BC and so on
404                 }
405                 return pCal->isLeapYear(year);
406         }
407         return false;
408 }
409
410 bool
411 _IcuCalendarImpl::IsLenient(void) const
412 {
413         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
414         return __pIcuCalendar->isLenient();
415 }
416
417 bool
418 _IcuCalendarImpl::IsSet(int field) const
419 {
420         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
421
422         UCalendarDateFields icuTimeField = static_cast< UCalendarDateFields >(GetIcuTimeField(field));
423         if (icuTimeField != UCAL_FIELD_COUNT)
424         {
425                 return __pIcuCalendar->isSet(icuTimeField);
426         }
427
428         return false;
429 }
430
431 result
432 _IcuCalendarImpl::Roll(int field, int amount)
433 {
434         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
435
436         result r = ValidateFieldsForOverFlow();
437         if (!IsFailed(r))
438         {
439                 // Non Roll-able fields
440                 if (field == TIME_FIELD_ZONE_OFFSET || field == TIME_FIELD_DST_OFFSET)
441                 {
442                         return E_INVALID_ARG;
443                 }
444
445                 field = GetIcuTimeField(field);
446                 if (field != UCAL_FIELD_COUNT)
447                 {
448                         IcuErrorCode err;
449                         __pIcuCalendar->roll(static_cast< UCalendarDateFields >(field), amount, err);
450                         return GetOspException(err);
451                 }
452                 return E_INVALID_ARG;
453         }
454         return r;
455 }
456
457 result
458 _IcuCalendarImpl::SetFirstDayOfWeek(int dayOfWeek)
459 {
460         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
461
462         if (dayOfWeek >= UCAL_SUNDAY && dayOfWeek <= UCAL_SATURDAY)
463         {
464                 __pIcuCalendar->setFirstDayOfWeek(static_cast< UCalendarDaysOfWeek >(dayOfWeek));
465                 return E_SUCCESS;
466         }
467         return E_INVALID_ARG;
468 }
469
470
471 result
472 _IcuCalendarImpl::SetLenient(bool lenient)
473 {
474         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
475
476         __pIcuCalendar->setLenient(lenient);
477         return E_SUCCESS;
478 }
479
480 result
481 _IcuCalendarImpl::SetMinDaysInFirstWeek(short value)
482 {
483         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
484
485         __pIcuCalendar->setMinimalDaysInFirstWeek(static_cast< byte >(value));
486         return E_SUCCESS;
487 }
488
489 result
490 _IcuCalendarImpl::SetTime(int year, int month, int day, int hour, int minute, int second, int millisecond)
491 {
492         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
493         UErrorCode ec = U_ZERO_ERROR;
494         UDate old = __pIcuCalendar->getTime(ec);
495         __pIcuCalendar->set(year, month - 1, day, hour, minute, second);
496         __pIcuCalendar->set(UCAL_MILLISECOND, millisecond);
497
498         ec = U_ZERO_ERROR;
499         __pIcuCalendar->getTime(ec);
500         if (U_FAILURE(ec))
501         {
502                 ec = U_ZERO_ERROR;
503                 __pIcuCalendar->setTime(old, ec);
504                 return E_OUT_OF_RANGE;
505         }
506
507         return E_SUCCESS;
508 }
509
510 result
511 _IcuCalendarImpl::SetTimeField(int field, int value)
512 {
513         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
514
515         field = GetIcuTimeField(field);
516         if (field != UCAL_FIELD_COUNT)
517         {
518                 CheckInputFieldValueForOverFlow(field, value);
519
520                 __pIcuCalendar->set(static_cast< UCalendarDateFields >(field), ConvertFromOspTimeFieldValue(field, value));
521                 return E_SUCCESS;
522         }
523         return E_INVALID_ARG;
524 }
525
526 result
527 _IcuCalendarImpl::SetTimeInMillisec(long long millisec)
528 {
529         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
530
531         millisec -= _EPOCH_OFFSET_IN_MILLISEC;
532
533         IcuErrorCode err;
534         __pIcuCalendar->setTime(static_cast< UDate >(millisec), err);
535         return GetOspException(err);
536 }
537
538 result
539 _IcuCalendarImpl::SetTimeZone(const TimeZone& timeZone)
540 {
541         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
542
543         const _TimeZoneImpl* pTimeZoneImpl = _TimeZoneImpl::GetTimeZoneImpl(timeZone);
544         if (pTimeZoneImpl)
545         {
546                 __pIcuCalendar->adoptTimeZone(pTimeZoneImpl->GetIcuTimeZone()->clone());
547         }
548         return E_SUCCESS;
549 }
550
551 long long
552 _IcuCalendarImpl::GetGregorianChange(void) const
553 {
554         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
555         IcuGregorianCalendar* pCal = dynamic_cast< IcuGregorianCalendar* >(__pIcuCalendar);
556         if (pCal != null)
557         {
558                 return pCal->getGregorianChange() + _EPOCH_OFFSET_IN_MILLISEC;
559         }
560         return -1;
561 }
562
563 result
564 _IcuCalendarImpl::SetGregorianChange(long long change)
565 {
566         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
567         IcuGregorianCalendar* pCal = dynamic_cast< IcuGregorianCalendar* >(__pIcuCalendar);
568         if (pCal)
569         {
570                 change -= _EPOCH_OFFSET_IN_MILLISEC;
571                 IcuErrorCode err;
572                 pCal->setGregorianChange(change, err);
573                 return GetOspException(err);
574         }
575         return E_UNSUPPORTED_OPERATION;
576 }
577
578 int
579 _IcuCalendarImpl::GetJulianDay(void) const
580 {
581         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
582         IcuGregorianCalendar* pCal = dynamic_cast< IcuGregorianCalendar* >(__pIcuCalendar);
583         if (pCal)
584         {
585                 IcuErrorCode err;
586                 return pCal->get(UCAL_JULIAN_DAY, err);
587         }
588         return -1;
589 }
590
591 result
592 _IcuCalendarImpl::SetJulianDay(int julianDay)
593 {
594         SysAssertf(__pIcuCalendar != null, "Not yet constructed! Construct() should be called before use.");
595         IcuGregorianCalendar* pCal = dynamic_cast< IcuGregorianCalendar* >(__pIcuCalendar);
596         if (pCal)
597         {
598                 pCal->set(UCAL_JULIAN_DAY, julianDay);
599                 return E_SUCCESS;
600         }
601         return E_UNSUPPORTED_OPERATION;
602 }
603
604 result
605 _IcuCalendarImpl::GetOspException(IcuErrorCode icuException) const
606 {
607         if (U_SUCCESS(icuException))
608         {
609                 return E_SUCCESS;
610         }
611
612         //SysTryLog(NID_LCL, false, "ICU Exception: %s", icuException.errorName());
613
614         result r = E_SUCCESS;
615         switch (icuException)
616         {
617         case U_ILLEGAL_ARGUMENT_ERROR:
618         {
619                 if (IsLenient())
620                 {
621                         r = E_OUT_OF_RANGE;
622                         break;
623                 }
624                 r = E_INVALID_STATE;
625                 break;
626         }
627         case U_MEMORY_ALLOCATION_ERROR:
628         {
629                 r = E_OUT_OF_MEMORY;
630                 break;
631         }
632         default:
633         {
634                 r = E_SYSTEM;
635                 break;
636         }
637         }
638         return r;
639 }
640
641 int
642 _IcuCalendarImpl::GetIcuTimeField(int field) const
643 {
644         int res = UCAL_FIELD_COUNT;
645         switch (field)
646         {
647         case TIME_FIELD_ERA:
648         {
649                 res = UCAL_ERA;
650                 break;
651         }
652
653         case TIME_FIELD_YEAR:
654         {
655                 res = UCAL_YEAR;
656                 break;
657         }
658
659         case TIME_FIELD_MONTH:
660         {
661                 res = UCAL_MONTH;
662                 break;
663         }
664
665         case TIME_FIELD_WEEK_OF_YEAR:
666         {
667                 res = UCAL_WEEK_OF_YEAR;
668                 break;
669         }
670
671         case TIME_FIELD_WEEK_OF_MONTH:
672         {
673                 res = UCAL_WEEK_OF_MONTH;
674                 break;
675         }
676
677         case TIME_FIELD_DAY_OF_MONTH:
678         {
679                 res = UCAL_DAY_OF_MONTH;
680                 break;
681         }
682
683         case TIME_FIELD_DAY_OF_YEAR:
684         {
685                 res = UCAL_DAY_OF_YEAR;
686                 break;
687         }
688
689         case TIME_FIELD_DAY_OF_WEEK:
690         {
691                 res = UCAL_DAY_OF_WEEK;
692                 break;
693         }
694
695         case TIME_FIELD_DAY_OF_WEEK_IN_MONTH:
696         {
697                 res = UCAL_DAY_OF_WEEK_IN_MONTH;
698                 break;
699         }
700
701         case TIME_FIELD_AM_PM:
702         {
703                 res = UCAL_AM_PM;
704                 break;
705         }
706
707         case TIME_FIELD_HOUR:
708         {
709                 res = UCAL_HOUR;
710                 break;
711         }
712
713         case TIME_FIELD_HOUR_OF_DAY:
714         {
715                 res = UCAL_HOUR_OF_DAY;
716                 break;
717         }
718
719         case TIME_FIELD_MINUTE:
720         {
721                 res = UCAL_MINUTE;
722                 break;
723         }
724
725         case TIME_FIELD_SECOND:
726         {
727                 res = UCAL_SECOND;
728                 break;
729         }
730
731         case TIME_FIELD_MILLISECOND:
732         {
733                 res = UCAL_MILLISECOND;
734                 break;
735         }
736
737         case TIME_FIELD_ZONE_OFFSET:
738         {
739                 res = UCAL_ZONE_OFFSET;
740                 break;
741         }
742
743         case TIME_FIELD_DST_OFFSET:
744         {
745                 res = UCAL_DST_OFFSET;
746                 break;
747         }
748
749         default:
750         {
751                 res = UCAL_FIELD_COUNT;
752                 break;
753         }
754         }
755         return res;
756 }
757
758 int
759 _IcuCalendarImpl::ConvertFromOspTimeFieldValue(int field, int value) const
760 {
761         int res = value;
762         switch (field)
763         {
764         case TIME_FIELD_MONTH:
765                 res -= 1;
766                 break;
767
768         case TIME_FIELD_ZONE_OFFSET:    // fall through
769         case TIME_FIELD_DST_OFFSET:     // fall through
770 // Uncomment these lines if OSP values are set to be in minutes
771 //        res *= Calendar::ONE_MINUTE_IN_MILLISEC;
772 //        break;
773
774         case TIME_FIELD_ERA:                    // fall through
775         case TIME_FIELD_YEAR:                   // fall through
776         case TIME_FIELD_WEEK_OF_YEAR:           // fall through
777         case TIME_FIELD_WEEK_OF_MONTH:          // fall through
778         case TIME_FIELD_DAY_OF_MONTH:           // fall through
779         case TIME_FIELD_DAY_OF_YEAR:            // fall through
780         case TIME_FIELD_DAY_OF_WEEK:            // fall through
781         case TIME_FIELD_DAY_OF_WEEK_IN_MONTH:   // fall through
782         case TIME_FIELD_AM_PM:                  // fall through
783         case TIME_FIELD_HOUR:                   // fall through
784         case TIME_FIELD_HOUR_OF_DAY:            // fall through
785         case TIME_FIELD_MINUTE:                 // fall through
786         case TIME_FIELD_SECOND:                 // fall through
787         case TIME_FIELD_MILLISECOND:            // fall through
788         default:
789                 break;
790         }
791
792         return res;
793 }
794
795 int
796 _IcuCalendarImpl::ConvertToOspTimeFieldValue(int field, int value) const
797 {
798         int res = value;
799         switch (field)
800         {
801         case TIME_FIELD_MONTH:
802                 res += 1;
803                 break;
804
805         case TIME_FIELD_ZONE_OFFSET:    // fall through
806         case TIME_FIELD_DST_OFFSET:     // fall through
807 // Uncomment these lines if OSP values are set to be in minutes
808 //        res /= Calendar::ONE_MINUTE_IN_MILLISEC;
809 //        break;
810
811         case TIME_FIELD_ERA:                    // fall through
812         case TIME_FIELD_YEAR:                   // fall through
813         case TIME_FIELD_WEEK_OF_YEAR:           // fall through
814         case TIME_FIELD_WEEK_OF_MONTH:          // fall through
815         case TIME_FIELD_DAY_OF_MONTH:           // fall through
816         case TIME_FIELD_DAY_OF_YEAR:            // fall through
817         case TIME_FIELD_DAY_OF_WEEK:            // fall through
818         case TIME_FIELD_DAY_OF_WEEK_IN_MONTH:   // fall through
819         case TIME_FIELD_AM_PM:                  // fall through
820         case TIME_FIELD_HOUR:                   // fall through
821         case TIME_FIELD_HOUR_OF_DAY:            // fall through
822         case TIME_FIELD_MINUTE:                 // fall through
823         case TIME_FIELD_SECOND:                 // fall through
824         case TIME_FIELD_MILLISECOND:            // fall through
825         default:
826                 break;
827         }
828
829         return res;
830 }
831
832 IcuLocale
833 _IcuCalendarImpl::GetIcuLocale(const Locale& ospLocale, int type) const
834 {
835         ClearLastResult();
836         String localeStr = ospLocale.GetLocaleCodeString();
837         if (!localeStr.IsEmpty())
838         {
839                 switch (type)
840                 {
841                 case CALENDAR_GREGORIAN:
842                         localeStr += "@calendar=gregorian";
843                         break;
844
845                 case CALENDAR_BUDDHIST:
846                         localeStr += "@calendar=buddhist";
847                         break;
848
849                 case CALENDAR_CHINESE:
850                         localeStr += "@calendar=chinese";
851                         break;
852
853                 case CALENDAR_COPTIC:
854                         localeStr += "@calendar=coptic";
855                         break;
856
857                 case CALENDAR_ETHIOPIC_AMETE_ALEM:
858                         localeStr += "@calendar=ethiopic-amete-alem";
859                         break;
860
861                 case CALENDAR_ETHIOPIC:
862                         localeStr += "@calendar=ethiopic";
863                         break;
864
865                 case CALENDAR_HEBREW:
866                         localeStr += "@calendar=hebrew";
867                         break;
868
869                 case CALENDAR_INDIAN:
870                         localeStr += "@calendar=indian";
871                         break;
872
873                 case CALENDAR_ISLAMIC:
874                         localeStr += "@calendar=islamic";
875                         break;
876
877                 case CALENDAR_ISLAMIC_CIVIL:
878                         localeStr += "@calendar=islamic-civil";
879                         break;
880
881                 case CALENDAR_JAPANESE:
882                         localeStr += "@calendar=japanese";
883                         break;
884
885                 case CALENDAR_PERSIAN:
886                         localeStr += "@calendar=persian";
887                         break;
888
889                 case CALENDAR_ROC:
890                         localeStr += "@calendar=roc";
891                         break;
892
893                 case CALENDAR_TAIWAN:
894                         localeStr += "@calendar=taiwan";
895                         break;
896
897                 case CALENDAR_ISO8601:
898                         localeStr += "@calendar=iso8601";
899                         break;
900
901                 default:
902                         localeStr += "@calendar=gregorian";
903                         break;
904                 }
905         }
906
907         std::unique_ptr< char [] > pLclStr(_StringConverter::CopyToCharArrayN(localeStr));
908         IcuLocale icuLocale = IcuLocale(pLclStr.get());
909         if (icuLocale.isBogus())
910         {
911                 SetLastResult(E_SYSTEM);
912         }
913
914         return icuLocale;
915 }
916
917 void
918 _IcuCalendarImpl::CheckInputFieldValueForOverFlow(int field, int value)
919 {
920         if (field == TIME_FIELD_ERA)
921         {
922                 __isEraFieldOverFlowed = (value < GetMinTimeField(TIME_FIELD_ERA)) || (value > GetMaxTimeField(TIME_FIELD_ERA));
923         }
924 }
925
926 result
927 _IcuCalendarImpl::ValidateFieldsForOverFlow(void) const
928 {
929         if (__isEraFieldOverFlowed)
930         {
931                 if (IsLenient())
932                 {
933                         return E_OUT_OF_RANGE;
934                 }
935                 return E_INVALID_STATE;
936         }
937         return E_SUCCESS;
938 }
939
940 IcuCalendar*
941 _IcuCalendarImpl::GetIcuCalendarCloneN(void) const
942 {
943         if (__pIcuCalendar)
944         {
945                 return __pIcuCalendar->clone();
946         }
947         return null;
948 }
949
950 };
951 };      // Tizen::Locales