1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (c) 2003-2013, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
9 * Created: July 21 2003
11 **********************************************************************
14 #include "utypeinfo.h" // for 'typeid' to work
18 #if !UCONFIG_NO_FORMATTING
20 #include "unicode/ures.h"
21 #include "unicode/simpletz.h"
22 #include "unicode/gregocal.h"
27 #include <float.h> // DBL_MAX
34 # include "uresimp.h" // for debugging
36 static void debug_tz_loc(const char *f
, int32_t l
)
38 fprintf(stderr
, "%s:%d: ", f
, l
);
41 static void debug_tz_msg(const char *pat
, ...)
45 vfprintf(stderr
, pat
, ap
);
48 // must use double parens, i.e.: U_DEBUG_TZ_MSG(("four is: %d",4));
49 #define U_DEBUG_TZ_MSG(x) {debug_tz_loc(__FILE__,__LINE__);debug_tz_msg x;}
51 #define U_DEBUG_TZ_MSG(x)
54 static UBool
arrayEqual(const void *a1
, const void *a2
, int32_t size
) {
55 if (a1
== NULL
&& a2
== NULL
) {
58 if ((a1
!= NULL
&& a2
== NULL
) || (a1
== NULL
&& a2
!= NULL
)) {
65 return (uprv_memcmp(a1
, a2
, size
) == 0);
70 #define kTRANS "trans"
71 #define kTRANSPRE32 "transPre32"
72 #define kTRANSPOST32 "transPost32"
73 #define kTYPEOFFSETS "typeOffsets"
74 #define kTYPEMAP "typeMap"
75 #define kLINKS "links"
76 #define kFINALRULE "finalRule"
77 #define kFINALRAW "finalRaw"
78 #define kFINALYEAR "finalYear"
80 #define SECONDS_PER_DAY (24*60*60)
82 static const int32_t ZEROS
[] = {0,0};
84 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(OlsonTimeZone
)
87 * Default constructor. Creates a time zone with an empty ID and
88 * a fixed GMT offset of zero.
90 /*OlsonTimeZone::OlsonTimeZone() : finalYear(INT32_MAX), finalMillis(DBL_MAX), finalZone(0), transitionRulesInitialized(FALSE) {
91 clearTransitionRules();
96 * Construct a GMT+0 zone with no transitions. This is done when a
97 * constructor fails so the resultant object is well-behaved.
99 void OlsonTimeZone::constructEmpty() {
102 transitionCountPre32
= transitionCount32
= transitionCountPost32
= 0;
103 transitionTimesPre32
= transitionTimes32
= transitionTimesPost32
= NULL
;
114 * Construct from a resource bundle
115 * @param top the top-level zoneinfo resource bundle. This is used
116 * to lookup the rule that `res' may refer to, if there is one.
117 * @param res the resource bundle of the zone to be constructed
118 * @param ec input-output error code
120 OlsonTimeZone::OlsonTimeZone(const UResourceBundle
* top
,
121 const UResourceBundle
* res
,
122 const UnicodeString
& tzid
,
124 BasicTimeZone(tzid
), finalZone(NULL
)
126 clearTransitionRules();
127 U_DEBUG_TZ_MSG(("OlsonTimeZone(%s)\n", ures_getKey((UResourceBundle
*)res
)));
128 if ((top
== NULL
|| res
== NULL
) && U_SUCCESS(ec
)) {
129 ec
= U_ILLEGAL_ARGUMENT_ERROR
;
132 // TODO -- clean up -- Doesn't work if res points to an alias
133 // // TODO remove nonconst casts below when ures_* API is fixed
134 // setID(ures_getKey((UResourceBundle*) res)); // cast away const
137 StackUResourceBundle r
;
139 // Pre-32bit second transitions
140 ures_getByKey(res
, kTRANSPRE32
, r
.getAlias(), &ec
);
141 transitionTimesPre32
= ures_getIntVector(r
.getAlias(), &len
, &ec
);
142 transitionCountPre32
= static_cast<int16_t>(len
>> 1);
143 if (ec
== U_MISSING_RESOURCE_ERROR
) {
144 // No pre-32bit transitions
145 transitionTimesPre32
= NULL
;
146 transitionCountPre32
= 0;
148 } else if (U_SUCCESS(ec
) && (len
< 0 || len
> 0x7FFF || (len
& 1) != 0) /* len must be even */) {
149 ec
= U_INVALID_FORMAT_ERROR
;
152 // 32bit second transitions
153 ures_getByKey(res
, kTRANS
, r
.getAlias(), &ec
);
154 transitionTimes32
= ures_getIntVector(r
.getAlias(), &len
, &ec
);
155 transitionCount32
= static_cast<int16_t>(len
);
156 if (ec
== U_MISSING_RESOURCE_ERROR
) {
157 // No 32bit transitions
158 transitionTimes32
= NULL
;
159 transitionCount32
= 0;
161 } else if (U_SUCCESS(ec
) && (len
< 0 || len
> 0x7FFF)) {
162 ec
= U_INVALID_FORMAT_ERROR
;
165 // Post-32bit second transitions
166 ures_getByKey(res
, kTRANSPOST32
, r
.getAlias(), &ec
);
167 transitionTimesPost32
= ures_getIntVector(r
.getAlias(), &len
, &ec
);
168 transitionCountPost32
= static_cast<int16_t>(len
>> 1);
169 if (ec
== U_MISSING_RESOURCE_ERROR
) {
170 // No pre-32bit transitions
171 transitionTimesPost32
= NULL
;
172 transitionCountPost32
= 0;
174 } else if (U_SUCCESS(ec
) && (len
< 0 || len
> 0x7FFF || (len
& 1) != 0) /* len must be even */) {
175 ec
= U_INVALID_FORMAT_ERROR
;
178 // Type offsets list must be of even size, with size >= 2
179 ures_getByKey(res
, kTYPEOFFSETS
, r
.getAlias(), &ec
);
180 typeOffsets
= ures_getIntVector(r
.getAlias(), &len
, &ec
);
181 if (U_SUCCESS(ec
) && (len
< 2 || len
> 0x7FFE || (len
& 1) != 0)) {
182 ec
= U_INVALID_FORMAT_ERROR
;
184 typeCount
= (int16_t) len
>> 1;
186 // Type map data must be of the same size as the transition count
188 if (transitionCount() > 0) {
189 ures_getByKey(res
, kTYPEMAP
, r
.getAlias(), &ec
);
190 typeMapData
= ures_getBinary(r
.getAlias(), &len
, &ec
);
191 if (ec
== U_MISSING_RESOURCE_ERROR
) {
192 // no type mapping data
193 ec
= U_INVALID_FORMAT_ERROR
;
194 } else if (U_SUCCESS(ec
) && len
!= transitionCount()) {
195 ec
= U_INVALID_FORMAT_ERROR
;
199 // Process final rule and data, if any
201 const UChar
*ruleIdUStr
= ures_getStringByKey(res
, kFINALRULE
, &len
, &ec
);
202 ures_getByKey(res
, kFINALRAW
, r
.getAlias(), &ec
);
203 int32_t ruleRaw
= ures_getInt(r
.getAlias(), &ec
);
204 ures_getByKey(res
, kFINALYEAR
, r
.getAlias(), &ec
);
205 int32_t ruleYear
= ures_getInt(r
.getAlias(), &ec
);
207 UnicodeString
ruleID(TRUE
, ruleIdUStr
, len
);
208 UResourceBundle
*rule
= TimeZone::loadRule(top
, ruleID
, NULL
, ec
);
209 const int32_t *ruleData
= ures_getIntVector(rule
, &len
, &ec
);
210 if (U_SUCCESS(ec
) && len
== 11) {
211 UnicodeString emptyStr
;
212 finalZone
= new SimpleTimeZone(
213 ruleRaw
* U_MILLIS_PER_SECOND
,
215 (int8_t)ruleData
[0], (int8_t)ruleData
[1], (int8_t)ruleData
[2],
216 ruleData
[3] * U_MILLIS_PER_SECOND
,
217 (SimpleTimeZone::TimeMode
) ruleData
[4],
218 (int8_t)ruleData
[5], (int8_t)ruleData
[6], (int8_t)ruleData
[7],
219 ruleData
[8] * U_MILLIS_PER_SECOND
,
220 (SimpleTimeZone::TimeMode
) ruleData
[9],
221 ruleData
[10] * U_MILLIS_PER_SECOND
, ec
);
222 if (finalZone
== NULL
) {
223 ec
= U_MEMORY_ALLOCATION_ERROR
;
225 finalStartYear
= ruleYear
;
227 // Note: Setting finalStartYear to the finalZone is problematic. When a date is around
228 // year boundary, SimpleTimeZone may return false result when DST is observed at the
229 // beginning of year. We could apply safe margin (day or two), but when one of recurrent
230 // rules falls around year boundary, it could return false result. Without setting the
231 // start year, finalZone works fine around the year boundary of the start year.
233 // finalZone->setStartYear(finalStartYear);
236 // Compute the millis for Jan 1, 0:00 GMT of the finalYear
238 // Note: finalStartMillis is used for detecting either if
239 // historic transition data or finalZone to be used. In an
240 // extreme edge case - for example, two transitions fall into
241 // small windows of time around the year boundary, this may
242 // result incorrect offset computation. But I think it will
243 // never happen practically. Yoshito - Feb 20, 2010
244 finalStartMillis
= Grego::fieldsToDay(finalStartYear
, 0, 1) * U_MILLIS_PER_DAY
;
247 ec
= U_INVALID_FORMAT_ERROR
;
250 } else if (ec
== U_MISSING_RESOURCE_ERROR
) {
256 // initialize canonical ID
257 canonicalID
= ZoneMeta::getCanonicalCLDRID(tzid
, ec
);
268 OlsonTimeZone::OlsonTimeZone(const OlsonTimeZone
& other
) :
269 BasicTimeZone(other
), finalZone(0) {
274 * Assignment operator
276 OlsonTimeZone
& OlsonTimeZone::operator=(const OlsonTimeZone
& other
) {
277 canonicalID
= other
.canonicalID
;
279 transitionTimesPre32
= other
.transitionTimesPre32
;
280 transitionTimes32
= other
.transitionTimes32
;
281 transitionTimesPost32
= other
.transitionTimesPost32
;
283 transitionCountPre32
= other
.transitionCountPre32
;
284 transitionCount32
= other
.transitionCount32
;
285 transitionCountPost32
= other
.transitionCountPost32
;
287 typeCount
= other
.typeCount
;
288 typeOffsets
= other
.typeOffsets
;
289 typeMapData
= other
.typeMapData
;
292 finalZone
= (other
.finalZone
!= 0) ? other
.finalZone
->clone() : 0;
294 finalStartYear
= other
.finalStartYear
;
295 finalStartMillis
= other
.finalStartMillis
;
297 clearTransitionRules();
305 OlsonTimeZone::~OlsonTimeZone() {
306 deleteTransitionRules();
311 * Returns true if the two TimeZone objects are equal.
313 UBool
OlsonTimeZone::operator==(const TimeZone
& other
) const {
314 return ((this == &other
) ||
315 (typeid(*this) == typeid(other
) &&
316 TimeZone::operator==(other
) &&
317 hasSameRules(other
)));
323 OlsonTimeZone
* OlsonTimeZone::clone() const {
324 return new OlsonTimeZone(*this);
330 int32_t OlsonTimeZone::getOffset(uint8_t era
, int32_t year
, int32_t month
,
331 int32_t dom
, uint8_t dow
,
332 int32_t millis
, UErrorCode
& ec
) const {
333 if (month
< UCAL_JANUARY
|| month
> UCAL_DECEMBER
) {
335 ec
= U_ILLEGAL_ARGUMENT_ERROR
;
339 return getOffset(era
, year
, month
, dom
, dow
, millis
,
340 Grego::monthLength(year
, month
),
348 int32_t OlsonTimeZone::getOffset(uint8_t era
, int32_t year
, int32_t month
,
349 int32_t dom
, uint8_t dow
,
350 int32_t millis
, int32_t monthLength
,
351 UErrorCode
& ec
) const {
356 if ((era
!= GregorianCalendar::AD
&& era
!= GregorianCalendar::BC
)
357 || month
< UCAL_JANUARY
358 || month
> UCAL_DECEMBER
362 || dow
> UCAL_SATURDAY
364 || millis
>= U_MILLIS_PER_DAY
366 || monthLength
> 31) {
367 ec
= U_ILLEGAL_ARGUMENT_ERROR
;
371 if (era
== GregorianCalendar::BC
) {
375 if (finalZone
!= NULL
&& year
>= finalStartYear
) {
376 return finalZone
->getOffset(era
, year
, month
, dom
, dow
,
377 millis
, monthLength
, ec
);
380 // Compute local epoch millis from input fields
381 UDate date
= (UDate
)(Grego::fieldsToDay(year
, month
, dom
) * U_MILLIS_PER_DAY
+ millis
);
382 int32_t rawoff
, dstoff
;
383 getHistoricalOffset(date
, TRUE
, kDaylight
, kStandard
, rawoff
, dstoff
);
384 return rawoff
+ dstoff
;
390 void OlsonTimeZone::getOffset(UDate date
, UBool local
, int32_t& rawoff
,
391 int32_t& dstoff
, UErrorCode
& ec
) const {
395 if (finalZone
!= NULL
&& date
>= finalStartMillis
) {
396 finalZone
->getOffset(date
, local
, rawoff
, dstoff
, ec
);
398 getHistoricalOffset(date
, local
, kFormer
, kLatter
, rawoff
, dstoff
);
403 OlsonTimeZone::getOffsetFromLocal(UDate date
, int32_t nonExistingTimeOpt
, int32_t duplicatedTimeOpt
,
404 int32_t& rawoff
, int32_t& dstoff
, UErrorCode
& ec
) const {
408 if (finalZone
!= NULL
&& date
>= finalStartMillis
) {
409 finalZone
->getOffsetFromLocal(date
, nonExistingTimeOpt
, duplicatedTimeOpt
, rawoff
, dstoff
, ec
);
411 getHistoricalOffset(date
, TRUE
, nonExistingTimeOpt
, duplicatedTimeOpt
, rawoff
, dstoff
);
419 void OlsonTimeZone::setRawOffset(int32_t /*offsetMillis*/) {
420 // We don't support this operation, since OlsonTimeZones are
421 // immutable (except for the ID, which is in the base class).
429 int32_t OlsonTimeZone::getRawOffset() const {
430 UErrorCode ec
= U_ZERO_ERROR
;
432 getOffset((double) uprv_getUTCtime() * U_MILLIS_PER_SECOND
,
433 FALSE
, raw
, dst
, ec
);
437 #if defined U_DEBUG_TZ
438 void printTime(double ms
) {
439 int32_t year
, month
, dom
, dow
;
441 double days
= ClockMath::floorDivide(((double)ms
), (double)U_MILLIS_PER_DAY
, millis
);
443 Grego::dayToFields(days
, year
, month
, dom
, dow
);
444 U_DEBUG_TZ_MSG((" getHistoricalOffset: time %.1f (%04d.%02d.%02d+%.1fh)\n", ms
,
445 year
, month
+1, dom
, (millis
/kOneHour
)));
450 OlsonTimeZone::transitionTimeInSeconds(int16_t transIdx
) const {
451 U_ASSERT(transIdx
>= 0 && transIdx
< transitionCount());
453 if (transIdx
< transitionCountPre32
) {
454 return (((int64_t)((uint32_t)transitionTimesPre32
[transIdx
<< 1])) << 32)
455 | ((int64_t)((uint32_t)transitionTimesPre32
[(transIdx
<< 1) + 1]));
458 transIdx
-= transitionCountPre32
;
459 if (transIdx
< transitionCount32
) {
460 return (int64_t)transitionTimes32
[transIdx
];
463 transIdx
-= transitionCount32
;
464 return (((int64_t)((uint32_t)transitionTimesPost32
[transIdx
<< 1])) << 32)
465 | ((int64_t)((uint32_t)transitionTimesPost32
[(transIdx
<< 1) + 1]));
468 // Maximum absolute offset in seconds (86400 seconds = 1 day)
469 // getHistoricalOffset uses this constant as safety margin of
470 // quick zone transition checking.
471 #define MAX_OFFSET_SECONDS 86400
474 OlsonTimeZone::getHistoricalOffset(UDate date
, UBool local
,
475 int32_t NonExistingTimeOpt
, int32_t DuplicatedTimeOpt
,
476 int32_t& rawoff
, int32_t& dstoff
) const {
477 U_DEBUG_TZ_MSG(("getHistoricalOffset(%.1f, %s, %d, %d, raw, dst)\n",
478 date
, local
?"T":"F", NonExistingTimeOpt
, DuplicatedTimeOpt
));
479 #if defined U_DEBUG_TZ
480 printTime(date
*1000.0);
482 int16_t transCount
= transitionCount();
484 if (transCount
> 0) {
485 double sec
= uprv_floor(date
/ U_MILLIS_PER_SECOND
);
486 if (!local
&& sec
< transitionTimeInSeconds(0)) {
487 // Before the first transition time
488 rawoff
= initialRawOffset() * U_MILLIS_PER_SECOND
;
489 dstoff
= initialDstOffset() * U_MILLIS_PER_SECOND
;
491 // Linear search from the end is the fastest approach, since
492 // most lookups will happen at/near the end.
494 for (transIdx
= transCount
- 1; transIdx
>= 0; transIdx
--) {
495 int64_t transition
= transitionTimeInSeconds(transIdx
);
497 if (local
&& (sec
>= (transition
- MAX_OFFSET_SECONDS
))) {
498 int32_t offsetBefore
= zoneOffsetAt(transIdx
- 1);
499 UBool dstBefore
= dstOffsetAt(transIdx
- 1) != 0;
501 int32_t offsetAfter
= zoneOffsetAt(transIdx
);
502 UBool dstAfter
= dstOffsetAt(transIdx
) != 0;
504 UBool dstToStd
= dstBefore
&& !dstAfter
;
505 UBool stdToDst
= !dstBefore
&& dstAfter
;
507 if (offsetAfter
- offsetBefore
>= 0) {
508 // Positive transition, which makes a non-existing local time range
509 if (((NonExistingTimeOpt
& kStdDstMask
) == kStandard
&& dstToStd
)
510 || ((NonExistingTimeOpt
& kStdDstMask
) == kDaylight
&& stdToDst
)) {
511 transition
+= offsetBefore
;
512 } else if (((NonExistingTimeOpt
& kStdDstMask
) == kStandard
&& stdToDst
)
513 || ((NonExistingTimeOpt
& kStdDstMask
) == kDaylight
&& dstToStd
)) {
514 transition
+= offsetAfter
;
515 } else if ((NonExistingTimeOpt
& kFormerLatterMask
) == kLatter
) {
516 transition
+= offsetBefore
;
518 // Interprets the time with rule before the transition,
519 // default for non-existing time range
520 transition
+= offsetAfter
;
523 // Negative transition, which makes a duplicated local time range
524 if (((DuplicatedTimeOpt
& kStdDstMask
) == kStandard
&& dstToStd
)
525 || ((DuplicatedTimeOpt
& kStdDstMask
) == kDaylight
&& stdToDst
)) {
526 transition
+= offsetAfter
;
527 } else if (((DuplicatedTimeOpt
& kStdDstMask
) == kStandard
&& stdToDst
)
528 || ((DuplicatedTimeOpt
& kStdDstMask
) == kDaylight
&& dstToStd
)) {
529 transition
+= offsetBefore
;
530 } else if ((DuplicatedTimeOpt
& kFormerLatterMask
) == kFormer
) {
531 transition
+= offsetBefore
;
533 // Interprets the time with rule after the transition,
534 // default for duplicated local time range
535 transition
+= offsetAfter
;
539 if (sec
>= transition
) {
543 // transIdx could be -1 when local=true
544 rawoff
= rawOffsetAt(transIdx
) * U_MILLIS_PER_SECOND
;
545 dstoff
= dstOffsetAt(transIdx
) * U_MILLIS_PER_SECOND
;
548 // No transitions, single pair of offsets only
549 rawoff
= initialRawOffset() * U_MILLIS_PER_SECOND
;
550 dstoff
= initialDstOffset() * U_MILLIS_PER_SECOND
;
552 U_DEBUG_TZ_MSG(("getHistoricalOffset(%.1f, %s, %d, %d, raw, dst) - raw=%d, dst=%d\n",
553 date
, local
?"T":"F", NonExistingTimeOpt
, DuplicatedTimeOpt
, rawoff
, dstoff
));
559 UBool
OlsonTimeZone::useDaylightTime() const {
560 // If DST was observed in 1942 (for example) but has never been
561 // observed from 1943 to the present, most clients will expect
562 // this method to return FALSE. This method determines whether
563 // DST is in use in the current year (at any point in the year)
564 // and returns TRUE if so.
566 UDate current
= uprv_getUTCtime();
567 if (finalZone
!= NULL
&& current
>= finalStartMillis
) {
568 return finalZone
->useDaylightTime();
571 int32_t year
, month
, dom
, dow
, doy
, mid
;
572 Grego::timeToFields(current
, year
, month
, dom
, dow
, doy
, mid
);
574 // Find start of this year, and start of next year
575 double start
= Grego::fieldsToDay(year
, 0, 1) * SECONDS_PER_DAY
;
576 double limit
= Grego::fieldsToDay(year
+1, 0, 1) * SECONDS_PER_DAY
;
578 // Return TRUE if DST is observed at any time during the current
580 for (int16_t i
= 0; i
< transitionCount(); ++i
) {
581 double transition
= (double)transitionTimeInSeconds(i
);
582 if (transition
>= limit
) {
585 if ((transition
>= start
&& dstOffsetAt(i
) != 0)
586 || (transition
> start
&& dstOffsetAt(i
- 1) != 0)) {
593 OlsonTimeZone::getDSTSavings() const{
594 if (finalZone
!= NULL
){
595 return finalZone
->getDSTSavings();
597 return TimeZone::getDSTSavings();
602 UBool
OlsonTimeZone::inDaylightTime(UDate date
, UErrorCode
& ec
) const {
604 getOffset(date
, FALSE
, raw
, dst
, ec
);
609 OlsonTimeZone::hasSameRules(const TimeZone
&other
) const {
610 if (this == &other
) {
613 const OlsonTimeZone
* z
= dynamic_cast<const OlsonTimeZone
*>(&other
);
618 // [sic] pointer comparison: typeMapData points into
619 // memory-mapped or DLL space, so if two zones have the same
620 // pointer, they are equal.
621 if (typeMapData
== z
->typeMapData
) {
625 // If the pointers are not equal, the zones may still
626 // be equal if their rules and transitions are equal
627 if ((finalZone
== NULL
&& z
->finalZone
!= NULL
)
628 || (finalZone
!= NULL
&& z
->finalZone
== NULL
)
629 || (finalZone
!= NULL
&& z
->finalZone
!= NULL
&& *finalZone
!= *z
->finalZone
)) {
633 if (finalZone
!= NULL
) {
634 if (finalStartYear
!= z
->finalStartYear
|| finalStartMillis
!= z
->finalStartMillis
) {
638 if (typeCount
!= z
->typeCount
639 || transitionCountPre32
!= z
->transitionCountPre32
640 || transitionCount32
!= z
->transitionCount32
641 || transitionCountPost32
!= z
->transitionCountPost32
) {
646 arrayEqual(transitionTimesPre32
, z
->transitionTimesPre32
, sizeof(transitionTimesPre32
[0]) * transitionCountPre32
<< 1)
647 && arrayEqual(transitionTimes32
, z
->transitionTimes32
, sizeof(transitionTimes32
[0]) * transitionCount32
)
648 && arrayEqual(transitionTimesPost32
, z
->transitionTimesPost32
, sizeof(transitionTimesPost32
[0]) * transitionCountPost32
<< 1)
649 && arrayEqual(typeOffsets
, z
->typeOffsets
, sizeof(typeOffsets
[0]) * typeCount
<< 1)
650 && arrayEqual(typeMapData
, z
->typeMapData
, sizeof(typeMapData
[0]) * transitionCount());
654 OlsonTimeZone::clearTransitionRules(void) {
656 firstTZTransition
= NULL
;
657 firstFinalTZTransition
= NULL
;
658 historicRules
= NULL
;
659 historicRuleCount
= 0;
660 finalZoneWithStartYear
= NULL
;
661 firstTZTransitionIdx
= 0;
662 transitionRulesInitOnce
.reset();
666 OlsonTimeZone::deleteTransitionRules(void) {
667 if (initialRule
!= NULL
) {
670 if (firstTZTransition
!= NULL
) {
671 delete firstTZTransition
;
673 if (firstFinalTZTransition
!= NULL
) {
674 delete firstFinalTZTransition
;
676 if (finalZoneWithStartYear
!= NULL
) {
677 delete finalZoneWithStartYear
;
679 if (historicRules
!= NULL
) {
680 for (int i
= 0; i
< historicRuleCount
; i
++) {
681 if (historicRules
[i
] != NULL
) {
682 delete historicRules
[i
];
685 uprv_free(historicRules
);
687 clearTransitionRules();
691 * Lazy transition rules initializer
694 static void U_CALLCONV
initRules(OlsonTimeZone
*This
, UErrorCode
&status
) {
695 This
->initTransitionRules(status
);
699 OlsonTimeZone::checkTransitionRules(UErrorCode
& status
) const {
700 OlsonTimeZone
*ncThis
= const_cast<OlsonTimeZone
*>(this);
701 umtx_initOnce(ncThis
->transitionRulesInitOnce
, &initRules
, ncThis
, status
);
705 OlsonTimeZone::initTransitionRules(UErrorCode
& status
) {
706 if(U_FAILURE(status
)) {
709 deleteTransitionRules();
713 UnicodeString stdName
= tzid
+ UNICODE_STRING_SIMPLE("(STD)");
714 UnicodeString dstName
= tzid
+ UNICODE_STRING_SIMPLE("(DST)");
718 // Create initial rule
719 raw
= initialRawOffset() * U_MILLIS_PER_SECOND
;
720 dst
= initialDstOffset() * U_MILLIS_PER_SECOND
;
721 initialRule
= new InitialTimeZoneRule((dst
== 0 ? stdName
: dstName
), raw
, dst
);
722 // Check to make sure initialRule was created
723 if (initialRule
== NULL
) {
724 status
= U_MEMORY_ALLOCATION_ERROR
;
725 deleteTransitionRules();
729 int32_t transCount
= transitionCount();
730 if (transCount
> 0) {
731 int16_t transitionIdx
, typeIdx
;
733 // We probably no longer need to check the first "real" transition
734 // here, because the new tzcode remove such transitions already.
735 // For now, keeping this code for just in case. Feb 19, 2010 Yoshito
736 firstTZTransitionIdx
= 0;
737 for (transitionIdx
= 0; transitionIdx
< transCount
; transitionIdx
++) {
738 if (typeMapData
[transitionIdx
] != 0) { // type 0 is the initial type
741 firstTZTransitionIdx
++;
743 if (transitionIdx
== transCount
) {
744 // Actually no transitions...
746 // Build historic rule array
747 UDate
* times
= (UDate
*)uprv_malloc(sizeof(UDate
)*transCount
); /* large enough to store all transition times */
749 status
= U_MEMORY_ALLOCATION_ERROR
;
750 deleteTransitionRules();
753 for (typeIdx
= 0; typeIdx
< typeCount
; typeIdx
++) {
754 // Gather all start times for each pair of offsets
756 for (transitionIdx
= firstTZTransitionIdx
; transitionIdx
< transCount
; transitionIdx
++) {
757 if (typeIdx
== (int16_t)typeMapData
[transitionIdx
]) {
758 UDate tt
= (UDate
)transitionTime(transitionIdx
);
759 if (finalZone
== NULL
|| tt
<= finalStartMillis
) {
760 // Exclude transitions after finalMillis
761 times
[nTimes
++] = tt
;
766 // Create a TimeArrayTimeZoneRule
767 raw
= typeOffsets
[typeIdx
<< 1] * U_MILLIS_PER_SECOND
;
768 dst
= typeOffsets
[(typeIdx
<< 1) + 1] * U_MILLIS_PER_SECOND
;
769 if (historicRules
== NULL
) {
770 historicRuleCount
= typeCount
;
771 historicRules
= (TimeArrayTimeZoneRule
**)uprv_malloc(sizeof(TimeArrayTimeZoneRule
*)*historicRuleCount
);
772 if (historicRules
== NULL
) {
773 status
= U_MEMORY_ALLOCATION_ERROR
;
774 deleteTransitionRules();
778 for (int i
= 0; i
< historicRuleCount
; i
++) {
779 // Initialize TimeArrayTimeZoneRule pointers as NULL
780 historicRules
[i
] = NULL
;
783 historicRules
[typeIdx
] = new TimeArrayTimeZoneRule((dst
== 0 ? stdName
: dstName
),
784 raw
, dst
, times
, nTimes
, DateTimeRule::UTC_TIME
);
785 // Check for memory allocation error
786 if (historicRules
[typeIdx
] == NULL
) {
787 status
= U_MEMORY_ALLOCATION_ERROR
;
788 deleteTransitionRules();
795 // Create initial transition
796 typeIdx
= (int16_t)typeMapData
[firstTZTransitionIdx
];
797 firstTZTransition
= new TimeZoneTransition((UDate
)transitionTime(firstTZTransitionIdx
),
798 *initialRule
, *historicRules
[typeIdx
]);
799 // Check to make sure firstTZTransition was created.
800 if (firstTZTransition
== NULL
) {
801 status
= U_MEMORY_ALLOCATION_ERROR
;
802 deleteTransitionRules();
807 if (finalZone
!= NULL
) {
808 // Get the first occurence of final rule starts
809 UDate startTime
= (UDate
)finalStartMillis
;
810 TimeZoneRule
*firstFinalRule
= NULL
;
812 if (finalZone
->useDaylightTime()) {
814 * Note: When an OlsonTimeZone is constructed, we should set the final year
815 * as the start year of finalZone. However, the bounday condition used for
816 * getting offset from finalZone has some problems.
817 * For now, we do not set the valid start year when the construction time
818 * and create a clone and set the start year when extracting rules.
820 finalZoneWithStartYear
= finalZone
->clone();
821 // Check to make sure finalZone was actually cloned.
822 if (finalZoneWithStartYear
== NULL
) {
823 status
= U_MEMORY_ALLOCATION_ERROR
;
824 deleteTransitionRules();
827 finalZoneWithStartYear
->setStartYear(finalStartYear
);
829 TimeZoneTransition tzt
;
830 finalZoneWithStartYear
->getNextTransition(startTime
, false, tzt
);
831 firstFinalRule
= tzt
.getTo()->clone();
832 // Check to make sure firstFinalRule received proper clone.
833 if (firstFinalRule
== NULL
) {
834 status
= U_MEMORY_ALLOCATION_ERROR
;
835 deleteTransitionRules();
838 startTime
= tzt
.getTime();
840 // final rule with no transitions
841 finalZoneWithStartYear
= finalZone
->clone();
842 // Check to make sure finalZone was actually cloned.
843 if (finalZoneWithStartYear
== NULL
) {
844 status
= U_MEMORY_ALLOCATION_ERROR
;
845 deleteTransitionRules();
848 finalZone
->getID(tzid
);
849 firstFinalRule
= new TimeArrayTimeZoneRule(tzid
,
850 finalZone
->getRawOffset(), 0, &startTime
, 1, DateTimeRule::UTC_TIME
);
851 // Check firstFinalRule was properly created.
852 if (firstFinalRule
== NULL
) {
853 status
= U_MEMORY_ALLOCATION_ERROR
;
854 deleteTransitionRules();
858 TimeZoneRule
*prevRule
= NULL
;
859 if (transCount
> 0) {
860 prevRule
= historicRules
[typeMapData
[transCount
- 1]];
862 if (prevRule
== NULL
) {
863 // No historic transitions, but only finalZone available
864 prevRule
= initialRule
;
866 firstFinalTZTransition
= new TimeZoneTransition();
867 // Check to make sure firstFinalTZTransition was created before dereferencing
868 if (firstFinalTZTransition
== NULL
) {
869 status
= U_MEMORY_ALLOCATION_ERROR
;
870 deleteTransitionRules();
873 firstFinalTZTransition
->setTime(startTime
);
874 firstFinalTZTransition
->adoptFrom(prevRule
->clone());
875 firstFinalTZTransition
->adoptTo(firstFinalRule
);
880 OlsonTimeZone::getNextTransition(UDate base
, UBool inclusive
, TimeZoneTransition
& result
) const {
881 UErrorCode status
= U_ZERO_ERROR
;
882 checkTransitionRules(status
);
883 if (U_FAILURE(status
)) {
887 if (finalZone
!= NULL
) {
888 if (inclusive
&& base
== firstFinalTZTransition
->getTime()) {
889 result
= *firstFinalTZTransition
;
891 } else if (base
>= firstFinalTZTransition
->getTime()) {
892 if (finalZone
->useDaylightTime()) {
893 //return finalZone->getNextTransition(base, inclusive, result);
894 return finalZoneWithStartYear
->getNextTransition(base
, inclusive
, result
);
896 // No more transitions
901 if (historicRules
!= NULL
) {
902 // Find a historical transition
903 int16_t transCount
= transitionCount();
904 int16_t ttidx
= transCount
- 1;
905 for (; ttidx
>= firstTZTransitionIdx
; ttidx
--) {
906 UDate t
= (UDate
)transitionTime(ttidx
);
907 if (base
> t
|| (!inclusive
&& base
== t
)) {
911 if (ttidx
== transCount
- 1) {
912 if (firstFinalTZTransition
!= NULL
) {
913 result
= *firstFinalTZTransition
;
918 } else if (ttidx
< firstTZTransitionIdx
) {
919 result
= *firstTZTransition
;
922 // Create a TimeZoneTransition
923 TimeZoneRule
*to
= historicRules
[typeMapData
[ttidx
+ 1]];
924 TimeZoneRule
*from
= historicRules
[typeMapData
[ttidx
]];
925 UDate startTime
= (UDate
)transitionTime(ttidx
+1);
927 // The transitions loaded from zoneinfo.res may contain non-transition data
928 UnicodeString fromName
, toName
;
929 from
->getName(fromName
);
931 if (fromName
== toName
&& from
->getRawOffset() == to
->getRawOffset()
932 && from
->getDSTSavings() == to
->getDSTSavings()) {
933 return getNextTransition(startTime
, false, result
);
935 result
.setTime(startTime
);
936 result
.adoptFrom(from
->clone());
937 result
.adoptTo(to
->clone());
945 OlsonTimeZone::getPreviousTransition(UDate base
, UBool inclusive
, TimeZoneTransition
& result
) const {
946 UErrorCode status
= U_ZERO_ERROR
;
947 checkTransitionRules(status
);
948 if (U_FAILURE(status
)) {
952 if (finalZone
!= NULL
) {
953 if (inclusive
&& base
== firstFinalTZTransition
->getTime()) {
954 result
= *firstFinalTZTransition
;
956 } else if (base
> firstFinalTZTransition
->getTime()) {
957 if (finalZone
->useDaylightTime()) {
958 //return finalZone->getPreviousTransition(base, inclusive, result);
959 return finalZoneWithStartYear
->getPreviousTransition(base
, inclusive
, result
);
961 result
= *firstFinalTZTransition
;
967 if (historicRules
!= NULL
) {
968 // Find a historical transition
969 int16_t ttidx
= transitionCount() - 1;
970 for (; ttidx
>= firstTZTransitionIdx
; ttidx
--) {
971 UDate t
= (UDate
)transitionTime(ttidx
);
972 if (base
> t
|| (inclusive
&& base
== t
)) {
976 if (ttidx
< firstTZTransitionIdx
) {
977 // No more transitions
979 } else if (ttidx
== firstTZTransitionIdx
) {
980 result
= *firstTZTransition
;
983 // Create a TimeZoneTransition
984 TimeZoneRule
*to
= historicRules
[typeMapData
[ttidx
]];
985 TimeZoneRule
*from
= historicRules
[typeMapData
[ttidx
-1]];
986 UDate startTime
= (UDate
)transitionTime(ttidx
);
988 // The transitions loaded from zoneinfo.res may contain non-transition data
989 UnicodeString fromName
, toName
;
990 from
->getName(fromName
);
992 if (fromName
== toName
&& from
->getRawOffset() == to
->getRawOffset()
993 && from
->getDSTSavings() == to
->getDSTSavings()) {
994 return getPreviousTransition(startTime
, false, result
);
996 result
.setTime(startTime
);
997 result
.adoptFrom(from
->clone());
998 result
.adoptTo(to
->clone());
1006 OlsonTimeZone::countTransitionRules(UErrorCode
& status
) const {
1007 if (U_FAILURE(status
)) {
1010 checkTransitionRules(status
);
1011 if (U_FAILURE(status
)) {
1016 if (historicRules
!= NULL
) {
1017 // historicRules may contain null entries when original zoneinfo data
1018 // includes non transition data.
1019 for (int32_t i
= 0; i
< historicRuleCount
; i
++) {
1020 if (historicRules
[i
] != NULL
) {
1025 if (finalZone
!= NULL
) {
1026 if (finalZone
->useDaylightTime()) {
1036 OlsonTimeZone::getTimeZoneRules(const InitialTimeZoneRule
*& initial
,
1037 const TimeZoneRule
* trsrules
[],
1039 UErrorCode
& status
) const {
1040 if (U_FAILURE(status
)) {
1043 checkTransitionRules(status
);
1044 if (U_FAILURE(status
)) {
1049 initial
= initialRule
;
1053 if (historicRules
!= NULL
&& trscount
> cnt
) {
1054 // historicRules may contain null entries when original zoneinfo data
1055 // includes non transition data.
1056 for (int32_t i
= 0; i
< historicRuleCount
; i
++) {
1057 if (historicRules
[i
] != NULL
) {
1058 trsrules
[cnt
++] = historicRules
[i
];
1059 if (cnt
>= trscount
) {
1065 if (finalZoneWithStartYear
!= NULL
&& trscount
> cnt
) {
1066 const InitialTimeZoneRule
*tmpini
;
1067 int32_t tmpcnt
= trscount
- cnt
;
1068 finalZoneWithStartYear
->getTimeZoneRules(tmpini
, &trsrules
[cnt
], tmpcnt
, status
);
1069 if (U_FAILURE(status
)) {
1074 // Set the result length
1080 #endif // !UCONFIG_NO_FORMATTING