Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/intl/icu/source/i18n/ucal.cpp
Line
Count
Source (jump to first uncovered line)
1
// © 2016 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
/*
4
*******************************************************************************
5
*   Copyright (C) 1996-2016, International Business Machines
6
*   Corporation and others.  All Rights Reserved.
7
*******************************************************************************
8
*/
9
10
#include "utypeinfo.h"  // for 'typeid' to work
11
12
#include "unicode/utypes.h"
13
14
#if !UCONFIG_NO_FORMATTING
15
16
#include "unicode/ucal.h"
17
#include "unicode/uloc.h"
18
#include "unicode/calendar.h"
19
#include "unicode/timezone.h"
20
#include "unicode/gregocal.h"
21
#include "unicode/simpletz.h"
22
#include "unicode/ustring.h"
23
#include "unicode/strenum.h"
24
#include "unicode/localpointer.h"
25
#include "cmemory.h"
26
#include "cstring.h"
27
#include "ustrenum.h"
28
#include "uenumimp.h"
29
#include "ulist.h"
30
#include "ulocimp.h"
31
32
U_NAMESPACE_USE
33
34
static TimeZone*
35
0
_createTimeZone(const UChar* zoneID, int32_t len, UErrorCode* ec) {
36
0
    TimeZone* zone = NULL;
37
0
    if (ec!=NULL && U_SUCCESS(*ec)) {
38
0
        // Note that if zoneID is invalid, we get back GMT. This odd
39
0
        // behavior is by design and goes back to the JDK. The only
40
0
        // failure we will see is a memory allocation failure.
41
0
        int32_t l = (len<0 ? u_strlen(zoneID) : len);
42
0
        UnicodeString zoneStrID;
43
0
        zoneStrID.setTo((UBool)(len < 0), zoneID, l); /* temporary read-only alias */
44
0
        zone = TimeZone::createTimeZone(zoneStrID);
45
0
        if (zone == NULL) {
46
0
            *ec = U_MEMORY_ALLOCATION_ERROR;
47
0
        }
48
0
    }
49
0
    return zone;
50
0
}
51
52
U_CAPI UEnumeration* U_EXPORT2
53
ucal_openTimeZoneIDEnumeration(USystemTimeZoneType zoneType, const char* region,
54
0
                                const int32_t* rawOffset, UErrorCode* ec) {
55
0
    return uenum_openFromStringEnumeration(TimeZone::createTimeZoneIDEnumeration(
56
0
        zoneType, region, rawOffset, *ec), ec);
57
0
}
58
59
U_CAPI UEnumeration* U_EXPORT2
60
0
ucal_openTimeZones(UErrorCode* ec) {
61
0
    return uenum_openFromStringEnumeration(TimeZone::createEnumeration(), ec);
62
0
}
63
64
U_CAPI UEnumeration* U_EXPORT2
65
0
ucal_openCountryTimeZones(const char* country, UErrorCode* ec) {
66
0
    return uenum_openFromStringEnumeration(TimeZone::createEnumeration(country), ec);
67
0
}
68
69
U_CAPI int32_t U_EXPORT2
70
0
ucal_getDefaultTimeZone(UChar* result, int32_t resultCapacity, UErrorCode* ec) {
71
0
    int32_t len = 0;
72
0
    if (ec!=NULL && U_SUCCESS(*ec)) {
73
0
        TimeZone* zone = TimeZone::createDefault();
74
0
        if (zone == NULL) {
75
0
            *ec = U_MEMORY_ALLOCATION_ERROR;
76
0
        } else {
77
0
            UnicodeString id;
78
0
            zone->getID(id);
79
0
            delete zone;
80
0
            len = id.extract(result, resultCapacity, *ec);
81
0
        }
82
0
    }
83
0
    return len;
84
0
}
85
86
U_CAPI void U_EXPORT2
87
0
ucal_setDefaultTimeZone(const UChar* zoneID, UErrorCode* ec) {
88
0
    TimeZone* zone = _createTimeZone(zoneID, -1, ec);
89
0
    if (zone != NULL) {
90
0
        TimeZone::adoptDefault(zone);
91
0
    }
92
0
}
93
94
U_CAPI int32_t U_EXPORT2
95
0
ucal_getDSTSavings(const UChar* zoneID, UErrorCode* ec) {
96
0
    int32_t result = 0;
97
0
    TimeZone* zone = _createTimeZone(zoneID, -1, ec);
98
0
    if (U_SUCCESS(*ec)) {
99
0
        SimpleTimeZone* stz = dynamic_cast<SimpleTimeZone*>(zone);
100
0
        if (stz != NULL) {
101
0
            result = stz->getDSTSavings();
102
0
        } else {
103
0
            // Since there is no getDSTSavings on TimeZone, we use a
104
0
            // heuristic: Starting with the current time, march
105
0
            // forwards for one year, looking for DST savings.
106
0
            // Stepping by weeks is sufficient.
107
0
            UDate d = Calendar::getNow();
108
0
            for (int32_t i=0; i<53; ++i, d+=U_MILLIS_PER_DAY*7.0) {
109
0
                int32_t raw, dst;
110
0
                zone->getOffset(d, FALSE, raw, dst, *ec);
111
0
                if (U_FAILURE(*ec)) {
112
0
                    break;
113
0
                } else if (dst != 0) {
114
0
                    result = dst;
115
0
                    break;
116
0
                }
117
0
            }
118
0
        }
119
0
    }
120
0
    delete zone;
121
0
    return result;
122
0
}
123
124
U_CAPI UDate  U_EXPORT2
125
ucal_getNow()
126
0
{
127
0
128
0
  return Calendar::getNow();
129
0
}
130
131
0
#define ULOC_LOCALE_IDENTIFIER_CAPACITY (ULOC_FULLNAME_CAPACITY + 1 + ULOC_KEYWORD_AND_VALUES_CAPACITY)
132
133
U_CAPI UCalendar*  U_EXPORT2
134
ucal_open(  const UChar*  zoneID,
135
            int32_t       len,
136
            const char*   locale,
137
            UCalendarType caltype,
138
            UErrorCode*   status)
