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