1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 1997-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
11 * Modification History:
13 * Date Name Description
14 * 12/05/96 clhuang Creation.
15 * 04/21/97 aliu General clean-up and bug fixing.
16 * 05/08/97 aliu Fixed Hashtable code per code review.
17 * 07/09/97 helena Changed createInstance to createDefault.
18 * 07/29/97 aliu Updated with all-new list of 96 UNIX-derived
19 * TimeZones. Changed mechanism to load from static
20 * array rather than resource bundle.
21 * 07/07/1998 srl Bugfixes from the Java side: UTC GMT CAT NST
22 * Added getDisplayName API
23 * going to add custom parsing.
26 * - should getDisplayName cache something?
27 * - should custom time zones be cached? [probably]
28 * 08/10/98 stephen Brought getDisplayName() API in-line w/ conventions
29 * 08/19/98 stephen Changed createTimeZone() to never return 0
30 * 09/02/98 stephen Added getOffset(monthLen) and hasSameRules()
31 * 09/15/98 stephen Added getStaticClassID()
32 * 02/22/99 stephen Removed character literals for EBCDIC safety
33 * 05/04/99 stephen Changed initDefault() for Mutex issues
34 * 07/12/99 helena HPUX 11 CC Port.
35 * 12/03/99 aliu Moved data out of static table into icudata.dll.
36 * Substantial rewrite of zone lookup, default zone, and
37 * available IDs code. Misc. cleanup.
38 *********************************************************************************/
40 #include "utypeinfo.h" // for 'typeid' to work
42 #include "unicode/utypes.h"
43 #include "unicode/ustring.h"
49 # include "uresimp.h" // for debugging
51 static void debug_tz_loc(const char *f
, int32_t l
)
53 fprintf(stderr
, "%s:%d: ", f
, l
);
56 static void debug_tz_msg(const char *pat
, ...)
60 vfprintf(stderr
, pat
, ap
);
63 static char gStrBuf
[256];
64 #define U_DEBUG_TZ_STR(x) u_austrncpy(gStrBuf,x,sizeof(gStrBuf)-1)
65 // must use double parens, i.e.: U_DEBUG_TZ_MSG(("four is: %d",4));
66 #define U_DEBUG_TZ_MSG(x) {debug_tz_loc(__FILE__,__LINE__);debug_tz_msg x;}
68 #define U_DEBUG_TZ_MSG(x)
71 #if !UCONFIG_NO_FORMATTING
73 #include "unicode/simpletz.h"
74 #include "unicode/calendar.h"
75 #include "unicode/gregocal.h"
76 #include "unicode/ures.h"
77 #include "unicode/tzfmt.h"
78 #include "unicode/numfmt.h"
80 #include "uresimp.h" // struct UResourceBundle
83 #include "unicode/udata.h"
87 #include "unicode/strenum.h"
91 #define kZONEINFO "zoneinfo64"
92 #define kREGIONS "Regions"
93 #define kZONES "Zones"
94 #define kRULES "Rules"
95 #define kNAMES "Names"
96 #define kTZVERSION "TZVersion"
97 #define kLINKS "links"
98 #define kMAX_CUSTOM_HOUR 23
99 #define kMAX_CUSTOM_MIN 59
100 #define kMAX_CUSTOM_SEC 59
103 #define ZERO_DIGIT 0x0030
106 // Static data and constants
108 static const UChar WORLD
[] = {0x30, 0x30, 0x31, 0x00}; /* "001" */
110 static const UChar GMT_ID
[] = {0x47, 0x4D, 0x54, 0x00}; /* "GMT" */
111 static const UChar UNKNOWN_ZONE_ID
[] = {0x45, 0x74, 0x63, 0x2F, 0x55, 0x6E, 0x6B, 0x6E, 0x6F, 0x77, 0x6E, 0x00}; /* "Etc/Unknown" */
112 static const int32_t GMT_ID_LENGTH
= 3;
113 static const int32_t UNKNOWN_ZONE_ID_LENGTH
= 11;
115 static icu::TimeZone
* DEFAULT_ZONE
= NULL
;
116 static icu::UInitOnce gDefaultZoneInitOnce
= U_INITONCE_INITIALIZER
;
118 alignas(icu::SimpleTimeZone
)
119 static char gRawGMT
[sizeof(icu::SimpleTimeZone
)];
121 alignas(icu::SimpleTimeZone
)
122 static char gRawUNKNOWN
[sizeof(icu::SimpleTimeZone
)];
124 static icu::UInitOnce gStaticZonesInitOnce
= U_INITONCE_INITIALIZER
;
125 static UBool gStaticZonesInitialized
= FALSE
; // Whether the static zones are initialized and ready to use.
127 static char TZDATA_VERSION
[16];
128 static icu::UInitOnce gTZDataVersionInitOnce
= U_INITONCE_INITIALIZER
;
130 static int32_t* MAP_SYSTEM_ZONES
= NULL
;
131 static int32_t* MAP_CANONICAL_SYSTEM_ZONES
= NULL
;
132 static int32_t* MAP_CANONICAL_SYSTEM_LOCATION_ZONES
= NULL
;
134 static int32_t LEN_SYSTEM_ZONES
= 0;
135 static int32_t LEN_CANONICAL_SYSTEM_ZONES
= 0;
136 static int32_t LEN_CANONICAL_SYSTEM_LOCATION_ZONES
= 0;
138 static icu::UInitOnce gSystemZonesInitOnce
= U_INITONCE_INITIALIZER
;
139 static icu::UInitOnce gCanonicalZonesInitOnce
= U_INITONCE_INITIALIZER
;
140 static icu::UInitOnce gCanonicalLocationZonesInitOnce
= U_INITONCE_INITIALIZER
;
143 static UBool U_CALLCONV
timeZone_cleanup(void)
148 gDefaultZoneInitOnce
.reset();
150 if (gStaticZonesInitialized
) {
151 reinterpret_cast<SimpleTimeZone
*>(gRawGMT
)->~SimpleTimeZone();
152 reinterpret_cast<SimpleTimeZone
*>(gRawUNKNOWN
)->~SimpleTimeZone();
153 gStaticZonesInitialized
= FALSE
;
154 gStaticZonesInitOnce
.reset();
157 uprv_memset(TZDATA_VERSION
, 0, sizeof(TZDATA_VERSION
));
158 gTZDataVersionInitOnce
.reset();
160 LEN_SYSTEM_ZONES
= 0;
161 uprv_free(MAP_SYSTEM_ZONES
);
162 MAP_SYSTEM_ZONES
= 0;
163 gSystemZonesInitOnce
.reset();
165 LEN_CANONICAL_SYSTEM_ZONES
= 0;
166 uprv_free(MAP_CANONICAL_SYSTEM_ZONES
);
167 MAP_CANONICAL_SYSTEM_ZONES
= 0;
168 gCanonicalZonesInitOnce
.reset();
170 LEN_CANONICAL_SYSTEM_LOCATION_ZONES
= 0;
171 uprv_free(MAP_CANONICAL_SYSTEM_LOCATION_ZONES
);
172 MAP_CANONICAL_SYSTEM_LOCATION_ZONES
= 0;
173 gCanonicalLocationZonesInitOnce
.reset();
181 static int32_t findInStringArray(UResourceBundle
* array
, const UnicodeString
& id
, UErrorCode
&status
)
188 int32_t limit
= ures_getSize(array
);
190 int32_t lastMid
= INT32_MAX
;
191 if(U_FAILURE(status
) || (limit
< 1)) {
194 U_DEBUG_TZ_MSG(("fisa: Looking for %s, between %d and %d\n", U_DEBUG_TZ_STR(UnicodeString(id
).getTerminatedBuffer()), start
, limit
));
197 mid
= (int32_t)((start
+ limit
) / 2);
198 if (lastMid
== mid
) { /* Have we moved? */
199 break; /* We haven't moved, and it wasn't found. */
202 u
= ures_getStringByIndex(array
, mid
, &len
, &status
);
203 if (U_FAILURE(status
)) {
206 U_DEBUG_TZ_MSG(("tz: compare to %s, %d .. [%d] .. %d\n", U_DEBUG_TZ_STR(u
), start
, mid
, limit
));
207 copy
.setTo(TRUE
, u
, len
);
208 int r
= id
.compare(copy
);
210 U_DEBUG_TZ_MSG(("fisa: found at %d\n", mid
));
218 U_DEBUG_TZ_MSG(("fisa: not found\n"));
223 * Fetch a specific zone by name. Replaces the getByKey call.
224 * @param top Top timezone resource
225 * @param id Time zone ID
226 * @param oldbundle Bundle for reuse (or NULL). see 'ures_open()'
227 * @return the zone's bundle if found, or undefined if error. Reuses oldbundle.
229 static UResourceBundle
* getZoneByName(const UResourceBundle
* top
, const UnicodeString
& id
, UResourceBundle
*oldbundle
, UErrorCode
& status
) {
230 // load the Rules object
231 UResourceBundle
*tmp
= ures_getByKey(top
, kNAMES
, NULL
, &status
);
233 // search for the string
234 int32_t idx
= findInStringArray(tmp
, id
, status
);
236 if((idx
== -1) && U_SUCCESS(status
)) {
238 status
= U_MISSING_RESOURCE_ERROR
;
239 //ures_close(oldbundle);
242 U_DEBUG_TZ_MSG(("gzbn: oldbundle= size %d, type %d, %s\n", ures_getSize(tmp
), ures_getType(tmp
), u_errorName(status
)));
243 tmp
= ures_getByKey(top
, kZONES
, tmp
, &status
); // get Zones object from top
244 U_DEBUG_TZ_MSG(("gzbn: loaded ZONES, size %d, type %d, path %s %s\n", ures_getSize(tmp
), ures_getType(tmp
), ures_getPath(tmp
), u_errorName(status
)));
245 oldbundle
= ures_getByIndex(tmp
, idx
, oldbundle
, &status
); // get nth Zone object
246 U_DEBUG_TZ_MSG(("gzbn: loaded z#%d, size %d, type %d, path %s, %s\n", idx
, ures_getSize(oldbundle
), ures_getType(oldbundle
), ures_getPath(oldbundle
), u_errorName(status
)));
249 if(U_FAILURE(status
)) {
250 //ures_close(oldbundle);
258 UResourceBundle
* TimeZone::loadRule(const UResourceBundle
* top
, const UnicodeString
& ruleid
, UResourceBundle
* oldbundle
, UErrorCode
& status
) {
260 ruleid
.extract(0, sizeof(key
)-1, key
, (int32_t)sizeof(key
)-1, US_INV
);
261 U_DEBUG_TZ_MSG(("loadRule(%s)\n", key
));
262 UResourceBundle
*r
= ures_getByKey(top
, kRULES
, oldbundle
, &status
);
263 U_DEBUG_TZ_MSG(("loadRule(%s) -> kRULES [%s]\n", key
, u_errorName(status
)));
264 r
= ures_getByKey(r
, key
, r
, &status
);
265 U_DEBUG_TZ_MSG(("loadRule(%s) -> item [%s]\n", key
, u_errorName(status
)));
270 * Given an ID, open the appropriate resource for the given time zone.
271 * Dereference aliases if necessary.
273 * @param res resource, which must be ready for use (initialized but not open)
274 * @param ec input-output error code
275 * @return top-level resource bundle
277 static UResourceBundle
* openOlsonResource(const UnicodeString
& id
,
278 UResourceBundle
& res
,
283 id
.extract(0, sizeof(buf
)-1, buf
, sizeof(buf
), "");
285 UResourceBundle
*top
= ures_openDirect(0, kZONEINFO
, &ec
);
286 U_DEBUG_TZ_MSG(("pre: res sz=%d\n", ures_getSize(&res
)));
287 /* &res = */ getZoneByName(top
, id
, &res
, ec
);
288 // Dereference if this is an alias. Docs say result should be 1
289 // but it is 0 in 2.8 (?).
290 U_DEBUG_TZ_MSG(("Loading zone '%s' (%s, size %d) - %s\n", buf
, ures_getKey((UResourceBundle
*)&res
), ures_getSize(&res
), u_errorName(ec
)));
291 if (ures_getType(&res
) == URES_INT
) {
292 int32_t deref
= ures_getInt(&res
, &ec
) + 0;
293 U_DEBUG_TZ_MSG(("getInt: %s - type is %d\n", u_errorName(ec
), ures_getType(&res
)));
294 UResourceBundle
*ares
= ures_getByKey(top
, kZONES
, NULL
, &ec
); // dereference Zones section
295 ures_getByIndex(ares
, deref
, &res
, &ec
);
297 U_DEBUG_TZ_MSG(("alias to #%d (%s) - %s\n", deref
, "??", u_errorName(ec
)));
299 U_DEBUG_TZ_MSG(("not an alias - size %d\n", ures_getSize(&res
)));
301 U_DEBUG_TZ_MSG(("%s - final status is %s\n", buf
, u_errorName(ec
)));
305 // -------------------------------------
309 void U_CALLCONV
initStaticTimeZones() {
310 // Initialize _GMT independently of other static data; it should
311 // be valid even if we can't load the time zone UDataMemory.
312 ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE
, timeZone_cleanup
);
314 // new can't fail below, as we use placement new into staticly allocated space.
315 new(gRawGMT
) SimpleTimeZone(0, UnicodeString(TRUE
, GMT_ID
, GMT_ID_LENGTH
));
316 new(gRawUNKNOWN
) SimpleTimeZone(0, UnicodeString(TRUE
, UNKNOWN_ZONE_ID
, UNKNOWN_ZONE_ID_LENGTH
));
318 gStaticZonesInitialized
= TRUE
;
321 } // anonymous namespace
323 const TimeZone
& U_EXPORT2
324 TimeZone::getUnknown()
326 umtx_initOnce(gStaticZonesInitOnce
, &initStaticTimeZones
);
327 return *reinterpret_cast<SimpleTimeZone
*>(gRawUNKNOWN
);
330 const TimeZone
* U_EXPORT2
331 TimeZone::getGMT(void)
333 umtx_initOnce(gStaticZonesInitOnce
, &initStaticTimeZones
);
334 return reinterpret_cast<SimpleTimeZone
*>(gRawGMT
);
337 // *****************************************************************************
339 // *****************************************************************************
341 UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(TimeZone
)
348 // -------------------------------------
350 TimeZone::TimeZone(const UnicodeString
&id
)
355 // -------------------------------------
357 TimeZone::~TimeZone()
361 // -------------------------------------
363 TimeZone::TimeZone(const TimeZone
&source
)
364 : UObject(source
), fID(source
.fID
)
368 // -------------------------------------
371 TimeZone::operator=(const TimeZone
&right
)
373 if (this != &right
) fID
= right
.fID
;
377 // -------------------------------------
380 TimeZone::operator==(const TimeZone
& that
) const
382 return typeid(*this) == typeid(that
) &&
386 // -------------------------------------
390 createSystemTimeZone(const UnicodeString
& id
, UErrorCode
& ec
) {
395 StackUResourceBundle res
;
396 U_DEBUG_TZ_MSG(("pre-err=%s\n", u_errorName(ec
)));
397 UResourceBundle
*top
= openOlsonResource(id
, res
.ref(), ec
);
398 U_DEBUG_TZ_MSG(("post-err=%s\n", u_errorName(ec
)));
400 z
= new OlsonTimeZone(top
, res
.getAlias(), id
, ec
);
402 ec
= U_MEMORY_ALLOCATION_ERROR
;
403 U_DEBUG_TZ_MSG(("cstz: olson time zone failed to initialize - err %s\n", u_errorName(ec
)));
408 U_DEBUG_TZ_MSG(("cstz: failed to create, err %s\n", u_errorName(ec
)));
416 * Lookup the given name in our system zone table. If found,
417 * instantiate a new zone of that name and return it. If not
421 createSystemTimeZone(const UnicodeString
& id
) {
422 UErrorCode ec
= U_ZERO_ERROR
;
423 return createSystemTimeZone(id
, ec
);
429 TimeZone::createTimeZone(const UnicodeString
& ID
)
431 /* We first try to lookup the zone ID in our system list. If this
432 * fails, we try to parse it as a custom string GMT[+-]hh:mm. If
433 * all else fails, we return GMT, which is probably not what the
434 * user wants, but at least is a functioning TimeZone object.
436 * We cannot return NULL, because that would break compatibility
439 TimeZone
* result
= createSystemTimeZone(ID
);
441 if (result
== NULL
) {
442 U_DEBUG_TZ_MSG(("failed to load system time zone with id - falling to custom"));
443 result
= createCustomTimeZone(ID
);
445 if (result
== NULL
) {
446 U_DEBUG_TZ_MSG(("failed to load time zone with id - falling to Etc/Unknown(GMT)"));
447 const TimeZone
& unknown
= getUnknown();
448 // Unknown zone uses staticly allocated memory, so creation of it can never fail due to OOM.
449 result
= unknown
.clone();
454 // -------------------------------------
457 TimeZone::detectHostTimeZone()
459 // We access system timezone data through uprv_tzset(), uprv_tzname(), and others,
460 // which have platform specific implementations in putil.cpp
461 int32_t rawOffset
= 0;
463 UBool hostDetectionSucceeded
= TRUE
;
465 // First, try to create a system timezone, based
466 // on the string ID in tzname[0].
468 uprv_tzset(); // Initialize tz... system data
470 uprv_tzname_clear_cache();
472 // Get the timezone ID from the host. This function should do
473 // any required host-specific remapping; e.g., on Windows this
474 // function maps the Windows Time Zone name to an ICU timezone ID.
475 hostID
= uprv_tzname(0);
477 // Invert sign because UNIX semantics are backwards
478 rawOffset
= uprv_timezone() * -U_MILLIS_PER_SECOND
;
480 TimeZone
* hostZone
= NULL
;
482 UnicodeString
hostStrID(hostID
, -1, US_INV
);
484 if (hostStrID
.length() == 0) {
485 // The host time zone detection (or remapping) above has failed and
486 // we have no name at all. Fallback to using the Unknown zone.
487 hostStrID
= UnicodeString(TRUE
, UNKNOWN_ZONE_ID
, UNKNOWN_ZONE_ID_LENGTH
);
488 hostDetectionSucceeded
= FALSE
;
491 hostZone
= createSystemTimeZone(hostStrID
);
493 #if U_PLATFORM_USES_ONLY_WIN32_API
494 // hostID points to a heap-allocated location on Windows.
495 uprv_free(const_cast<char *>(hostID
));
498 int32_t hostIDLen
= hostStrID
.length();
499 if (hostZone
!= NULL
&& rawOffset
!= hostZone
->getRawOffset()
500 && (3 <= hostIDLen
&& hostIDLen
<= 4))
502 // Uh oh. This probably wasn't a good id.
503 // It was probably an ambiguous abbreviation
508 // Construct a fixed standard zone with the host's ID
510 if (hostZone
== NULL
&& hostDetectionSucceeded
) {
511 hostZone
= new SimpleTimeZone(rawOffset
, hostStrID
);
514 // If we _still_ don't have a time zone, use the Unknown zone.
516 // Note: This is extremely unlikely situation. If
517 // new SimpleTimeZone(...) above fails, the following
518 // code may also fail.
519 if (hostZone
== NULL
) {
520 // Unknown zone uses static allocated memory, so it must always exist.
521 // However, clone() allocates memory and can fail.
522 hostZone
= TimeZone::getUnknown().clone();
528 // -------------------------------------
531 * Initialize DEFAULT_ZONE from the system default time zone.
532 * Upon return, DEFAULT_ZONE will not be NULL, unless operator new()
535 static void U_CALLCONV
initDefault()
537 ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE
, timeZone_cleanup
);
539 // If setDefault() has already been called we can skip getting the
540 // default zone information from the system.
541 if (DEFAULT_ZONE
!= NULL
) {
545 // NOTE: this code is safely single threaded, being only
546 // run via umtx_initOnce().
548 // Some of the locale/timezone OS functions may not be thread safe,
550 // The operating system might actually use ICU to implement timezones.
551 // So we may have ICU calling ICU here, like on AIX.
552 // There shouldn't be a problem with this; initOnce does not hold a mutex
553 // while the init function is being run.
555 // The code detecting the host time zone was separated from this
556 // and implemented as TimeZone::detectHostTimeZone()
558 TimeZone
*default_zone
= TimeZone::detectHostTimeZone();
560 // The only way for DEFAULT_ZONE to be non-null at this point is if the user
561 // made a thread-unsafe call to setDefault() or adoptDefault() in another
562 // thread while this thread was doing something that required getting the default.
563 U_ASSERT(DEFAULT_ZONE
== NULL
);
565 DEFAULT_ZONE
= default_zone
;
568 // -------------------------------------
571 TimeZone::createDefault()
573 umtx_initOnce(gDefaultZoneInitOnce
, initDefault
);
574 return (DEFAULT_ZONE
!= NULL
) ? DEFAULT_ZONE
->clone() : NULL
;
577 // -------------------------------------
580 TimeZone::adoptDefault(TimeZone
* zone
)
584 TimeZone
*old
= DEFAULT_ZONE
;
587 ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE
, timeZone_cleanup
);
590 // -------------------------------------
593 TimeZone::setDefault(const TimeZone
& zone
)
595 adoptDefault(zone
.clone());
598 //----------------------------------------------------------------------
601 static void U_CALLCONV
initMap(USystemTimeZoneType type
, UErrorCode
& ec
) {
602 ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE
, timeZone_cleanup
);
604 UResourceBundle
*res
= ures_openDirect(0, kZONEINFO
, &ec
);
605 res
= ures_getByKey(res
, kNAMES
, res
, &ec
); // dereference Zones section
607 int32_t size
= ures_getSize(res
);
608 int32_t *m
= (int32_t *)uprv_malloc(size
* sizeof(int32_t));
610 ec
= U_MEMORY_ALLOCATION_ERROR
;
612 int32_t numEntries
= 0;
613 for (int32_t i
= 0; i
< size
; i
++) {
614 UnicodeString id
= ures_getUnicodeStringByIndex(res
, i
, &ec
);
618 if (0 == id
.compare(UNKNOWN_ZONE_ID
, UNKNOWN_ZONE_ID_LENGTH
)) {
619 // exclude Etc/Unknown
622 if (type
== UCAL_ZONE_TYPE_CANONICAL
|| type
== UCAL_ZONE_TYPE_CANONICAL_LOCATION
) {
623 UnicodeString canonicalID
;
624 ZoneMeta::getCanonicalCLDRID(id
, canonicalID
, ec
);
628 if (canonicalID
!= id
) {
633 if (type
== UCAL_ZONE_TYPE_CANONICAL_LOCATION
) {
634 const UChar
*region
= TimeZone::getRegion(id
, ec
);
638 if (u_strcmp(region
, WORLD
) == 0) {
639 // exclude non-location ("001")
647 m
= (int32_t *)uprv_realloc(tmp
, numEntries
* sizeof(int32_t));
649 // realloc failed.. use the original one even it has unused
655 case UCAL_ZONE_TYPE_ANY
:
656 U_ASSERT(MAP_SYSTEM_ZONES
== NULL
);
657 MAP_SYSTEM_ZONES
= m
;
658 LEN_SYSTEM_ZONES
= numEntries
;
660 case UCAL_ZONE_TYPE_CANONICAL
:
661 U_ASSERT(MAP_CANONICAL_SYSTEM_ZONES
== NULL
);
662 MAP_CANONICAL_SYSTEM_ZONES
= m
;
663 LEN_CANONICAL_SYSTEM_ZONES
= numEntries
;
665 case UCAL_ZONE_TYPE_CANONICAL_LOCATION
:
666 U_ASSERT(MAP_CANONICAL_SYSTEM_LOCATION_ZONES
== NULL
);
667 MAP_CANONICAL_SYSTEM_LOCATION_ZONES
= m
;
668 LEN_CANONICAL_SYSTEM_LOCATION_ZONES
= numEntries
;
679 * This is the default implementation for subclasses that do not
680 * override this method. This implementation calls through to the
681 * 8-argument getOffset() method after suitable computations, and
682 * correctly adjusts GMT millis to local millis when necessary.
684 void TimeZone::getOffset(UDate date
, UBool local
, int32_t& rawOffset
,
685 int32_t& dstOffset
, UErrorCode
& ec
) const {
690 rawOffset
= getRawOffset();
692 date
+= rawOffset
; // now in local standard millis
695 // When local == TRUE, date might not be in local standard
696 // millis. getOffset taking 7 parameters used here assume
697 // the given time in day is local standard time.
698 // At STD->DST transition, there is a range of time which
699 // does not exist. When 'date' is in this time range
700 // (and local == TRUE), this method interprets the specified
701 // local time as DST. At DST->STD transition, there is a
702 // range of time which occurs twice. In this case, this
703 // method interprets the specified local time as STD.
704 // To support the behavior above, we need to call getOffset
705 // (with 7 args) twice when local == true and DST is
706 // detected in the initial call.
707 for (int32_t pass
=0; ; ++pass
) {
708 int32_t year
, month
, dom
, dow
;
709 double day
= uprv_floor(date
/ U_MILLIS_PER_DAY
);
710 int32_t millis
= (int32_t) (date
- day
* U_MILLIS_PER_DAY
);
712 Grego::dayToFields(day
, year
, month
, dom
, dow
);
714 dstOffset
= getOffset(GregorianCalendar::AD
, year
, month
, dom
,
715 (uint8_t) dow
, millis
,
716 Grego::monthLength(year
, month
),
719 // Recompute if local==TRUE, dstOffset!=0.
720 if (pass
!=0 || !local
|| dstOffset
== 0) {
723 // adjust to local standard millis
728 // -------------------------------------
730 // New available IDs API as of ICU 2.4. Uses StringEnumeration API.
732 class TZEnumeration
: public StringEnumeration
{
735 // Map into to zones. Our results are zone[map[i]] for
736 // i=0..len-1, where zone[i] is the i-th Olson zone. If map==NULL
737 // then our results are zone[i] for i=0..len-1. Len will be zero
738 // if the zone data could not be loaded.
744 TZEnumeration(int32_t* mapData
, int32_t mapLen
, UBool adoptMapData
) : pos(0) {
746 localMap
= adoptMapData
? mapData
: NULL
;
750 UBool
getID(int32_t i
, UErrorCode
& ec
) {
752 const UChar
* id
= NULL
;
753 UResourceBundle
*top
= ures_openDirect(0, kZONEINFO
, &ec
);
754 top
= ures_getByKey(top
, kNAMES
, top
, &ec
); // dereference Zones section
755 id
= ures_getStringByIndex(top
, i
, &idLen
, &ec
);
760 unistr
.fastCopyFrom(UnicodeString(TRUE
, id
, idLen
));
763 return U_SUCCESS(ec
);
766 static int32_t* getMap(USystemTimeZoneType type
, int32_t& len
, UErrorCode
& ec
) {
773 case UCAL_ZONE_TYPE_ANY
:
774 umtx_initOnce(gSystemZonesInitOnce
, &initMap
, type
, ec
);
775 m
= MAP_SYSTEM_ZONES
;
776 len
= LEN_SYSTEM_ZONES
;
778 case UCAL_ZONE_TYPE_CANONICAL
:
779 umtx_initOnce(gCanonicalZonesInitOnce
, &initMap
, type
, ec
);
780 m
= MAP_CANONICAL_SYSTEM_ZONES
;
781 len
= LEN_CANONICAL_SYSTEM_ZONES
;
783 case UCAL_ZONE_TYPE_CANONICAL_LOCATION
:
784 umtx_initOnce(gCanonicalLocationZonesInitOnce
, &initMap
, type
, ec
);
785 m
= MAP_CANONICAL_SYSTEM_LOCATION_ZONES
;
786 len
= LEN_CANONICAL_SYSTEM_LOCATION_ZONES
;
789 ec
= U_ILLEGAL_ARGUMENT_ERROR
;
799 #define DEFAULT_FILTERED_MAP_SIZE 8
800 #define MAP_INCREMENT_SIZE 8
802 static TZEnumeration
* create(USystemTimeZoneType type
, const char* region
, const int32_t* rawOffset
, UErrorCode
& ec
) {
808 int32_t *baseMap
= getMap(type
, baseLen
, ec
);
814 // If any additional conditions are available,
815 // create instance local map filtered by the conditions.
817 int32_t *filteredMap
= NULL
;
818 int32_t numEntries
= 0;
820 if (region
!= NULL
|| rawOffset
!= NULL
) {
821 int32_t filteredMapSize
= DEFAULT_FILTERED_MAP_SIZE
;
822 filteredMap
= (int32_t *)uprv_malloc(filteredMapSize
* sizeof(int32_t));
823 if (filteredMap
== NULL
) {
824 ec
= U_MEMORY_ALLOCATION_ERROR
;
828 // Walk through the base map
829 UResourceBundle
*res
= ures_openDirect(0, kZONEINFO
, &ec
);
830 res
= ures_getByKey(res
, kNAMES
, res
, &ec
); // dereference Zones section
831 for (int32_t i
= 0; i
< baseLen
; i
++) {
832 int32_t zidx
= baseMap
[i
];
833 UnicodeString id
= ures_getUnicodeStringByIndex(res
, zidx
, &ec
);
837 if (region
!= NULL
) {
839 char tzregion
[4]; // max 3 letters + null term
840 TimeZone::getRegion(id
, tzregion
, sizeof(tzregion
), ec
);
844 if (uprv_stricmp(tzregion
, region
) != 0) {
845 // region does not match
849 if (rawOffset
!= NULL
) {
850 // Filter by raw offset
851 // Note: This is VERY inefficient
852 TimeZone
*z
= createSystemTimeZone(id
, ec
);
856 int32_t tzoffset
= z
->getRawOffset();
859 if (tzoffset
!= *rawOffset
) {
864 if (filteredMapSize
<= numEntries
) {
865 filteredMapSize
+= MAP_INCREMENT_SIZE
;
866 int32_t *tmp
= (int32_t *)uprv_realloc(filteredMap
, filteredMapSize
* sizeof(int32_t));
868 ec
= U_MEMORY_ALLOCATION_ERROR
;
875 filteredMap
[numEntries
++] = zidx
;
879 uprv_free(filteredMap
);
886 TZEnumeration
*result
= NULL
;
888 // Finally, create a new enumeration instance
889 if (filteredMap
== NULL
) {
890 result
= new TZEnumeration(baseMap
, baseLen
, FALSE
);
892 result
= new TZEnumeration(filteredMap
, numEntries
, TRUE
);
895 if (result
== NULL
) {
896 ec
= U_MEMORY_ALLOCATION_ERROR
;
900 if (filteredMap
!= NULL
) {
901 uprv_free(filteredMap
);
907 TZEnumeration(const TZEnumeration
&other
) : StringEnumeration(), map(NULL
), localMap(NULL
), len(0), pos(0) {
908 if (other
.localMap
!= NULL
) {
909 localMap
= (int32_t *)uprv_malloc(other
.len
* sizeof(int32_t));
910 if (localMap
!= NULL
) {
912 uprv_memcpy(localMap
, other
.localMap
, len
* sizeof(int32_t));
928 virtual ~TZEnumeration();
930 virtual StringEnumeration
*clone() const {
931 return new TZEnumeration(*this);
934 virtual int32_t count(UErrorCode
& status
) const {
935 return U_FAILURE(status
) ? 0 : len
;
938 virtual const UnicodeString
* snext(UErrorCode
& status
) {
939 if (U_SUCCESS(status
) && map
!= NULL
&& pos
< len
) {
940 getID(map
[pos
], status
);
947 virtual void reset(UErrorCode
& /*status*/) {
952 static UClassID U_EXPORT2
getStaticClassID(void);
953 virtual UClassID
getDynamicClassID(void) const;
956 TZEnumeration::~TZEnumeration() {
957 if (localMap
!= NULL
) {
962 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TZEnumeration
)
964 StringEnumeration
* U_EXPORT2
965 TimeZone::createTimeZoneIDEnumeration(
966 USystemTimeZoneType zoneType
,
968 const int32_t* rawOffset
,
970 return TZEnumeration::create(zoneType
, region
, rawOffset
, ec
);
973 StringEnumeration
* U_EXPORT2
974 TimeZone::createEnumeration() {
975 UErrorCode ec
= U_ZERO_ERROR
;
976 return TZEnumeration::create(UCAL_ZONE_TYPE_ANY
, NULL
, NULL
, ec
);
979 StringEnumeration
* U_EXPORT2
980 TimeZone::createEnumeration(int32_t rawOffset
) {
981 UErrorCode ec
= U_ZERO_ERROR
;
982 return TZEnumeration::create(UCAL_ZONE_TYPE_ANY
, NULL
, &rawOffset
, ec
);
985 StringEnumeration
* U_EXPORT2
986 TimeZone::createEnumeration(const char* country
) {
987 UErrorCode ec
= U_ZERO_ERROR
;
988 return TZEnumeration::create(UCAL_ZONE_TYPE_ANY
, country
, NULL
, ec
);
991 // ---------------------------------------
994 TimeZone::countEquivalentIDs(const UnicodeString
& id
) {
996 UErrorCode ec
= U_ZERO_ERROR
;
997 StackUResourceBundle res
;
998 U_DEBUG_TZ_MSG(("countEquivalentIDs..\n"));
999 UResourceBundle
*top
= openOlsonResource(id
, res
.ref(), ec
);
1000 if (U_SUCCESS(ec
)) {
1001 StackUResourceBundle r
;
1002 ures_getByKey(res
.getAlias(), kLINKS
, r
.getAlias(), &ec
);
1003 ures_getIntVector(r
.getAlias(), &result
, &ec
);
1009 // ---------------------------------------
1011 const UnicodeString U_EXPORT2
1012 TimeZone::getEquivalentID(const UnicodeString
& id
, int32_t index
) {
1013 U_DEBUG_TZ_MSG(("gEI(%d)\n", index
));
1014 UnicodeString result
;
1015 UErrorCode ec
= U_ZERO_ERROR
;
1016 StackUResourceBundle res
;
1017 UResourceBundle
*top
= openOlsonResource(id
, res
.ref(), ec
);
1019 if (U_SUCCESS(ec
)) {
1020 StackUResourceBundle r
;
1022 ures_getByKey(res
.getAlias(), kLINKS
, r
.getAlias(), &ec
);
1023 const int32_t *v
= ures_getIntVector(r
.getAlias(), &size
, &ec
);
1024 if (U_SUCCESS(ec
)) {
1025 if (index
>= 0 && index
< size
) {
1031 UResourceBundle
*ares
= ures_getByKey(top
, kNAMES
, NULL
, &ec
); // dereference Zones section
1032 if (U_SUCCESS(ec
)) {
1034 const UChar
* id2
= ures_getStringByIndex(ares
, zone
, &idLen
, &ec
);
1035 result
.fastCopyFrom(UnicodeString(TRUE
, id2
, idLen
));
1036 U_DEBUG_TZ_MSG(("gei(%d) -> %d, len%d, %s\n", index
, zone
, result
.length(), u_errorName(ec
)));
1041 #if defined(U_DEBUG_TZ)
1042 if(result
.length() ==0) {
1043 U_DEBUG_TZ_MSG(("equiv [__, #%d] -> 0 (%s)\n", index
, u_errorName(ec
)));
1049 // ---------------------------------------
1051 // These methods are used by ZoneMeta class only.
1054 TimeZone::findID(const UnicodeString
& id
) {
1055 const UChar
*result
= NULL
;
1056 UErrorCode ec
= U_ZERO_ERROR
;
1057 UResourceBundle
*rb
= ures_openDirect(NULL
, kZONEINFO
, &ec
);
1059 // resolve zone index by name
1060 UResourceBundle
*names
= ures_getByKey(rb
, kNAMES
, NULL
, &ec
);
1061 int32_t idx
= findInStringArray(names
, id
, ec
);
1062 result
= ures_getStringByIndex(names
, idx
, NULL
, &ec
);
1063 if (U_FAILURE(ec
)) {
1073 TimeZone::dereferOlsonLink(const UnicodeString
& id
) {
1074 const UChar
*result
= NULL
;
1075 UErrorCode ec
= U_ZERO_ERROR
;
1076 UResourceBundle
*rb
= ures_openDirect(NULL
, kZONEINFO
, &ec
);
1078 // resolve zone index by name
1079 UResourceBundle
*names
= ures_getByKey(rb
, kNAMES
, NULL
, &ec
);
1080 int32_t idx
= findInStringArray(names
, id
, ec
);
1081 result
= ures_getStringByIndex(names
, idx
, NULL
, &ec
);
1083 // open the zone bundle by index
1084 ures_getByKey(rb
, kZONES
, rb
, &ec
);
1085 ures_getByIndex(rb
, idx
, rb
, &ec
);
1087 if (U_SUCCESS(ec
)) {
1088 if (ures_getType(rb
) == URES_INT
) {
1089 // this is a link - dereference the link
1090 int32_t deref
= ures_getInt(rb
, &ec
);
1091 const UChar
* tmp
= ures_getStringByIndex(names
, deref
, NULL
, &ec
);
1092 if (U_SUCCESS(ec
)) {
1105 TimeZone::getRegion(const UnicodeString
& id
) {
1106 UErrorCode status
= U_ZERO_ERROR
;
1107 return getRegion(id
, status
);
1111 TimeZone::getRegion(const UnicodeString
& id
, UErrorCode
& status
) {
1112 if (U_FAILURE(status
)) {
1115 const UChar
*result
= NULL
;
1116 UResourceBundle
*rb
= ures_openDirect(NULL
, kZONEINFO
, &status
);
1118 // resolve zone index by name
1119 UResourceBundle
*res
= ures_getByKey(rb
, kNAMES
, NULL
, &status
);
1120 int32_t idx
= findInStringArray(res
, id
, status
);
1122 // get region mapping
1123 ures_getByKey(rb
, kREGIONS
, res
, &status
);
1124 const UChar
*tmp
= ures_getStringByIndex(res
, idx
, NULL
, &status
);
1125 if (U_SUCCESS(status
)) {
1136 // ---------------------------------------
1138 TimeZone::getRegion(const UnicodeString
& id
, char *region
, int32_t capacity
, UErrorCode
& status
)
1140 int32_t resultLen
= 0;
1142 if (U_FAILURE(status
)) {
1146 const UChar
*uregion
= NULL
;
1147 // "Etc/Unknown" is not a system zone ID,
1148 // but in the zone data
1149 if (id
.compare(UNKNOWN_ZONE_ID
, UNKNOWN_ZONE_ID_LENGTH
) != 0) {
1150 uregion
= getRegion(id
);
1152 if (uregion
== NULL
) {
1153 status
= U_ILLEGAL_ARGUMENT_ERROR
;
1156 resultLen
= u_strlen(uregion
);
1157 // A region code is represented by invariant characters
1158 u_UCharsToChars(uregion
, region
, uprv_min(resultLen
, capacity
));
1160 if (capacity
< resultLen
) {
1161 status
= U_BUFFER_OVERFLOW_ERROR
;
1165 return u_terminateChars(region
, capacity
, resultLen
, &status
);
1168 // ---------------------------------------
1172 TimeZone::getDisplayName(UnicodeString
& result
) const
1174 return getDisplayName(FALSE
,LONG
,Locale::getDefault(), result
);
1178 TimeZone::getDisplayName(const Locale
& locale
, UnicodeString
& result
) const
1180 return getDisplayName(FALSE
, LONG
, locale
, result
);
1184 TimeZone::getDisplayName(UBool inDaylight
, EDisplayType style
, UnicodeString
& result
) const
1186 return getDisplayName(inDaylight
,style
, Locale::getDefault(), result
);
1188 //--------------------------------------
1190 TimeZone::getDSTSavings()const {
1191 if (useDaylightTime()) {
1196 //---------------------------------------
1198 TimeZone::getDisplayName(UBool inDaylight
, EDisplayType style
, const Locale
& locale
, UnicodeString
& result
) const
1200 UErrorCode status
= U_ZERO_ERROR
;
1201 UDate date
= Calendar::getNow();
1202 UTimeZoneFormatTimeType timeType
= UTZFMT_TIME_TYPE_UNKNOWN
;
1205 if (style
== GENERIC_LOCATION
|| style
== LONG_GENERIC
|| style
== SHORT_GENERIC
) {
1206 LocalPointer
<TimeZoneFormat
> tzfmt(TimeZoneFormat::createInstance(locale
, status
));
1207 if (U_FAILURE(status
)) {
1213 case GENERIC_LOCATION
:
1214 tzfmt
->format(UTZFMT_STYLE_GENERIC_LOCATION
, *this, date
, result
, &timeType
);
1217 tzfmt
->format(UTZFMT_STYLE_GENERIC_LONG
, *this, date
, result
, &timeType
);
1220 tzfmt
->format(UTZFMT_STYLE_GENERIC_SHORT
, *this, date
, result
, &timeType
);
1225 // Generic format many use Localized GMT as the final fallback.
1226 // When Localized GMT format is used, the result might not be
1227 // appropriate for the requested daylight value.
1228 if ((inDaylight
&& timeType
== UTZFMT_TIME_TYPE_STANDARD
) || (!inDaylight
&& timeType
== UTZFMT_TIME_TYPE_DAYLIGHT
)) {
1229 offset
= inDaylight
? getRawOffset() + getDSTSavings() : getRawOffset();
1230 if (style
== SHORT_GENERIC
) {
1231 tzfmt
->formatOffsetShortLocalizedGMT(offset
, result
, status
);
1233 tzfmt
->formatOffsetLocalizedGMT(offset
, result
, status
);
1236 } else if (style
== LONG_GMT
|| style
== SHORT_GMT
) {
1237 LocalPointer
<TimeZoneFormat
> tzfmt(TimeZoneFormat::createInstance(locale
, status
));
1238 if (U_FAILURE(status
)) {
1242 offset
= inDaylight
&& useDaylightTime() ? getRawOffset() + getDSTSavings() : getRawOffset();
1245 tzfmt
->formatOffsetLocalizedGMT(offset
, result
, status
);
1248 tzfmt
->formatOffsetISO8601Basic(offset
, FALSE
, FALSE
, FALSE
, result
, status
);
1255 U_ASSERT(style
== LONG
|| style
== SHORT
|| style
== SHORT_COMMONLY_USED
);
1256 UTimeZoneNameType nameType
= UTZNM_UNKNOWN
;
1259 nameType
= inDaylight
? UTZNM_LONG_DAYLIGHT
: UTZNM_LONG_STANDARD
;
1262 case SHORT_COMMONLY_USED
:
1263 nameType
= inDaylight
? UTZNM_SHORT_DAYLIGHT
: UTZNM_SHORT_STANDARD
;
1268 LocalPointer
<TimeZoneNames
> tznames(TimeZoneNames::createInstance(locale
, status
));
1269 if (U_FAILURE(status
)) {
1273 UnicodeString
canonicalID(ZoneMeta::getCanonicalCLDRID(*this));
1274 tznames
->getDisplayName(canonicalID
, nameType
, date
, result
);
1275 if (result
.isEmpty()) {
1276 // Fallback to localized GMT
1277 LocalPointer
<TimeZoneFormat
> tzfmt(TimeZoneFormat::createInstance(locale
, status
));
1278 offset
= inDaylight
&& useDaylightTime() ? getRawOffset() + getDSTSavings() : getRawOffset();
1279 if (style
== LONG
) {
1280 tzfmt
->formatOffsetLocalizedGMT(offset
, result
, status
);
1282 tzfmt
->formatOffsetShortLocalizedGMT(offset
, result
, status
);
1286 if (U_FAILURE(status
)) {
1293 * Parse a custom time zone identifier and return a corresponding zone.
1294 * @param id a string of the form GMT[+-]hh:mm, GMT[+-]hhmm, or
1296 * @return a newly created SimpleTimeZone with the given offset and
1297 * no Daylight Savings Time, or null if the id cannot be parsed.
1300 TimeZone::createCustomTimeZone(const UnicodeString
& id
)
1302 int32_t sign
, hour
, min
, sec
;
1303 if (parseCustomID(id
, sign
, hour
, min
, sec
)) {
1304 UnicodeString customID
;
1305 formatCustomID(hour
, min
, sec
, (sign
< 0), customID
);
1306 int32_t offset
= sign
* ((hour
* 60 + min
) * 60 + sec
) * 1000;
1307 return new SimpleTimeZone(offset
, customID
);
1313 TimeZone::getCustomID(const UnicodeString
& id
, UnicodeString
& normalized
, UErrorCode
& status
) {
1314 normalized
.remove();
1315 if (U_FAILURE(status
)) {
1318 int32_t sign
, hour
, min
, sec
;
1319 if (parseCustomID(id
, sign
, hour
, min
, sec
)) {
1320 formatCustomID(hour
, min
, sec
, (sign
< 0), normalized
);
1322 status
= U_ILLEGAL_ARGUMENT_ERROR
;
1328 TimeZone::parseCustomID(const UnicodeString
& id
, int32_t& sign
,
1329 int32_t& hour
, int32_t& min
, int32_t& sec
) {
1330 static const int32_t kParseFailed
= -99999;
1332 NumberFormat
* numberFormat
= 0;
1333 UnicodeString idUppercase
= id
;
1334 idUppercase
.toUpper("");
1336 if (id
.length() > GMT_ID_LENGTH
&&
1337 idUppercase
.startsWith(GMT_ID
, GMT_ID_LENGTH
))
1339 ParsePosition
pos(GMT_ID_LENGTH
);
1345 if (id
[pos
.getIndex()] == MINUS
/*'-'*/) {
1347 } else if (id
[pos
.getIndex()] != PLUS
/*'+'*/) {
1350 pos
.setIndex(pos
.getIndex() + 1);
1352 UErrorCode success
= U_ZERO_ERROR
;
1353 numberFormat
= NumberFormat::createInstance(success
);
1354 if(U_FAILURE(success
)){
1357 numberFormat
->setParseIntegerOnly(TRUE
);
1358 //numberFormat->setLenient(TRUE); // TODO: May need to set this, depends on latest timezone parsing
1360 // Look for either hh:mm, hhmm, or hh
1361 int32_t start
= pos
.getIndex();
1362 Formattable
n(kParseFailed
);
1363 numberFormat
->parse(id
, n
, pos
);
1364 if (pos
.getIndex() == start
) {
1365 delete numberFormat
;
1370 if (pos
.getIndex() < id
.length()) {
1371 if (pos
.getIndex() - start
> 2
1372 || id
[pos
.getIndex()] != COLON
) {
1373 delete numberFormat
;
1377 pos
.setIndex(pos
.getIndex() + 1);
1378 int32_t oldPos
= pos
.getIndex();
1379 n
.setLong(kParseFailed
);
1380 numberFormat
->parse(id
, n
, pos
);
1381 if ((pos
.getIndex() - oldPos
) != 2) {
1383 delete numberFormat
;
1387 if (pos
.getIndex() < id
.length()) {
1388 if (id
[pos
.getIndex()] != COLON
) {
1389 delete numberFormat
;
1393 pos
.setIndex(pos
.getIndex() + 1);
1394 oldPos
= pos
.getIndex();
1395 n
.setLong(kParseFailed
);
1396 numberFormat
->parse(id
, n
, pos
);
1397 if (pos
.getIndex() != id
.length()
1398 || (pos
.getIndex() - oldPos
) != 2) {
1399 delete numberFormat
;
1405 // Supported formats are below -
1414 int32_t length
= pos
.getIndex() - start
;
1415 if (length
<= 0 || 6 < length
) {
1417 delete numberFormat
;
1423 // already set to hour
1433 min
= (hour
/100) % 100;
1439 delete numberFormat
;
1441 if (hour
> kMAX_CUSTOM_HOUR
|| min
> kMAX_CUSTOM_MIN
|| sec
> kMAX_CUSTOM_SEC
) {
1450 TimeZone::formatCustomID(int32_t hour
, int32_t min
, int32_t sec
,
1451 UBool negative
, UnicodeString
& id
) {
1452 // Create time zone ID - GMT[+|-]hhmm[ss]
1453 id
.setTo(GMT_ID
, GMT_ID_LENGTH
);
1454 if (hour
| min
| sec
) {
1462 id
+= (UChar
)ZERO_DIGIT
;
1464 id
+= (UChar
)(ZERO_DIGIT
+ hour
/10);
1466 id
+= (UChar
)(ZERO_DIGIT
+ hour%10
);
1469 id
+= (UChar
)ZERO_DIGIT
;
1471 id
+= (UChar
)(ZERO_DIGIT
+ min
/10);
1473 id
+= (UChar
)(ZERO_DIGIT
+ min%10
);
1478 id
+= (UChar
)ZERO_DIGIT
;
1480 id
+= (UChar
)(ZERO_DIGIT
+ sec
/10);
1482 id
+= (UChar
)(ZERO_DIGIT
+ sec%10
);
1490 TimeZone::hasSameRules(const TimeZone
& other
) const
1492 return (getRawOffset() == other
.getRawOffset() &&
1493 useDaylightTime() == other
.useDaylightTime());
1496 static void U_CALLCONV
initTZDataVersion(UErrorCode
&status
) {
1497 ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE
, timeZone_cleanup
);
1499 StackUResourceBundle bundle
;
1500 ures_openDirectFillIn(bundle
.getAlias(), NULL
, kZONEINFO
, &status
);
1501 const UChar
*tzver
= ures_getStringByKey(bundle
.getAlias(), kTZVERSION
, &len
, &status
);
1503 if (U_SUCCESS(status
)) {
1504 if (len
>= (int32_t)sizeof(TZDATA_VERSION
)) {
1505 // Ensure that there is always space for a trailing nul in TZDATA_VERSION
1506 len
= sizeof(TZDATA_VERSION
) - 1;
1508 u_UCharsToChars(tzver
, TZDATA_VERSION
, len
);
1513 TimeZone::getTZDataVersion(UErrorCode
& status
)
1515 umtx_initOnce(gTZDataVersionInitOnce
, &initTZDataVersion
, status
);
1516 return (const char*)TZDATA_VERSION
;
1520 TimeZone::getCanonicalID(const UnicodeString
& id
, UnicodeString
& canonicalID
, UErrorCode
& status
)
1522 UBool isSystemID
= FALSE
;
1523 return getCanonicalID(id
, canonicalID
, isSystemID
, status
);
1527 TimeZone::getCanonicalID(const UnicodeString
& id
, UnicodeString
& canonicalID
, UBool
& isSystemID
,
1530 canonicalID
.remove();
1532 if (U_FAILURE(status
)) {
1535 if (id
.compare(UNKNOWN_ZONE_ID
, UNKNOWN_ZONE_ID_LENGTH
) == 0) {
1536 // special case - Etc/Unknown is a canonical ID, but not system ID
1537 canonicalID
.fastCopyFrom(id
);
1540 ZoneMeta::getCanonicalCLDRID(id
, canonicalID
, status
);
1541 if (U_SUCCESS(status
)) {
1545 status
= U_ZERO_ERROR
;
1546 getCustomID(id
, canonicalID
, status
);
1553 TimeZone::getWindowsID(const UnicodeString
& id
, UnicodeString
& winid
, UErrorCode
& status
) {
1555 if (U_FAILURE(status
)) {
1559 // canonicalize the input ID
1560 UnicodeString canonicalID
;
1561 UBool isSystemID
= FALSE
;
1563 getCanonicalID(id
, canonicalID
, isSystemID
, status
);
1564 if (U_FAILURE(status
) || !isSystemID
) {
1565 // mapping data is only applicable to tz database IDs
1566 if (status
== U_ILLEGAL_ARGUMENT_ERROR
) {
1567 // getWindowsID() sets an empty string where
1568 // getCanonicalID() sets a U_ILLEGAL_ARGUMENT_ERROR.
1569 status
= U_ZERO_ERROR
;
1574 UResourceBundle
*mapTimezones
= ures_openDirect(NULL
, "windowsZones", &status
);
1575 ures_getByKey(mapTimezones
, "mapTimezones", mapTimezones
, &status
);
1577 if (U_FAILURE(status
)) {
1581 UResourceBundle
*winzone
= NULL
;
1582 UBool found
= FALSE
;
1583 while (ures_hasNext(mapTimezones
) && !found
) {
1584 winzone
= ures_getNextResource(mapTimezones
, winzone
, &status
);
1585 if (U_FAILURE(status
)) {
1588 if (ures_getType(winzone
) != URES_TABLE
) {
1591 UResourceBundle
*regionalData
= NULL
;
1592 while (ures_hasNext(winzone
) && !found
) {
1593 regionalData
= ures_getNextResource(winzone
, regionalData
, &status
);
1594 if (U_FAILURE(status
)) {
1597 if (ures_getType(regionalData
) != URES_STRING
) {
1601 const UChar
*tzids
= ures_getString(regionalData
, &len
, &status
);
1602 if (U_FAILURE(status
)) {
1606 const UChar
*start
= tzids
;
1607 UBool hasNext
= TRUE
;
1609 const UChar
*end
= u_strchr(start
, (UChar
)0x20);
1614 if (canonicalID
.compare(start
, static_cast<int32_t>(end
- start
)) == 0) {
1615 winid
= UnicodeString(ures_getKey(winzone
), -1 , US_INV
);
1622 ures_close(regionalData
);
1624 ures_close(winzone
);
1625 ures_close(mapTimezones
);
1630 #define MAX_WINDOWS_ID_SIZE 128
1633 TimeZone::getIDForWindowsID(const UnicodeString
& winid
, const char* region
, UnicodeString
& id
, UErrorCode
& status
) {
1635 if (U_FAILURE(status
)) {
1639 UResourceBundle
*zones
= ures_openDirect(NULL
, "windowsZones", &status
);
1640 ures_getByKey(zones
, "mapTimezones", zones
, &status
);
1641 if (U_FAILURE(status
)) {
1646 UErrorCode tmperr
= U_ZERO_ERROR
;
1647 char winidKey
[MAX_WINDOWS_ID_SIZE
];
1648 int32_t winKeyLen
= winid
.extract(0, winid
.length(), winidKey
, sizeof(winidKey
) - 1, US_INV
);
1650 if (winKeyLen
== 0 || winKeyLen
>= (int32_t)sizeof(winidKey
)) {
1654 winidKey
[winKeyLen
] = 0;
1656 ures_getByKey(zones
, winidKey
, zones
, &tmperr
); // use tmperr, because windows mapping might not
1657 // be avaiable by design
1658 if (U_FAILURE(tmperr
)) {
1663 const UChar
*tzid
= NULL
;
1665 UBool gotID
= FALSE
;
1667 const UChar
*tzids
= ures_getStringByKey(zones
, region
, &len
, &tmperr
); // use tmperr, because
1668 // regional mapping is optional
1669 if (U_SUCCESS(tmperr
)) {
1670 // first ID delimited by space is the defasult one
1671 const UChar
*end
= u_strchr(tzids
, (UChar
)0x20);
1673 id
.setTo(tzids
, -1);
1675 id
.setTo(tzids
, static_cast<int32_t>(end
- tzids
));
1682 tzid
= ures_getStringByKey(zones
, "001", &len
, &status
); // using status, because "001" must be
1683 // available at this point
1684 if (U_SUCCESS(status
)) {
1685 id
.setTo(tzid
, len
);
1696 #endif /* #if !UCONFIG_NO_FORMATTING */