139
0
{
140
0
141
0
  if(U_FAILURE(*status)) return 0;
142
0
  
143
0
  TimeZone* zone = (zoneID==NULL) ? TimeZone::createDefault()
144
0
      : _createTimeZone(zoneID, len, status);
145
0
146
0
  if (U_FAILURE(*status)) {
147
0
      return NULL;
148
0
  }
149
0
150
0
  if ( caltype == UCAL_GREGORIAN ) {
151
0
      char  localeBuf[ULOC_LOCALE_IDENTIFIER_CAPACITY];
152
0
      if ( locale == NULL ) {
153
0
          locale = uloc_getDefault();
154
0
      }
155
0
      uprv_strncpy(localeBuf, locale, ULOC_LOCALE_IDENTIFIER_CAPACITY);
156
0
      uloc_setKeywordValue("calendar", "gregorian", localeBuf, ULOC_LOCALE_IDENTIFIER_CAPACITY, status);
157
0
      if (U_FAILURE(*status)) {
158
0
          return NULL;
159
0
      }
160
0
      return (UCalendar*)Calendar::createInstance(zone, Locale(localeBuf), *status);
161
0
  }
162
0
  return (UCalendar*)Calendar::createInstance(zone, Locale(locale), *status);
163
0
}
164
165
U_CAPI void U_EXPORT2
166
ucal_close(UCalendar *cal)
167
0
{
168
0
169
0
  delete (Calendar*) cal;
170
0
}
171
172
U_CAPI UCalendar* U_EXPORT2 
173
ucal_clone(const UCalendar* cal,
174
           UErrorCode*      status)
175
0
{
176
0
  if(U_FAILURE(*status)) return 0;
177
0
  
178
0
  Calendar* res = ((Calendar*)cal)->clone();
179
0
180
0
  if(res == 0) {
181
0
    *status = U_MEMORY_ALLOCATION_ERROR;
182
0
    return 0;
183
0
  }
184
0
185
0
  return (UCalendar*) res;
186
0
}
187
188
U_CAPI void  U_EXPORT2
189
ucal_setTimeZone(    UCalendar*      cal,
190
            const    UChar*            zoneID,
191
            int32_t        len,
192
            UErrorCode *status)
