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