2 **********************************************************************
3 * Copyright (c) 2003-2013, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
7 * Created: July 21 2003
9 **********************************************************************
12 #include "utypeinfo.h" // for 'typeid' to work
16 #if !UCONFIG_NO_FORMATTING
18 #include "unicode/ures.h"
19 #include "unicode/simpletz.h"
20 #include "unicode/gregocal.h"
25 #include <float.h> // DBL_MAX
26 #include "uresimp.h" // struct UResourceBundle
32 # include "uresimp.h" // for debugging
34 static void debug_tz_loc(const char *f
, int32_t l
)
36 fprintf(stderr
, "%s:%d: ", f
, l
);
39 static void debug_tz_msg(const char *pat
, ...)
43 vfprintf(stderr
, pat
, ap
);
46 // must use double parens, i.e.: U_DEBUG_TZ_MSG(("four is: %d",4));
47 #define U_DEBUG_TZ_MSG(x) {debug_tz_loc(__FILE__,__LINE__);debug_tz_msg x;}
49 #define U_DEBUG_TZ_MSG(x)
52 static UBool
arrayEqual(const void *a1
, const void *a2
, int32_t size
) {
53 if (a1
== NULL
&& a2
== NULL
) {
56 if ((a1
!= NULL
&& a2
== NULL
) || (a1
== NULL
&& a2
!= NULL
)) {
63 return (uprv_memcmp(a1
, a2
, size
) == 0);
68 #define kTRANS "trans"
69 #define kTRANSPRE32 "transPre32"
70 #define kTRANSPOST32 "transPost32"
71 #define kTYPEOFFSETS "typeOffsets"
72 #define kTYPEMAP "typeMap"
73 #define kLINKS "links"
74 #define kFINALRULE "finalRule"
75 #define kFINALRAW "finalRaw"
76 #define kFINALYEAR "finalYear"
78 #define SECONDS_PER_DAY (24*60*60)
80 static const int32_t ZEROS
[] = {0,0};
82 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(OlsonTimeZone
)
85 * Default constructor. Creates a time zone with an empty ID and
86 * a fixed GMT offset of zero.
88 /*OlsonTimeZone::OlsonTimeZone() : finalYear(INT32_MAX), finalMillis(DBL_MAX), finalZone(0), transitionRulesInitialized(FALSE) {
89 clearTransitionRules();
94 * Construct a GMT+0 zone with no transitions. This is done when a
95 * constructor fails so the resultant object is well-behaved.
97 void OlsonTimeZone::constructEmpty() {
100 transitionCountPre32
= transitionCount32
= transitionCountPost32
= 0;
101 transitionTimesPre32
= transitionTimes32
= transitionTimesPost32
= NULL
;
112 * Construct from a resource bundle
113 * @param top the top-level zoneinfo resource bundle. This is used
114 * to lookup the rule that `res' may refer to, if there is one.
115 * @param res the resource bundle of the zone to be constructed
116 * @param ec input-output error code
118 OlsonTimeZone::OlsonTimeZone(const UResourceBundle
* top
,
119 const UResourceBundle
* res
,
120 const UnicodeString
& tzid
,
122 BasicTimeZone(tzid
), finalZone(NULL
), transitionRulesInitialized(FALSE
)
124 clearTransitionRules();
125 U_DEBUG_TZ_MSG(("OlsonTimeZone(%s)\n", ures_getKey((UResourceBundle
*)res
)));
126 if ((top
== NULL
|| res
== NULL
) && U_SUCCESS(ec
)) {
127 ec
= U_ILLEGAL_ARGUMENT_ERROR
;
130 // TODO -- clean up -- Doesn't work if res points to an alias
131 // // TODO remove nonconst casts below when ures_* API is fixed
132 // setID(ures_getKey((UResourceBundle*) res)); // cast away const
136 ures_initStackObject(&r
);
138 // Pre-32bit second transitions
139 ures_getByKey(res
, kTRANSPRE32
, &r
, &ec
);
140 transitionTimesPre32
= ures_getIntVector(&r
, &len
, &ec
);
141 transitionCountPre32
= len
>> 1;
142 if (ec
== U_MISSING_RESOURCE_ERROR
) {
143 // No pre-32bit transitions
144 transitionTimesPre32
= NULL
;
145 transitionCountPre32
= 0;
147 } else if (U_SUCCESS(ec
) && (len
< 0 || len
> 0x7FFF || (len
& 1) != 0) /* len must be even */) {
148 ec
= U_INVALID_FORMAT_ERROR
;
151 // 32bit second transitions
152 ures_getByKey(res
, kTRANS
, &r
, &ec
);
153 transitionTimes32
= ures_getIntVector(&r
, &len
, &ec
);
154 transitionCount32
= len
;
155 if (ec
== U_MISSING_RESOURCE_ERROR
) {
156 // No 32bit transitions
157 transitionTimes32
= NULL
;
158 transitionCount32
= 0;
160 } else if (U_SUCCESS(ec
) && (len
< 0 || len
> 0x7FFF)) {
161 ec
= U_INVALID_FORMAT_ERROR
;
164 // Post-32bit second transitions
165 ures_getByKey(res
, kTRANSPOST32
, &r
, &ec
);
166 transitionTimesPost32
= ures_getIntVector(&r
, &len
, &ec
);
167 transitionCountPost32
= len
>> 1;
168 if (ec
== U_MISSING_RESOURCE_ERROR
) {
169 // No pre-32bit transitions
170 transitionTimesPost32
= NULL
;
171 transitionCountPost32
= 0;
173 } else if (U_SUCCESS(ec
) && (len
< 0 || len
> 0x7FFF || (len
& 1) != 0) /* len must be even */) {
174 ec
= U_INVALID_FORMAT_ERROR
;
177 // Type offsets list must be of even size, with size >= 2
178 ures_getByKey(res
, kTYPEOFFSETS
, &r
, &ec
);
179 typeOffsets
= ures_getIntVector(&r
, &len
, &ec
);
180 if (U_SUCCESS(ec
) && (len
< 2 || len
> 0x7FFE || (len
& 1) != 0)) {
181 ec
= U_INVALID_FORMAT_ERROR
;
183 typeCount
= (int16_t) len
>> 1;
185 // Type map data must be of the same size as the transition count
187 if (transitionCount() > 0) {
188 ures_getByKey(res
, kTYPEMAP
, &r
, &ec
);
189 typeMapData
= ures_getBinary(&r
, &len
, &ec
);
190 if (ec
== U_MISSING_RESOURCE_ERROR
) {
191 // no type mapping data
192 ec
= U_INVALID_FORMAT_ERROR
;
193 } else if (U_SUCCESS(ec
) && len
!= transitionCount()) {
194 ec
= U_INVALID_FORMAT_ERROR
;
198 // Process final rule and data, if any
199 const UChar
*ruleIdUStr
= ures_getStringByKey(res
, kFINALRULE
, &len
, &ec
);
200 ures_getByKey(res
, kFINALRAW
, &r
, &ec
);
201 int32_t ruleRaw
= ures_getInt(&r
, &ec
);
202 ures_getByKey(res
, kFINALYEAR
, &r
, &ec
);
203 int32_t ruleYear
= ures_getInt(&r
, &ec
);
205 UnicodeString
ruleID(TRUE
, ruleIdUStr
, len
);
206 UResourceBundle
*rule
= TimeZone::loadRule(top
, ruleID
, NULL
, ec
);
207 const int32_t *ruleData
= ures_getIntVector(rule
, &len
, &ec
);
208 if (U_SUCCESS(ec
) && len
== 11) {
209 UnicodeString emptyStr
;
210 finalZone
= new SimpleTimeZone(
211 ruleRaw
* U_MILLIS_PER_SECOND
,
213 (int8_t)ruleData
[0], (int8_t)ruleData
[1], (int8_t)ruleData
[2],
214 ruleData
[3] * U_MILLIS_PER_SECOND
,
215 (SimpleTimeZone::TimeMode
) ruleData
[4],
216 (int8_t)ruleData
[5], (int8_t)ruleData
[6], (int8_t)ruleData
[7],
217 ruleData
[8] * U_MILLIS_PER_SECOND
,
218 (SimpleTimeZone::TimeMode
) ruleData
[9],
219 ruleData
[10] * U_MILLIS_PER_SECOND
, ec
);
220 if (finalZone
== NULL
) {
221 ec
= U_MEMORY_ALLOCATION_ERROR
;
223 finalStartYear
= ruleYear
;
225 // Note: Setting finalStartYear to the finalZone is problematic. When a date is around
226 // year boundary, SimpleTimeZone may return false result when DST is observed at the
227 // beginning of year. We could apply safe margin (day or two), but when one of recurrent
228 // rules falls around year boundary, it could return false result. Without setting the
229 // start year, finalZone works fine around the year boundary of the start year.
231 // finalZone->setStartYear(finalStartYear);
234 // Compute the millis for Jan 1, 0:00 GMT of the finalYear
236 // Note: finalStartMillis is used for detecting either if
237 // historic transition data or finalZone to be used. In an
238 // extreme edge case - for example, two transitions fall into
239 // small windows of time around the year boundary, this may
240 // result incorrect offset computation. But I think it will
241 // never happen practically. Yoshito - Feb 20, 2010
242 finalStartMillis
= Grego::fieldsToDay(finalStartYear
, 0, 1) * U_MILLIS_PER_DAY
;
245 ec
= U_INVALID_FORMAT_ERROR
;
248 } else if (ec
== U_MISSING_RESOURCE_ERROR
) {
254 // initialize canonical ID
255 canonicalID
= ZoneMeta::getCanonicalCLDRID(tzid
, ec
);
266 OlsonTimeZone::OlsonTimeZone(const OlsonTimeZone
& other
) :
267 BasicTimeZone(other
), finalZone(0) {
272 * Assignment operator
274 OlsonTimeZone
& OlsonTimeZone::operator=(const OlsonTimeZone
& other
) {
275 canonicalID
= other
.canonicalID
;
277 transitionTimesPre32
= other
.transitionTimesPre32
;
278 transitionTimes32
= other
.transitionTimes32
;
279 transitionTimesPost32
= other
.transitionTimesPost32
;
281 transitionCountPre32
= other
.transitionCountPre32
;
282 transitionCount32
= other
.transitionCount32
;
283 transitionCountPost32
= other
.transitionCountPost32
;
285 typeCount
= other
.typeCount
;
286 typeOffsets
= other
.typeOffsets
;
287 typeMapData
= other
.typeMapData
;
290 finalZone
= (other
.finalZone
!= 0) ?
291 (SimpleTimeZone
*) other
.finalZone
->clone() : 0;
293 finalStartYear
= other
.finalStartYear
;
294 finalStartMillis
= other
.finalStartMillis
;
296 clearTransitionRules();
304 OlsonTimeZone::~OlsonTimeZone() {
305 deleteTransitionRules();
310 * Returns true if the two TimeZone objects are equal.
312 UBool
OlsonTimeZone::operator==(const TimeZone
& other
) const {
313 return ((this == &other
) ||
314 (typeid(*this) == typeid(other
) &&
315 TimeZone::operator==(other
) &&
316 hasSameRules(other
)));
322 TimeZone
* OlsonTimeZone::clone() const {
323 return new OlsonTimeZone(*this);
329 int32_t OlsonTimeZone::getOffset(uint8_t era
, int32_t year
, int32_t month
,
330 int32_t dom
, uint8_t dow
,
331 int32_t millis
, UErrorCode
& ec
) const {
332 if (month
< UCAL_JANUARY
|| month
> UCAL_DECEMBER
) {
334 ec
= U_ILLEGAL_ARGUMENT_ERROR
;
338 return getOffset(era
, year
, month
, dom
, dow
, millis
,
339 Grego::monthLength(year
, month
),
347 int32_t OlsonTimeZone::getOffset(uint8_t era
, int32_t year
, int32_t month
,
348 int32_t dom
, uint8_t dow
,
349 int32_t millis
, int32_t monthLength
,
350 UErrorCode
& ec
) const {
355 if ((era
!= GregorianCalendar::AD
&& era
!= GregorianCalendar::BC
)
356 || month
< UCAL_JANUARY
357 || month
> UCAL_DECEMBER
361 || dow
> UCAL_SATURDAY
363 || millis
>= U_MILLIS_PER_DAY
365 || monthLength
> 31) {
366 ec
= U_ILLEGAL_ARGUMENT_ERROR
;
370 if (era
== GregorianCalendar::BC
) {
374 if (finalZone
!= NULL
&& year
>= finalStartYear
) {
375 return finalZone
->getOffset(era
, year
, month
, dom
, dow
,
376 millis
, monthLength
, ec
);
379 // Compute local epoch millis from input fields
380 UDate date
= (UDate
)(Grego::fieldsToDay(year
, month
, dom
) * U_MILLIS_PER_DAY
+ millis
);
381 int32_t rawoff
, dstoff
;
382 getHistoricalOffset(date
, TRUE
, kDaylight
, kStandard
, rawoff
, dstoff
);
383 return rawoff
+ dstoff
;
389 void OlsonTimeZone::getOffset(UDate date
, UBool local
, int32_t& rawoff
,
390 int32_t& dstoff
, UErrorCode
& ec
) const {
394 if (finalZone
!= NULL
&& date
>= finalStartMillis
) {
395 finalZone
->getOffset(date
, local
, rawoff
, dstoff
, ec
);
397 getHistoricalOffset(date
, local
, kFormer
, kLatter
, rawoff
, dstoff
);
402 OlsonTimeZone::getOffsetFromLocal(UDate date
, int32_t nonExistingTimeOpt
, int32_t duplicatedTimeOpt
,
403 int32_t& rawoff
, int32_t& dstoff
, UErrorCode
& ec
) const {
407 if (finalZone
!= NULL
&& date
>= finalStartMillis
) {
408 finalZone
->getOffsetFromLocal(date
, nonExistingTimeOpt
, duplicatedTimeOpt
, rawoff
, dstoff
, ec
);
410 getHistoricalOffset(date
, TRUE
, nonExistingTimeOpt
, duplicatedTimeOpt
, rawoff
, dstoff
);
418 void OlsonTimeZone::setRawOffset(int32_t /*offsetMillis*/) {
419 // We don't support this operation, since OlsonTimeZones are
420 // immutable (except for the ID, which is in the base class).
428 int32_t OlsonTimeZone::getRawOffset() const {
429 UErrorCode ec
= U_ZERO_ERROR
;
431 getOffset((double) uprv_getUTCtime() * U_MILLIS_PER_SECOND
,
432 FALSE
, raw
, dst
, ec
);
436 #if defined U_DEBUG_TZ
437 void printTime(double ms
) {
438 int32_t year
, month
, dom
, dow
;
440 double days
= ClockMath::floorDivide(((double)ms
), (double)U_MILLIS_PER_DAY
, millis
);
442 Grego::dayToFields(days
, year
, month
, dom
, dow
);
443 U_DEBUG_TZ_MSG((" getHistoricalOffset: time %.1f (%04d.%02d.%02d+%.1fh)\n", ms
,
444 year
, month
+1, dom
, (millis
/kOneHour
)));
449 OlsonTimeZone::transitionTimeInSeconds(int16_t transIdx
) const {
450 U_ASSERT(transIdx
>= 0 && transIdx
< transitionCount());
452 if (transIdx
< transitionCountPre32
) {
453 return (((int64_t)((uint32_t)transitionTimesPre32
[transIdx
<< 1])) << 32)
454 | ((int64_t)((uint32_t)transitionTimesPre32
[(transIdx
<< 1) + 1]));
457 transIdx
-= transitionCountPre32
;
458 if (transIdx
< transitionCount32
) {
459 return (int64_t)transitionTimes32
[transIdx
];
462 transIdx
-= transitionCount32
;
463 return (((int64_t)((uint32_t)transitionTimesPost32
[transIdx
<< 1])) << 32)
464 | ((int64_t)((uint32_t)transitionTimesPost32
[(transIdx
<< 1) + 1]));
468 OlsonTimeZone::getHistoricalOffset(UDate date
, UBool local
,
469 int32_t NonExistingTimeOpt
, int32_t DuplicatedTimeOpt
,
470 int32_t& rawoff
, int32_t& dstoff
) const {
471 U_DEBUG_TZ_MSG(("getHistoricalOffset(%.1f, %s, %d, %d, raw, dst)\n",
472 date
, local
?"T":"F", NonExistingTimeOpt
, DuplicatedTimeOpt
));
473 #if defined U_DEBUG_TZ
474 printTime(date
*1000.0);
476 int16_t transCount
= transitionCount();
478 if (transCount
> 0) {
479 double sec
= uprv_floor(date
/ U_MILLIS_PER_SECOND
);
480 if (!local
&& sec
< transitionTimeInSeconds(0)) {
481 // Before the first transition time
482 rawoff
= initialRawOffset() * U_MILLIS_PER_SECOND
;
483 dstoff
= initialDstOffset() * U_MILLIS_PER_SECOND
;
485 // Linear search from the end is the fastest approach, since
486 // most lookups will happen at/near the end.
488 for (transIdx
= transCount
- 1; transIdx
>= 0; transIdx
--) {
489 int64_t transition
= transitionTimeInSeconds(transIdx
);
492 int32_t offsetBefore
= zoneOffsetAt(transIdx
- 1);
493 UBool dstBefore
= dstOffsetAt(transIdx
- 1) != 0;
495 int32_t offsetAfter
= zoneOffsetAt(transIdx
);
496 UBool dstAfter
= dstOffsetAt(transIdx
) != 0;
498 UBool dstToStd
= dstBefore
&& !dstAfter
;
499 UBool stdToDst
= !dstBefore
&& dstAfter
;
501 if (offsetAfter
- offsetBefore
>= 0) {
502 // Positive transition, which makes a non-existing local time range
503 if (((NonExistingTimeOpt
& kStdDstMask
) == kStandard
&& dstToStd
)
504 || ((NonExistingTimeOpt
& kStdDstMask
) == kDaylight
&& stdToDst
)) {
505 transition
+= offsetBefore
;
506 } else if (((NonExistingTimeOpt
& kStdDstMask
) == kStandard
&& stdToDst
)
507 || ((NonExistingTimeOpt
& kStdDstMask
) == kDaylight
&& dstToStd
)) {
508 transition
+= offsetAfter
;
509 } else if ((NonExistingTimeOpt
& kFormerLatterMask
) == kLatter
) {
510 transition
+= offsetBefore
;
512 // Interprets the time with rule before the transition,
513 // default for non-existing time range
514 transition
+= offsetAfter
;
517 // Negative transition, which makes a duplicated local time range
518 if (((DuplicatedTimeOpt
& kStdDstMask
) == kStandard
&& dstToStd
)
519 || ((DuplicatedTimeOpt
& kStdDstMask
) == kDaylight
&& stdToDst
)) {
520 transition
+= offsetAfter
;
521 } else if (((DuplicatedTimeOpt
& kStdDstMask
) == kStandard
&& stdToDst
)
522 || ((DuplicatedTimeOpt
& kStdDstMask
) == kDaylight
&& dstToStd
)) {
523 transition
+= offsetBefore
;
524 } else if ((DuplicatedTimeOpt
& kFormerLatterMask
) == kFormer
) {
525 transition
+= offsetBefore
;
527 // Interprets the time with rule after the transition,
528 // default for duplicated local time range
529 transition
+= offsetAfter
;
533 if (sec
>= transition
) {
537 // transIdx could be -1 when local=true
538 rawoff
= rawOffsetAt(transIdx
) * U_MILLIS_PER_SECOND
;
539 dstoff
= dstOffsetAt(transIdx
) * U_MILLIS_PER_SECOND
;
542 // No transitions, single pair of offsets only
543 rawoff
= initialRawOffset() * U_MILLIS_PER_SECOND
;
544 dstoff
= initialDstOffset() * U_MILLIS_PER_SECOND
;
546 U_DEBUG_TZ_MSG(("getHistoricalOffset(%.1f, %s, %d, %d, raw, dst) - raw=%d, dst=%d\n",
547 date
, local
?"T":"F", NonExistingTimeOpt
, DuplicatedTimeOpt
, rawoff
, dstoff
));
553 UBool
OlsonTimeZone::useDaylightTime() const {
554 // If DST was observed in 1942 (for example) but has never been
555 // observed from 1943 to the present, most clients will expect
556 // this method to return FALSE. This method determines whether
557 // DST is in use in the current year (at any point in the year)
558 // and returns TRUE if so.
560 UDate current
= uprv_getUTCtime();
561 if (finalZone
!= NULL
&& current
>= finalStartMillis
) {
562 return finalZone
->useDaylightTime();
565 int32_t year
, month
, dom
, dow
, doy
, mid
;
566 Grego::timeToFields(current
, year
, month
, dom
, dow
, doy
, mid
);
568 // Find start of this year, and start of next year
569 double start
= Grego::fieldsToDay(year
, 0, 1) * SECONDS_PER_DAY
;
570 double limit
= Grego::fieldsToDay(year
+1, 0, 1) * SECONDS_PER_DAY
;
572 // Return TRUE if DST is observed at any time during the current
574 for (int16_t i
= 0; i
< transitionCount(); ++i
) {
575 double transition
= (double)transitionTimeInSeconds(i
);
576 if (transition
>= limit
) {
579 if ((transition
>= start
&& dstOffsetAt(i
) != 0)
580 || (transition
> start
&& dstOffsetAt(i
- 1) != 0)) {
587 OlsonTimeZone::getDSTSavings() const{
588 if (finalZone
!= NULL
){
589 return finalZone
->getDSTSavings();
591 return TimeZone::getDSTSavings();
596 UBool
OlsonTimeZone::inDaylightTime(UDate date
, UErrorCode
& ec
) const {
598 getOffset(date
, FALSE
, raw
, dst
, ec
);
603 OlsonTimeZone::hasSameRules(const TimeZone
&other
) const {
604 if (this == &other
) {
607 const OlsonTimeZone
* z
= dynamic_cast<const OlsonTimeZone
*>(&other
);
612 // [sic] pointer comparison: typeMapData points into
613 // memory-mapped or DLL space, so if two zones have the same
614 // pointer, they are equal.
615 if (typeMapData
== z
->typeMapData
) {
619 // If the pointers are not equal, the zones may still
620 // be equal if their rules and transitions are equal
621 if ((finalZone
== NULL
&& z
->finalZone
!= NULL
)
622 || (finalZone
!= NULL
&& z
->finalZone
== NULL
)
623 || (finalZone
!= NULL
&& z
->finalZone
!= NULL
&& *finalZone
!= *z
->finalZone
)) {
627 if (finalZone
!= NULL
) {
628 if (finalStartYear
!= z
->finalStartYear
|| finalStartMillis
!= z
->finalStartMillis
) {
632 if (typeCount
!= z
->typeCount
633 || transitionCountPre32
!= z
->transitionCountPre32
634 || transitionCount32
!= z
->transitionCount32
635 || transitionCountPost32
!= z
->transitionCountPost32
) {
640 arrayEqual(transitionTimesPre32
, z
->transitionTimesPre32
, sizeof(transitionTimesPre32
[0]) * transitionCountPre32
<< 1)
641 && arrayEqual(transitionTimes32
, z
->transitionTimes32
, sizeof(transitionTimes32
[0]) * transitionCount32
)
642 && arrayEqual(transitionTimesPost32
, z
->transitionTimesPost32
, sizeof(transitionTimesPost32
[0]) * transitionCountPost32
<< 1)
643 && arrayEqual(typeOffsets
, z
->typeOffsets
, sizeof(typeOffsets
[0]) * typeCount
<< 1)
644 && arrayEqual(typeMapData
, z
->typeMapData
, sizeof(typeMapData
[0]) * transitionCount());
648 OlsonTimeZone::clearTransitionRules(void) {
650 firstTZTransition
= NULL
;
651 firstFinalTZTransition
= NULL
;
652 historicRules
= NULL
;
653 historicRuleCount
= 0;
654 finalZoneWithStartYear
= NULL
;
655 firstTZTransitionIdx
= 0;
656 transitionRulesInitialized
= FALSE
;
660 OlsonTimeZone::deleteTransitionRules(void) {
661 if (initialRule
!= NULL
) {
664 if (firstTZTransition
!= NULL
) {
665 delete firstTZTransition
;
667 if (firstFinalTZTransition
!= NULL
) {
668 delete firstFinalTZTransition
;
670 if (finalZoneWithStartYear
!= NULL
) {
671 delete finalZoneWithStartYear
;
673 if (historicRules
!= NULL
) {
674 for (int i
= 0; i
< historicRuleCount
; i
++) {
675 if (historicRules
[i
] != NULL
) {
676 delete historicRules
[i
];
679 uprv_free(historicRules
);
681 clearTransitionRules();
685 * Lazy transition rules initializer
687 static UMutex gLock
= U_MUTEX_INITIALIZER
;
690 OlsonTimeZone::checkTransitionRules(UErrorCode
& status
) const {
691 if (U_FAILURE(status
)) {
695 UMTX_CHECK(&gLock
, transitionRulesInitialized
, initialized
);
698 if (!transitionRulesInitialized
) {
699 OlsonTimeZone
*ncThis
= const_cast<OlsonTimeZone
*>(this);
700 ncThis
->initTransitionRules(status
);
707 OlsonTimeZone::initTransitionRules(UErrorCode
& status
) {
708 if(U_FAILURE(status
)) {
711 if (transitionRulesInitialized
) {
714 deleteTransitionRules();
718 UnicodeString stdName
= tzid
+ UNICODE_STRING_SIMPLE("(STD)");
719 UnicodeString dstName
= tzid
+ UNICODE_STRING_SIMPLE("(DST)");
723 // Create initial rule
724 raw
= initialRawOffset() * U_MILLIS_PER_SECOND
;
725 dst
= initialDstOffset() * U_MILLIS_PER_SECOND
;
726 initialRule
= new InitialTimeZoneRule((dst
== 0 ? stdName
: dstName
), raw
, dst
);
727 // Check to make sure initialRule was created
728 if (initialRule
== NULL
) {
729 status
= U_MEMORY_ALLOCATION_ERROR
;
730 deleteTransitionRules();
734 int32_t transCount
= transitionCount();
735 if (transCount
> 0) {
736 int16_t transitionIdx
, typeIdx
;
738 // We probably no longer need to check the first "real" transition
739 // here, because the new tzcode remove such transitions already.
740 // For now, keeping this code for just in case. Feb 19, 2010 Yoshito
741 firstTZTransitionIdx
= 0;
742 for (transitionIdx
= 0; transitionIdx
< transCount
; transitionIdx
++) {
743 if (typeMapData
[transitionIdx
] != 0) { // type 0 is the initial type
746 firstTZTransitionIdx
++;
748 if (transitionIdx
== transCount
) {
749 // Actually no transitions...
751 // Build historic rule array
752 UDate
* times
= (UDate
*)uprv_malloc(sizeof(UDate
)*transCount
); /* large enough to store all transition times */
754 status
= U_MEMORY_ALLOCATION_ERROR
;
755 deleteTransitionRules();
758 for (typeIdx
= 0; typeIdx
< typeCount
; typeIdx
++) {
759 // Gather all start times for each pair of offsets
761 for (transitionIdx
= firstTZTransitionIdx
; transitionIdx
< transCount
; transitionIdx
++) {
762 if (typeIdx
== (int16_t)typeMapData
[transitionIdx
]) {
763 UDate tt
= (UDate
)transitionTime(transitionIdx
);
764 if (finalZone
== NULL
|| tt
<= finalStartMillis
) {
765 // Exclude transitions after finalMillis
766 times
[nTimes
++] = tt
;
771 // Create a TimeArrayTimeZoneRule
772 raw
= typeOffsets
[typeIdx
<< 1] * U_MILLIS_PER_SECOND
;
773 dst
= typeOffsets
[(typeIdx
<< 1) + 1] * U_MILLIS_PER_SECOND
;
774 if (historicRules
== NULL
) {
775 historicRuleCount
= typeCount
;
776 historicRules
= (TimeArrayTimeZoneRule
**)uprv_malloc(sizeof(TimeArrayTimeZoneRule
*)*historicRuleCount
);
777 if (historicRules
== NULL
) {
778 status
= U_MEMORY_ALLOCATION_ERROR
;
779 deleteTransitionRules();
783 for (int i
= 0; i
< historicRuleCount
; i
++) {
784 // Initialize TimeArrayTimeZoneRule pointers as NULL
785 historicRules
[i
] = NULL
;
788 historicRules
[typeIdx
] = new TimeArrayTimeZoneRule((dst
== 0 ? stdName
: dstName
),
789 raw
, dst
, times
, nTimes
, DateTimeRule::UTC_TIME
);
790 // Check for memory allocation error
791 if (historicRules
[typeIdx
] == NULL
) {
792 status
= U_MEMORY_ALLOCATION_ERROR
;
793 deleteTransitionRules();
800 // Create initial transition
801 typeIdx
= (int16_t)typeMapData
[firstTZTransitionIdx
];
802 firstTZTransition
= new TimeZoneTransition((UDate
)transitionTime(firstTZTransitionIdx
),
803 *initialRule
, *historicRules
[typeIdx
]);
804 // Check to make sure firstTZTransition was created.
805 if (firstTZTransition
== NULL
) {
806 status
= U_MEMORY_ALLOCATION_ERROR
;
807 deleteTransitionRules();
812 if (finalZone
!= NULL
) {
813 // Get the first occurence of final rule starts
814 UDate startTime
= (UDate
)finalStartMillis
;
815 TimeZoneRule
*firstFinalRule
= NULL
;
817 if (finalZone
->useDaylightTime()) {
819 * Note: When an OlsonTimeZone is constructed, we should set the final year
820 * as the start year of finalZone. However, the bounday condition used for
821 * getting offset from finalZone has some problems.
822 * For now, we do not set the valid start year when the construction time
823 * and create a clone and set the start year when extracting rules.
825 finalZoneWithStartYear
= (SimpleTimeZone
*)finalZone
->clone();
826 // Check to make sure finalZone was actually cloned.
827 if (finalZoneWithStartYear
== NULL
) {
828 status
= U_MEMORY_ALLOCATION_ERROR
;
829 deleteTransitionRules();
832 finalZoneWithStartYear
->setStartYear(finalStartYear
);
834 TimeZoneTransition tzt
;
835 finalZoneWithStartYear
->getNextTransition(startTime
, false, tzt
);
836 firstFinalRule
= tzt
.getTo()->clone();
837 // Check to make sure firstFinalRule received proper clone.
838 if (firstFinalRule
== NULL
) {
839 status
= U_MEMORY_ALLOCATION_ERROR
;
840 deleteTransitionRules();
843 startTime
= tzt
.getTime();
845 // final rule with no transitions
846 finalZoneWithStartYear
= (SimpleTimeZone
*)finalZone
->clone();
847 // Check to make sure finalZone was actually cloned.
848 if (finalZoneWithStartYear
== NULL
) {
849 status
= U_MEMORY_ALLOCATION_ERROR
;
850 deleteTransitionRules();
853 finalZone
->getID(tzid
);
854 firstFinalRule
= new TimeArrayTimeZoneRule(tzid
,
855 finalZone
->getRawOffset(), 0, &startTime
, 1, DateTimeRule::UTC_TIME
);
856 // Check firstFinalRule was properly created.
857 if (firstFinalRule
== NULL
) {
858 status
= U_MEMORY_ALLOCATION_ERROR
;
859 deleteTransitionRules();
863 TimeZoneRule
*prevRule
= NULL
;
864 if (transCount
> 0) {
865 prevRule
= historicRules
[typeMapData
[transCount
- 1]];
867 if (prevRule
== NULL
) {
868 // No historic transitions, but only finalZone available
869 prevRule
= initialRule
;
871 firstFinalTZTransition
= new TimeZoneTransition();
872 // Check to make sure firstFinalTZTransition was created before dereferencing
873 if (firstFinalTZTransition
== NULL
) {
874 status
= U_MEMORY_ALLOCATION_ERROR
;
875 deleteTransitionRules();
878 firstFinalTZTransition
->setTime(startTime
);
879 firstFinalTZTransition
->adoptFrom(prevRule
->clone());
880 firstFinalTZTransition
->adoptTo(firstFinalRule
);
882 transitionRulesInitialized
= TRUE
;
886 OlsonTimeZone::getNextTransition(UDate base
, UBool inclusive
, TimeZoneTransition
& result
) const {
887 UErrorCode status
= U_ZERO_ERROR
;
888 checkTransitionRules(status
);
889 if (U_FAILURE(status
)) {
893 if (finalZone
!= NULL
) {
894 if (inclusive
&& base
== firstFinalTZTransition
->getTime()) {
895 result
= *firstFinalTZTransition
;
897 } else if (base
>= firstFinalTZTransition
->getTime()) {
898 if (finalZone
->useDaylightTime()) {
899 //return finalZone->getNextTransition(base, inclusive, result);
900 return finalZoneWithStartYear
->getNextTransition(base
, inclusive
, result
);
902 // No more transitions
907 if (historicRules
!= NULL
) {
908 // Find a historical transition
909 int16_t transCount
= transitionCount();
910 int16_t ttidx
= transCount
- 1;
911 for (; ttidx
>= firstTZTransitionIdx
; ttidx
--) {
912 UDate t
= (UDate
)transitionTime(ttidx
);
913 if (base
> t
|| (!inclusive
&& base
== t
)) {
917 if (ttidx
== transCount
- 1) {
918 if (firstFinalTZTransition
!= NULL
) {
919 result
= *firstFinalTZTransition
;
924 } else if (ttidx
< firstTZTransitionIdx
) {
925 result
= *firstTZTransition
;
928 // Create a TimeZoneTransition
929 TimeZoneRule
*to
= historicRules
[typeMapData
[ttidx
+ 1]];
930 TimeZoneRule
*from
= historicRules
[typeMapData
[ttidx
]];
931 UDate startTime
= (UDate
)transitionTime(ttidx
+1);
933 // The transitions loaded from zoneinfo.res may contain non-transition data
934 UnicodeString fromName
, toName
;
935 from
->getName(fromName
);
937 if (fromName
== toName
&& from
->getRawOffset() == to
->getRawOffset()
938 && from
->getDSTSavings() == to
->getDSTSavings()) {
939 return getNextTransition(startTime
, false, result
);
941 result
.setTime(startTime
);
942 result
.adoptFrom(from
->clone());
943 result
.adoptTo(to
->clone());
951 OlsonTimeZone::getPreviousTransition(UDate base
, UBool inclusive
, TimeZoneTransition
& result
) const {
952 UErrorCode status
= U_ZERO_ERROR
;
953 checkTransitionRules(status
);
954 if (U_FAILURE(status
)) {
958 if (finalZone
!= NULL
) {
959 if (inclusive
&& base
== firstFinalTZTransition
->getTime()) {
960 result
= *firstFinalTZTransition
;
962 } else if (base
> firstFinalTZTransition
->getTime()) {
963 if (finalZone
->useDaylightTime()) {
964 //return finalZone->getPreviousTransition(base, inclusive, result);
965 return finalZoneWithStartYear
->getPreviousTransition(base
, inclusive
, result
);
967 result
= *firstFinalTZTransition
;
973 if (historicRules
!= NULL
) {
974 // Find a historical transition
975 int16_t ttidx
= transitionCount() - 1;
976 for (; ttidx
>= firstTZTransitionIdx
; ttidx
--) {
977 UDate t
= (UDate
)transitionTime(ttidx
);
978 if (base
> t
|| (inclusive
&& base
== t
)) {
982 if (ttidx
< firstTZTransitionIdx
) {
983 // No more transitions
985 } else if (ttidx
== firstTZTransitionIdx
) {
986 result
= *firstTZTransition
;
989 // Create a TimeZoneTransition
990 TimeZoneRule
*to
= historicRules
[typeMapData
[ttidx
]];
991 TimeZoneRule
*from
= historicRules
[typeMapData
[ttidx
-1]];
992 UDate startTime
= (UDate
)transitionTime(ttidx
);
994 // The transitions loaded from zoneinfo.res may contain non-transition data
995 UnicodeString fromName
, toName
;
996 from
->getName(fromName
);
998 if (fromName
== toName
&& from
->getRawOffset() == to
->getRawOffset()
999 && from
->getDSTSavings() == to
->getDSTSavings()) {
1000 return getPreviousTransition(startTime
, false, result
);
1002 result
.setTime(startTime
);
1003 result
.adoptFrom(from
->clone());
1004 result
.adoptTo(to
->clone());
1012 OlsonTimeZone::countTransitionRules(UErrorCode
& status
) const {
1013 if (U_FAILURE(status
)) {
1016 checkTransitionRules(status
);
1017 if (U_FAILURE(status
)) {
1022 if (historicRules
!= NULL
) {
1023 // historicRules may contain null entries when original zoneinfo data
1024 // includes non transition data.
1025 for (int32_t i
= 0; i
< historicRuleCount
; i
++) {
1026 if (historicRules
[i
] != NULL
) {
1031 if (finalZone
!= NULL
) {
1032 if (finalZone
->useDaylightTime()) {
1042 OlsonTimeZone::getTimeZoneRules(const InitialTimeZoneRule
*& initial
,
1043 const TimeZoneRule
* trsrules
[],
1045 UErrorCode
& status
) const {
1046 if (U_FAILURE(status
)) {
1049 checkTransitionRules(status
);
1050 if (U_FAILURE(status
)) {
1055 initial
= initialRule
;
1059 if (historicRules
!= NULL
&& trscount
> cnt
) {
1060 // historicRules may contain null entries when original zoneinfo data
1061 // includes non transition data.
1062 for (int32_t i
= 0; i
< historicRuleCount
; i
++) {
1063 if (historicRules
[i
] != NULL
) {
1064 trsrules
[cnt
++] = historicRules
[i
];
1065 if (cnt
>= trscount
) {
1071 if (finalZoneWithStartYear
!= NULL
&& trscount
> cnt
) {
1072 const InitialTimeZoneRule
*tmpini
;
1073 int32_t tmpcnt
= trscount
- cnt
;
1074 finalZoneWithStartYear
->getTimeZoneRules(tmpini
, &trsrules
[cnt
], tmpcnt
, status
);
1075 if (U_FAILURE(status
)) {
1080 // Set the result length
1086 #endif // !UCONFIG_NO_FORMATTING