193
0
{
194
0
195
0
  if(U_FAILURE(*status))
196
0
    return;
197
0
198
0
  TimeZone* zone = (zoneID==NULL) ? TimeZone::createDefault()
199
0
      : _createTimeZone(zoneID, len, status);
200
0
201
0
  if (zone != NULL) {
202
0
      ((Calendar*)cal)->adoptTimeZone(zone);
203
0
  }
204
0
}
205
206
U_CAPI int32_t U_EXPORT2
207
ucal_getTimeZoneID(const UCalendar *cal,
208
                   UChar *result,
209
                   int32_t resultLength,
210
                   UErrorCode *status)
211
0
{
212
0
    if (U_FAILURE(*status)) {
213
0
        return 0;
214
0
    }
215
0
    const TimeZone& tz = ((Calendar*)cal)->getTimeZone();
216
0
    UnicodeString id;
217
0
    tz.getID(id);
218
0
    return id.extract(result, resultLength, *status);
219
0
}
220
221
U_CAPI int32_t U_EXPORT2
222
ucal_getTimeZoneDisplayName(const     UCalendar*                 cal,
223
                    UCalendarDisplayNameType     type,
224
                    const char             *locale,
225
                    UChar*                  result,
226
                    int32_t                 resultLength,
227
                    UErrorCode*             status)
228
0
{
229
0
230
0
    if(U_FAILURE(*status)) return -1;
231
0
232
0
    const TimeZone& tz = ((Calendar*)cal)->getTimeZone();
233
0
    UnicodeString id;
234
0
    if(!(result==NULL && resultLength==0)) {
235
0
        // NULL destination for pure preflighting: empty dummy string
236
0
        // otherwise, alias the destination buffer
237
0
        id.setTo(result, 0, resultLength);
238
0
    }
239
0
240
0
    switch(type) {
241
0
  case UCAL_STANDARD:
242
0
      tz.getDisplayName(FALSE, TimeZone::LONG, Locale(locale), id);
243
0
      break;
244
0
245
0
  case UCAL_SHORT_STANDARD:
246
0
      tz.getDisplayName(FALSE, TimeZone::SHORT, Locale(locale), id);
247
0
      break;
248
0
249
0
  case UCAL_DST:
250
0
      tz.getDisplayName(TRUE, TimeZone::LONG, Locale(locale), id);
251
0
      break;
252
0
253
0
  case UCAL_SHORT_DST:
254
0
      tz.getDisplayName(TRUE, TimeZone::SHORT, Locale(locale), id);
255
0
      break;
256
0
    }
257
0
258
0
    return id.extract(result, resultLength, *status);
259
0
}
260
261
U_CAPI UBool  U_EXPORT2
262
ucal_inDaylightTime(    const    UCalendar*      cal, 
263
                    UErrorCode*     status )
264
0
{
265
0
266
0
    if(U_FAILURE(*status)) return (UBool) -1;
267
0
    return ((Calendar*)cal)->inDaylightTime(*status);
268
0
}
269
270
U_CAPI void U_EXPORT2
271
0
ucal_setGregorianChange(UCalendar *cal, UDate date, UErrorCode *pErrorCode) {
272
0
    if(U_FAILURE(*pErrorCode)) {
273
0
        return;
274
0
    }
275
0
    Calendar *cpp_cal = (Calendar *)cal;
276
0
    GregorianCalendar *gregocal = dynamic_cast<GregorianCalendar *>(cpp_cal);
277
0
    // Not if(gregocal == NULL) {
278
0
    // because we really want to work only with a GregorianCalendar, not with
279
0
    // its subclasses like BuddhistCalendar.
280
0
    if (cpp_cal == NULL) {
281
0
        // We normally don't check "this" pointers for NULL, but this here avoids
282
0
        // compiler-generated exception-throwing code in case cal == NULL.
283
0
        *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
284
0
        return;
285
0
    }
286
0
    if(typeid(*cpp_cal) != typeid(GregorianCalendar)) {
287
0
        *pErrorCode = U_UNSUPPORTED_ERROR;
288
0
        return;
289
0
    }
290
0
    gregocal->setGregorianChange(date, *pErrorCode);
291
0
}
292
293
U_CAPI UDate U_EXPORT2
294
0
ucal_getGregorianChange(const UCalendar *cal, UErrorCode *pErrorCode) {
295
0
    if(U_FAILURE(*pErrorCode)) {
296
0
        return (UDate)0;
297
0
    }
298
0
    const Calendar *cpp_cal = (const Calendar *)cal;
299
0
    const GregorianCalendar *gregocal = dynamic_cast<const GregorianCalendar *>(cpp_cal);
300
0
    // Not if(gregocal == NULL) {
301
0
    // see comments in ucal_setGregorianChange().
302
0
    if (cpp_cal == NULL) {
303
0
        // We normally don't check "this" pointers for NULL, but this here avoids
304
0
        // compiler-generated exception-throwing code in case cal == NULL.
305
0
        *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
306
0
        return (UDate)0;
307
0
    }
308
0
    if(typeid(*cpp_cal) != typeid(GregorianCalendar)) {
309
0
        *pErrorCode = U_UNSUPPORTED_ERROR;
310
0
        return (UDate)0;
311
0
    }
312
0
    return gregocal->getGregorianChange();
313
0
}
314
315
U_CAPI int32_t U_EXPORT2
316
ucal_getAttribute(    const    UCalendar*              cal,
317
                  UCalendarAttribute      attr)
318
0
{
319
0
320
0
    switch(attr) {
321
0
  case UCAL_LENIENT:
322
0
      return ((Calendar*)cal)->isLenient();
323
0
324
0
  case UCAL_FIRST_DAY_OF_WEEK:
325
0
      return ((Calendar*)cal)->getFirstDayOfWeek();
326
0
327
0
  case UCAL_MINIMAL_DAYS_IN_FIRST_WEEK:
328
0
      return ((Calendar*)cal)->getMinimalDaysInFirstWeek();
329
0
330
0
  case UCAL_REPEATED_WALL_TIME:
331
0
      return ((Calendar*)cal)->getRepeatedWallTimeOption();
332
0
333
0
  case UCAL_SKIPPED_WALL_TIME:
334
0
      return ((Calendar*)cal)->getSkippedWallTimeOption();
335
0
336
0
  default:
337
0
      break;
338
0
    }
339
0
    return -1;
340
0
}
341
342
U_CAPI void U_EXPORT2
343
ucal_setAttribute(      UCalendar*              cal,
344
                  UCalendarAttribute      attr,
345
                  int32_t                 newValue)
346
{
347
348
    switch(attr) {
349
  case UCAL_LENIENT:
350
      ((Calendar*)cal)->setLenient((UBool)newValue);
351
      break;
352
353
  case UCAL_FIRST_DAY_OF_WEEK:
354
      ((Calendar*)cal)->setFirstDayOfWeek((UCalendarDaysOfWeek)newValue);
355
      break;
356
357
  case UCAL_MINIMAL_DAYS_IN_FIRST_WEEK:
358
      ((Calendar*)cal)->setMinimalDaysInFirstWeek((uint8_t)newValue);
359
      break;
360
361
  case UCAL_REPEATED_WALL_TIME:
362
      ((Calendar*)cal)->setRepeatedWallTimeOption((UCalendarWallTimeOption)newValue);
363
      break;
364
365
  case UCAL_SKIPPED_WALL_TIME:
366
      ((Calendar*)cal)->setSkippedWallTimeOption((UCalendarWallTimeOption)newValue);
367
      break;
368
    }
369
}
370
371
U_CAPI const char* U_EXPORT2
372
ucal_getAvailable(int32_t index)
373
0
{
374
0
375
0
    return uloc_getAvailable(index);
376
0
}
377
378
U_CAPI int32_t U_EXPORT2
379
ucal_countAvailable()
380
0
{
381
0
382
0
    return uloc_countAvailable();
383
0
}
384
385
U_CAPI UDate  U_EXPORT2
386
ucal_getMillis(    const    UCalendar*      cal,
387
               UErrorCode*     status)
388
0
{
389
0
390
0
    if(U_FAILURE(*status)) return (UDate) 0;
391
0
392
0
    return ((Calendar*)cal)->getTime(*status);
393
0
}
394
395
U_CAPI void  U_EXPORT2
396
ucal_setMillis(        UCalendar*      cal,
397
               UDate           dateTime,
398
               UErrorCode*     status )
399
0
{
400
0
    if(U_FAILURE(*status)) return;
401
0
402
0
    ((Calendar*)cal)->setTime(dateTime, *status);
403
0
}
404
405
// TBD: why does this take an UErrorCode?
406
U_CAPI void  U_EXPORT2
407
ucal_setDate(        UCalendar*        cal,
408
             int32_t            year,
409
             int32_t            month,
410
             int32_t            date,
411
             UErrorCode        *status)
412
0
{
413
0
414
0
    if(U_FAILURE(*status)) return;
415
0
416
0
    ((Calendar*)cal)->set(year, month, date);
417
0
}
418
419
// TBD: why does this take an UErrorCode?
420
U_CAPI void  U_EXPORT2
421
ucal_setDateTime(    UCalendar*        cal,
422
                 int32_t            year,
423
                 int32_t            month,
424
                 int32_t            date,
425
                 int32_t            hour,
426
                 int32_t            minute,
427
                 int32_t            second,
428
                 UErrorCode        *status)
429
0
{
430
0
    if(U_FAILURE(*status)) return;
431
0
432
0
    ((Calendar*)cal)->set(year, month, date, hour, minute, second);
433
0
}
434
435
U_CAPI UBool  U_EXPORT2
436
ucal_equivalentTo(    const UCalendar*      cal1,
437
                  const UCalendar*      cal2)
438
0
{
439
0
440
0
    return ((Calendar*)cal1)->isEquivalentTo(*((Calendar*)cal2));
441
0
}
442
443
U_CAPI void  U_EXPORT2
444
ucal_add(    UCalendar*                cal,
445
         UCalendarDateFields        field,
446
         int32_t                    amount,
447
         UErrorCode*                status)
448
0
{
449
0
450
0
    if(U_FAILURE(*status)) return;
451
0
452
0
    ((Calendar*)cal)->add(field, amount, *status);
453
0
}
454
455
U_CAPI void  U_EXPORT2
456
ucal_roll(        UCalendar*            cal,
457
          UCalendarDateFields field,
458
          int32_t                amount,
459
          UErrorCode*            status)
460
0
{
461
0
462
0
    if(U_FAILURE(*status)) return;
463
0
464
0
    ((Calendar*)cal)->roll(field, amount, *status);
465
0
}
466
467
U_CAPI int32_t  U_EXPORT2
468
ucal_get(    const    UCalendar*                cal,
469
         UCalendarDateFields        field,
470
         UErrorCode*                status )
471
0
{
472
0
473
0
    if(U_FAILURE(*status)) return -1;
474
0
475
0
    return ((Calendar*)cal)->get(field, *status);
476
0
}
477
478
U_CAPI void  U_EXPORT2
479
ucal_set(    UCalendar*                cal,
480
         UCalendarDateFields        field,
481
         int32_t                    value)
482
0
{
483
0
484
0
    ((Calendar*)cal)->set(field, value);
485
0
}
486
487
U_CAPI UBool  U_EXPORT2
488
ucal_isSet(    const    UCalendar*                cal,
489
           UCalendarDateFields        field)
490
0
{
491
0
492
0
    return ((Calendar*)cal)->isSet(field);
493
0
}
494
495
U_CAPI void  U_EXPORT2
496
ucal_clearField(    UCalendar*            cal,
497
                UCalendarDateFields field)
498
0
{
499
0
500
0
    ((Calendar*)cal)->clear(field);
501
0
}
502
503
U_CAPI void  U_EXPORT2
504
ucal_clear(UCalendar* calendar)
505
0
{
506
0
507
0
    ((Calendar*)calendar)->clear();
508
0
}
509
510
U_CAPI int32_t  U_EXPORT2
511
ucal_getLimit(    const    UCalendar*              cal,
512
              UCalendarDateFields     field,
513
              UCalendarLimitType      type,
514
              UErrorCode        *status)
515
0
{
516
0
517
0
    if(status==0 || U_FAILURE(*status)) {
518
0
        return -1;
519
0
    }
520
0
521
0
    switch(type) {
522
0
  case UCAL_MINIMUM:
523
0
      return ((Calendar*)cal)->getMinimum(field);
524
0
525
0
  case UCAL_MAXIMUM:
526
0
      return ((Calendar*)cal)->getMaximum(field);
527
0
528
0
  case UCAL_GREATEST_MINIMUM:
529
0
      return ((Calendar*)cal)->getGreatestMinimum(field);
530
0
531
0
  case UCAL_LEAST_MAXIMUM:
532
0
      return ((Calendar*)cal)->getLeastMaximum(field);
533
0
534
0
  case UCAL_ACTUAL_MINIMUM:
535
0
      return ((Calendar*)cal)->getActualMinimum(field,
536
0
          *status);
537
0
538
0
  case UCAL_ACTUAL_MAXIMUM:
539
0
      return ((Calendar*)cal)->getActualMaximum(field,
540
0
          *status);
541
0
542
0
  default:
543
0
      break;
544
0
    }
545
0
    return -1;
546
0
}
547
548
U_CAPI const char * U_EXPORT2
549
ucal_getLocaleByType(const UCalendar *cal, ULocDataLocaleType type, UErrorCode* status) 
550
0
{
551
0
    if (cal == NULL) {
552
0
        if (U_SUCCESS(*status)) {
553
0
            *status = U_ILLEGAL_ARGUMENT_ERROR;
554
0
        }
555
0
        return NULL;
556
0
    }
557
0
    return ((Calendar*)cal)->getLocaleID(type, *status);
558
0
}
559
560
U_CAPI const char * U_EXPORT2
561
ucal_getTZDataVersion(UErrorCode* status)
562
0
{
563
0
    return TimeZone::getTZDataVersion(*status);
564
0
}
565
566
U_CAPI int32_t U_EXPORT2
567
ucal_getCanonicalTimeZoneID(const UChar* id, int32_t len,
568
0
                            UChar* result, int32_t resultCapacity, UBool *isSystemID, UErrorCode* status) {
569
0
    if(status == 0 || U_FAILURE(*status)) {
570
0
        return 0;
571
0
    }
572
0
    if (isSystemID) {
573
0
        *isSystemID = FALSE;
574
0
    }
575
0
    if (id == 0 || len == 0 || result == 0 || resultCapacity <= 0) {
576
0
        *status = U_ILLEGAL_ARGUMENT_ERROR;
577
0
        return 0;
578
0
    }
579
0
    int32_t reslen = 0;
580
0
    UnicodeString canonical;
581
0
    UBool systemID = FALSE;
582
0
    TimeZone::getCanonicalID(UnicodeString(id, len), canonical, systemID, *status);
583
0
    if (U_SUCCESS(*status)) {
584
0
        if (isSystemID) {
585
0
            *isSystemID = systemID;
586
0
        }
587
0
        reslen = canonical.extract(result, resultCapacity, *status);
588
0
    }
589
0
    return reslen;
590
0
}
591
592
U_CAPI const char * U_EXPORT2
593
ucal_getType(const UCalendar *cal, UErrorCode* status)
594
0
{
595
0
    if (U_FAILURE(*status)) {
596
0
        return NULL;
597
0
    }
598
0
    return ((Calendar*)cal)->getType();
599
0
}
600
601
U_CAPI UCalendarWeekdayType U_EXPORT2
602
ucal_getDayOfWeekType(const UCalendar *cal, UCalendarDaysOfWeek dayOfWeek, UErrorCode* status)
603
0
{
604
0
    if (U_FAILURE(*status)) {
605
0
        return UCAL_WEEKDAY;
606
0
    }
607
0
    return ((Calendar*)cal)->getDayOfWeekType(dayOfWeek, *status);
608
0
}
609
610
U_CAPI int32_t U_EXPORT2
611
ucal_getWeekendTransition(const UCalendar *cal, UCalendarDaysOfWeek dayOfWeek, UErrorCode *status)
612
0
{
613
0
    if (U_FAILURE(*status)) {
614
0
        return 0;
615
0
    }
616
0
    return ((Calendar*)cal)->getWeekendTransition(dayOfWeek, *status);
617
0
}
618
619
U_CAPI UBool U_EXPORT2
620
ucal_isWeekend(const UCalendar *cal, UDate date, UErrorCode *status)
621
0
{
622
0
    if (U_FAILURE(*status)) {
623
0
        return FALSE;
624
0
    }
625
0
    return ((Calendar*)cal)->isWeekend(date, *status);
626
0
}
627
628
U_CAPI int32_t  U_EXPORT2
629
ucal_getFieldDifference(UCalendar* cal, UDate target,
630
                        UCalendarDateFields field,
631
                        UErrorCode* status )
632
0
{
633
0
    if (U_FAILURE(*status)) {
634
0
        return 0;
635
0
    }
636
0
    return ((Calendar*)cal)->fieldDifference(target, field, *status);
637
0
}
638
639
640
static const UEnumeration defaultKeywordValues = {
641
    NULL,
642
    NULL,
643
    ulist_close_keyword_values_iterator,
644
    ulist_count_keyword_values,
645
    uenum_unextDefault,
646
    ulist_next_keyword_value, 
647
    ulist_reset_keyword_values_iterator
648
};
649
650
static const char * const CAL_TYPES[] = {
651
        "gregorian",
652
        "japanese",
653
        "buddhist",
654
        "roc",
655
        "persian",
656
        "islamic-civil",
657
        "islamic",
658
        "hebrew",
659
        "chinese",
660
        "indian",
661
        "coptic",
662
        "ethiopic",
663
        "ethiopic-amete-alem",
664
        "iso8601",
665
        "dangi",
666
        "islamic-umalqura",
667
        "islamic-tbla",
668
        "islamic-rgsa",
669
        NULL
670
};
671
672
U_CAPI UEnumeration* U_EXPORT2
673
0
ucal_getKeywordValuesForLocale(const char * /* key */, const char* locale, UBool commonlyUsed, UErrorCode *status) {
674
0
    // Resolve region
675
0
    char prefRegion[ULOC_COUNTRY_CAPACITY];
676
0
    (void)ulocimp_getRegionForSupplementalData(locale, TRUE, prefRegion, sizeof(prefRegion), status);
677
0
    
678
0
    // Read preferred calendar values from supplementalData calendarPreference
679
0
    UResourceBundle *rb = ures_openDirect(NULL, "supplementalData", status);
680
0
    ures_getByKey(rb, "calendarPreferenceData", rb, status);
681
0
    UResourceBundle *order = ures_getByKey(rb, prefRegion, NULL, status);
682
0
    if (*status == U_MISSING_RESOURCE_ERROR && rb != NULL) {
683
0
        *status = U_ZERO_ERROR;
684
0
        order = ures_getByKey(rb, "001", NULL, status);
685
0
    }
686
0
687
0
    // Create a list of calendar type strings
688
0
    UList *values = NULL;
689
0
    if (U_SUCCESS(*status)) {
690
0
        values = ulist_createEmptyList(status);
691
0
        if (U_SUCCESS(*status)) {
692
0
            for (int i = 0; i < ures_getSize(order); i++) {
693
0
                int32_t len;
694
0
                const UChar *type = ures_getStringByIndex(order, i, &len, status);
695
0
                char *caltype = (char*)uprv_malloc(len + 1);
696
0
                if (caltype == NULL) {
697
0
                    *status = U_MEMORY_ALLOCATION_ERROR;
698
0
                    break;
699
0
                }
700
0
                u_UCharsToChars(type, caltype, len);
701
0
                *(caltype + len) = 0;
702
0
703
0
                ulist_addItemEndList(values, caltype, TRUE, status);
704
0
                if (U_FAILURE(*status)) {
705
0
                    break;
706
0
                }
707
0
            }
708
0
709
0
            if (U_SUCCESS(*status) && !commonlyUsed) {
710
0
                // If not commonlyUsed, add other available values
711
0
                for (int32_t i = 0; CAL_TYPES[i] != NULL; i++) {
712
0
                    if (!ulist_containsString(values, CAL_TYPES[i], (int32_t)uprv_strlen(CAL_TYPES[i]))) {
713
0
                        ulist_addItemEndList(values, CAL_TYPES[i], FALSE, status);
714
0
                        if (U_FAILURE(*status)) {
715
0
                            break;
716
0
                        }
717
0
                    }
718
0
                }
719
0
            }
720
0
            if (U_FAILURE(*status)) {
721
0
                ulist_deleteList(values);
722
0
                values = NULL;
723
0
            }
724
0
        }
725
0
    }
726
0
727
0
    ures_close(order);
728
0
    ures_close(rb);
729
0
730
0
    if (U_FAILURE(*status) || values == NULL) {
731
0
        return NULL;
732
0
    }
733
0
734
0
    // Create string enumeration
735
0
    UEnumeration *en = (UEnumeration*)uprv_malloc(sizeof(UEnumeration));
736
0
    if (en == NULL) {
737
0
        *status = U_MEMORY_ALLOCATION_ERROR;
738
0
        ulist_deleteList(values);
739
0
        return NULL;
740
0
    }
741
0
    ulist_resetList(values);
742
0
    memcpy(en, &defaultKeywordValues, sizeof(UEnumeration));
743
0
    en->context = values;
744
0
    return en;
745
0
}
746
747
U_CAPI UBool U_EXPORT2 
748
ucal_getTimeZoneTransitionDate(const UCalendar* cal, UTimeZoneTransitionType type,
749
                               UDate* transition, UErrorCode* status)
750
0
{
751
0
    if (U_FAILURE(*status)) {
752
0
        return FALSE;
753
0
    }
754
0
    UDate base = ((Calendar*)cal)->getTime(*status);
755
0
    const TimeZone& tz = ((Calendar*)cal)->getTimeZone();
756
0
    const BasicTimeZone * btz = dynamic_cast<const BasicTimeZone *>(&tz);
757
0
    if (btz != NULL && U_SUCCESS(*status)) {
758
0
        TimeZoneTransition tzt;
759
0
        UBool inclusive = (type == UCAL_TZ_TRANSITION_NEXT_INCLUSIVE || type == UCAL_TZ_TRANSITION_PREVIOUS_INCLUSIVE);
760
0
        UBool result = (type == UCAL_TZ_TRANSITION_NEXT || type == UCAL_TZ_TRANSITION_NEXT_INCLUSIVE)?
761
0
                        btz->getNextTransition(base, inclusive, tzt):
762
0
                        btz->getPreviousTransition(base, inclusive, tzt);
763
0
        if (result) {
764
0
            *transition = tzt.getTime();
765
0
            return TRUE;
766
0
        }
767
0
    }
768
0
    return FALSE;
769
0
}
770
771
U_CAPI int32_t U_EXPORT2
772
0
ucal_getWindowsTimeZoneID(const UChar* id, int32_t len, UChar* winid, int32_t winidCapacity, UErrorCode* status) {
773
0
    if (U_FAILURE(*status)) {
774
0
        return 0;
775
0
    }
776
0
777
0
    int32_t resultLen = 0;
778
0
    UnicodeString resultWinID;
779
0
780
0
    TimeZone::getWindowsID(UnicodeString(id, len), resultWinID, *status);
781
0
    if (U_SUCCESS(*status) && resultWinID.length() > 0) {
782
0
        resultLen = resultWinID.length();
783
0
        resultWinID.extract(winid, winidCapacity, *status);
784
0
    }
785
0
786
0
    return resultLen;
787
0
}
788
789
U_CAPI int32_t U_EXPORT2
790
0
ucal_getTimeZoneIDForWindowsID(const UChar* winid, int32_t len, const char* region, UChar* id, int32_t idCapacity, UErrorCode* status) {
791
0
    if (U_FAILURE(*status)) {
792
0
        return 0;
793
0
    }
794
0
795
0
    int32_t resultLen = 0;
796
0
    UnicodeString resultID;
797
0
798
0
    TimeZone::getIDForWindowsID(UnicodeString(winid, len), region, resultID, *status);
799
0
    if (U_SUCCESS(*status) && resultID.length() > 0) {
800
0
        resultLen = resultID.length();
801
0
        resultID.extract(id, idCapacity, *status);
802
0
    }
803
0
804
0
    return resultLen;
805
0
}
806
807
#endif /* #if !UCONFIG_NO_FORMATTING */