/src/icu/icu4c/source/i18n/olsontz.cpp
Line | Count | Source |
1 | | // © 2016 and later: Unicode, Inc. and others. |
2 | | // License & terms of use: http://www.unicode.org/copyright.html |
3 | | /* |
4 | | ********************************************************************** |
5 | | * Copyright (c) 2003-2013, International Business Machines |
6 | | * Corporation and others. All Rights Reserved. |
7 | | ********************************************************************** |
8 | | * Author: Alan Liu |
9 | | * Created: July 21 2003 |
10 | | * Since: ICU 2.8 |
11 | | ********************************************************************** |
12 | | */ |
13 | | |
14 | | #include "utypeinfo.h" // for 'typeid' to work |
15 | | |
16 | | #include "olsontz.h" |
17 | | |
18 | | #if !UCONFIG_NO_FORMATTING |
19 | | |
20 | | #include "unicode/ures.h" |
21 | | #include "unicode/simpletz.h" |
22 | | #include "unicode/gregocal.h" |
23 | | #include "gregoimp.h" |
24 | | #include "cmemory.h" |
25 | | #include "uassert.h" |
26 | | #include "uvector.h" |
27 | | #include <float.h> // DBL_MAX |
28 | | #include "uresimp.h" |
29 | | #include "zonemeta.h" |
30 | | #include "umutex.h" |
31 | | |
32 | | #ifdef U_DEBUG_TZ |
33 | | # include <stdio.h> |
34 | | # include "uresimp.h" // for debugging |
35 | | |
36 | | static void debug_tz_loc(const char *f, int32_t l) |
37 | | { |
38 | | fprintf(stderr, "%s:%d: ", f, l); |
39 | | } |
40 | | |
41 | | static void debug_tz_msg(const char *pat, ...) |
42 | | { |
43 | | va_list ap; |
44 | | va_start(ap, pat); |
45 | | vfprintf(stderr, pat, ap); |
46 | | fflush(stderr); |
47 | | } |
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;} |
50 | | #else |
51 | | #define U_DEBUG_TZ_MSG(x) |
52 | | #endif |
53 | | |
54 | 0 | static UBool arrayEqual(const void *a1, const void *a2, int32_t size) { |
55 | 0 | if (a1 == nullptr && a2 == nullptr) { |
56 | 0 | return true; |
57 | 0 | } |
58 | 0 | if ((a1 != nullptr && a2 == nullptr) || (a1 == nullptr && a2 != nullptr)) { |
59 | 0 | return false; |
60 | 0 | } |
61 | 0 | if (a1 == a2) { |
62 | 0 | return true; |
63 | 0 | } |
64 | | |
65 | 0 | return (uprv_memcmp(a1, a2, size) == 0); |
66 | 0 | } |
67 | | |
68 | | U_NAMESPACE_BEGIN |
69 | | |
70 | 5.04k | #define kTRANS "trans" |
71 | 5.04k | #define kTRANSPRE32 "transPre32" |
72 | 5.04k | #define kTRANSPOST32 "transPost32" |
73 | 5.04k | #define kTYPEOFFSETS "typeOffsets" |
74 | 4.60k | #define kTYPEMAP "typeMap" |
75 | | #define kLINKS "links" |
76 | 5.04k | #define kFINALRULE "finalRule" |
77 | 5.04k | #define kFINALRAW "finalRaw" |
78 | 5.04k | #define kFINALYEAR "finalYear" |
79 | | |
80 | 0 | #define SECONDS_PER_DAY (24*60*60) |
81 | | |
82 | | static const int32_t ZEROS[] = {0,0}; |
83 | | |
84 | | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(OlsonTimeZone) |
85 | | |
86 | | /** |
87 | | * Default constructor. Creates a time zone with an empty ID and |
88 | | * a fixed GMT offset of zero. |
89 | | */ |
90 | | /*OlsonTimeZone::OlsonTimeZone() : finalYear(INT32_MAX), finalMillis(DBL_MAX), finalZone(0), transitionRulesInitialized(false) { |
91 | | clearTransitionRules(); |
92 | | constructEmpty(); |
93 | | }*/ |
94 | | |
95 | | /** |
96 | | * Construct a GMT+0 zone with no transitions. This is done when a |
97 | | * constructor fails so the resultant object is well-behaved. |
98 | | */ |
99 | 0 | void OlsonTimeZone::constructEmpty() { |
100 | 0 | canonicalID = nullptr; |
101 | |
|
102 | 0 | transitionCountPre32 = transitionCount32 = transitionCountPost32 = 0; |
103 | 0 | transitionTimesPre32 = transitionTimes32 = transitionTimesPost32 = nullptr; |
104 | |
|
105 | 0 | typeMapData = nullptr; |
106 | |
|
107 | 0 | typeCount = 1; |
108 | 0 | typeOffsets = ZEROS; |
109 | |
|
110 | 0 | finalZone = nullptr; |
111 | 0 | } |
112 | | |
113 | | /** |
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 |
119 | | */ |
120 | | OlsonTimeZone::OlsonTimeZone(const UResourceBundle* top, |
121 | | const UResourceBundle* res, |
122 | | const UnicodeString& tzid, |
123 | | UErrorCode& ec) : |
124 | 5.04k | BasicTimeZone(tzid), finalZone(nullptr) |
125 | 5.04k | { |
126 | 5.04k | clearTransitionRules(); |
127 | 5.04k | U_DEBUG_TZ_MSG(("OlsonTimeZone(%s)\n", ures_getKey((UResourceBundle*)res))); |
128 | 5.04k | if ((top == nullptr || res == nullptr) && U_SUCCESS(ec)) { |
129 | 0 | ec = U_ILLEGAL_ARGUMENT_ERROR; |
130 | 0 | } |
131 | 5.04k | if (U_SUCCESS(ec)) { |
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 |
135 | | |
136 | 5.04k | int32_t len; |
137 | 5.04k | StackUResourceBundle r; |
138 | | |
139 | | // Pre-32bit second transitions |
140 | 5.04k | ures_getByKey(res, kTRANSPRE32, r.getAlias(), &ec); |
141 | 5.04k | transitionTimesPre32 = ures_getIntVector(r.getAlias(), &len, &ec); |
142 | 5.04k | transitionCountPre32 = static_cast<int16_t>(len >> 1); |
143 | 5.04k | if (ec == U_MISSING_RESOURCE_ERROR) { |
144 | | // No pre-32bit transitions |
145 | 2.70k | transitionTimesPre32 = nullptr; |
146 | 2.70k | transitionCountPre32 = 0; |
147 | 2.70k | ec = U_ZERO_ERROR; |
148 | 2.70k | } else if (U_SUCCESS(ec) && (len < 0 || len > 0x7FFF || (len & 1) != 0) /* len must be even */) { |
149 | 0 | ec = U_INVALID_FORMAT_ERROR; |
150 | 0 | } |
151 | | |
152 | | // 32bit second transitions |
153 | 5.04k | ures_getByKey(res, kTRANS, r.getAlias(), &ec); |
154 | 5.04k | transitionTimes32 = ures_getIntVector(r.getAlias(), &len, &ec); |
155 | 5.04k | transitionCount32 = static_cast<int16_t>(len); |
156 | 5.04k | if (ec == U_MISSING_RESOURCE_ERROR) { |
157 | | // No 32bit transitions |
158 | 489 | transitionTimes32 = nullptr; |
159 | 489 | transitionCount32 = 0; |
160 | 489 | ec = U_ZERO_ERROR; |
161 | 4.55k | } else if (U_SUCCESS(ec) && (len < 0 || len > 0x7FFF)) { |
162 | 0 | ec = U_INVALID_FORMAT_ERROR; |
163 | 0 | } |
164 | | |
165 | | // Post-32bit second transitions |
166 | 5.04k | ures_getByKey(res, kTRANSPOST32, r.getAlias(), &ec); |
167 | 5.04k | transitionTimesPost32 = ures_getIntVector(r.getAlias(), &len, &ec); |
168 | 5.04k | transitionCountPost32 = static_cast<int16_t>(len >> 1); |
169 | 5.04k | if (ec == U_MISSING_RESOURCE_ERROR) { |
170 | | // No pre-32bit transitions |
171 | 4.99k | transitionTimesPost32 = nullptr; |
172 | 4.99k | transitionCountPost32 = 0; |
173 | 4.99k | ec = U_ZERO_ERROR; |
174 | 4.99k | } else if (U_SUCCESS(ec) && (len < 0 || len > 0x7FFF || (len & 1) != 0) /* len must be even */) { |
175 | 0 | ec = U_INVALID_FORMAT_ERROR; |
176 | 0 | } |
177 | | |
178 | | // Type offsets list must be of even size, with size >= 2 |
179 | 5.04k | ures_getByKey(res, kTYPEOFFSETS, r.getAlias(), &ec); |
180 | 5.04k | typeOffsets = ures_getIntVector(r.getAlias(), &len, &ec); |
181 | 5.04k | if (U_SUCCESS(ec) && (len < 2 || len > 0x7FFE || (len & 1) != 0)) { |
182 | 0 | ec = U_INVALID_FORMAT_ERROR; |
183 | 0 | } |
184 | 5.04k | typeCount = static_cast<int16_t>(len) >> 1; |
185 | | |
186 | | // Type map data must be of the same size as the transition count |
187 | 5.04k | typeMapData = nullptr; |
188 | 5.04k | if (transitionCount() > 0) { |
189 | 4.60k | ures_getByKey(res, kTYPEMAP, r.getAlias(), &ec); |
190 | 4.60k | typeMapData = ures_getBinary(r.getAlias(), &len, &ec); |
191 | 4.60k | if (ec == U_MISSING_RESOURCE_ERROR) { |
192 | | // no type mapping data |
193 | 0 | ec = U_INVALID_FORMAT_ERROR; |
194 | 4.60k | } else if (U_SUCCESS(ec) && len != transitionCount()) { |
195 | 0 | ec = U_INVALID_FORMAT_ERROR; |
196 | 0 | } |
197 | 4.60k | } |
198 | | |
199 | | // Process final rule and data, if any |
200 | 5.04k | if (U_SUCCESS(ec)) { |
201 | 5.04k | const char16_t *ruleIdUStr = ures_getStringByKey(res, kFINALRULE, &len, &ec); |
202 | 5.04k | ures_getByKey(res, kFINALRAW, r.getAlias(), &ec); |
203 | 5.04k | int32_t ruleRaw = ures_getInt(r.getAlias(), &ec); |
204 | 5.04k | ures_getByKey(res, kFINALYEAR, r.getAlias(), &ec); |
205 | 5.04k | int32_t ruleYear = ures_getInt(r.getAlias(), &ec); |
206 | 5.04k | if (U_SUCCESS(ec)) { |
207 | 1.91k | UnicodeString ruleID(true, ruleIdUStr, len); |
208 | 1.91k | UResourceBundle *rule = TimeZone::loadRule(top, ruleID, nullptr, ec); |
209 | 1.91k | const int32_t *ruleData = ures_getIntVector(rule, &len, &ec); |
210 | 1.91k | if (U_SUCCESS(ec) && len == 11) { |
211 | 1.91k | UnicodeString emptyStr; |
212 | 1.91k | finalZone = new SimpleTimeZone( |
213 | 1.91k | ruleRaw * U_MILLIS_PER_SECOND, |
214 | 1.91k | emptyStr, |
215 | 1.91k | static_cast<int8_t>(ruleData[0]), static_cast<int8_t>(ruleData[1]), static_cast<int8_t>(ruleData[2]), |
216 | 1.91k | ruleData[3] * U_MILLIS_PER_SECOND, |
217 | 1.91k | static_cast<SimpleTimeZone::TimeMode>(ruleData[4]), |
218 | 1.91k | static_cast<int8_t>(ruleData[5]), static_cast<int8_t>(ruleData[6]), static_cast<int8_t>(ruleData[7]), |
219 | 1.91k | ruleData[8] * U_MILLIS_PER_SECOND, |
220 | 1.91k | static_cast<SimpleTimeZone::TimeMode>(ruleData[9]), |
221 | 1.91k | ruleData[10] * U_MILLIS_PER_SECOND, ec); |
222 | 1.91k | if (finalZone == nullptr) { |
223 | 0 | ec = U_MEMORY_ALLOCATION_ERROR; |
224 | 1.91k | } else { |
225 | 1.91k | finalStartYear = ruleYear; |
226 | | |
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. |
232 | | |
233 | | // finalZone->setStartYear(finalStartYear); |
234 | | |
235 | | |
236 | | // Compute the millis for Jan 1, 0:00 GMT of the finalYear |
237 | | |
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 | 1.91k | finalStartMillis = Grego::fieldsToDay(finalStartYear, 0, 1) * U_MILLIS_PER_DAY; |
245 | 1.91k | } |
246 | 1.91k | } else { |
247 | 0 | ec = U_INVALID_FORMAT_ERROR; |
248 | 0 | } |
249 | 1.91k | ures_close(rule); |
250 | 3.13k | } else if (ec == U_MISSING_RESOURCE_ERROR) { |
251 | | // No final zone |
252 | 3.13k | ec = U_ZERO_ERROR; |
253 | 3.13k | } |
254 | 5.04k | } |
255 | | |
256 | | // initialize canonical ID |
257 | 5.04k | canonicalID = ZoneMeta::getCanonicalCLDRID(tzid, ec); |
258 | 5.04k | } |
259 | | |
260 | 5.04k | if (U_FAILURE(ec)) { |
261 | 0 | constructEmpty(); |
262 | 0 | } |
263 | 5.04k | } |
264 | | |
265 | | /** |
266 | | * Copy constructor |
267 | | */ |
268 | | OlsonTimeZone::OlsonTimeZone(const OlsonTimeZone& other) : |
269 | 746k | BasicTimeZone(other), finalZone(nullptr) { |
270 | 746k | *this = other; |
271 | 746k | } |
272 | | |
273 | | /** |
274 | | * Assignment operator |
275 | | */ |
276 | 746k | OlsonTimeZone& OlsonTimeZone::operator=(const OlsonTimeZone& other) { |
277 | 746k | if (this == &other) { return *this; } // self-assignment: no-op |
278 | 746k | canonicalID = other.canonicalID; |
279 | | |
280 | 746k | transitionTimesPre32 = other.transitionTimesPre32; |
281 | 746k | transitionTimes32 = other.transitionTimes32; |
282 | 746k | transitionTimesPost32 = other.transitionTimesPost32; |
283 | | |
284 | 746k | transitionCountPre32 = other.transitionCountPre32; |
285 | 746k | transitionCount32 = other.transitionCount32; |
286 | 746k | transitionCountPost32 = other.transitionCountPost32; |
287 | | |
288 | 746k | typeCount = other.typeCount; |
289 | 746k | typeOffsets = other.typeOffsets; |
290 | 746k | typeMapData = other.typeMapData; |
291 | | |
292 | 746k | delete finalZone; |
293 | 746k | finalZone = other.finalZone != nullptr ? other.finalZone->clone() : nullptr; |
294 | | |
295 | 746k | finalStartYear = other.finalStartYear; |
296 | 746k | finalStartMillis = other.finalStartMillis; |
297 | | |
298 | 746k | clearTransitionRules(); |
299 | | |
300 | 746k | return *this; |
301 | 746k | } |
302 | | |
303 | | /** |
304 | | * Destructor |
305 | | */ |
306 | 749k | OlsonTimeZone::~OlsonTimeZone() { |
307 | 749k | deleteTransitionRules(); |
308 | 749k | delete finalZone; |
309 | 749k | } |
310 | | |
311 | | /** |
312 | | * Returns true if the two TimeZone objects are equal. |
313 | | */ |
314 | 0 | bool OlsonTimeZone::operator==(const TimeZone& other) const { |
315 | 0 | return ((this == &other) || |
316 | 0 | (typeid(*this) == typeid(other) && |
317 | 0 | TimeZone::operator==(other) && |
318 | 0 | hasSameRules(other))); |
319 | 0 | } |
320 | | |
321 | | /** |
322 | | * TimeZone API. |
323 | | */ |
324 | 746k | OlsonTimeZone* OlsonTimeZone::clone() const { |
325 | 746k | return new OlsonTimeZone(*this); |
326 | 746k | } |
327 | | |
328 | | /** |
329 | | * TimeZone API. |
330 | | */ |
331 | | int32_t OlsonTimeZone::getOffset(uint8_t era, int32_t year, int32_t month, |
332 | | int32_t dom, uint8_t dow, |
333 | 0 | int32_t millis, UErrorCode& ec) const { |
334 | 0 | if (month < UCAL_JANUARY || month > UCAL_DECEMBER) { |
335 | 0 | if (U_SUCCESS(ec)) { |
336 | 0 | ec = U_ILLEGAL_ARGUMENT_ERROR; |
337 | 0 | } |
338 | 0 | return 0; |
339 | 0 | } else { |
340 | 0 | return getOffset(era, year, month, dom, dow, millis, |
341 | 0 | Grego::monthLength(year, month), |
342 | 0 | ec); |
343 | 0 | } |
344 | 0 | } |
345 | | |
346 | | /** |
347 | | * TimeZone API. |
348 | | */ |
349 | | int32_t OlsonTimeZone::getOffset(uint8_t era, int32_t year, int32_t month, |
350 | | int32_t dom, uint8_t dow, |
351 | | int32_t millis, int32_t monthLength, |
352 | 0 | UErrorCode& ec) const { |
353 | 0 | if (U_FAILURE(ec)) { |
354 | 0 | return 0; |
355 | 0 | } |
356 | | |
357 | 0 | if ((era != GregorianCalendar::AD && era != GregorianCalendar::BC) |
358 | 0 | || month < UCAL_JANUARY |
359 | 0 | || month > UCAL_DECEMBER |
360 | 0 | || dom < 1 |
361 | 0 | || dom > monthLength |
362 | 0 | || dow < UCAL_SUNDAY |
363 | 0 | || dow > UCAL_SATURDAY |
364 | 0 | || millis < 0 |
365 | 0 | || millis >= U_MILLIS_PER_DAY |
366 | 0 | || monthLength < 28 |
367 | 0 | || monthLength > 31) { |
368 | 0 | ec = U_ILLEGAL_ARGUMENT_ERROR; |
369 | 0 | return 0; |
370 | 0 | } |
371 | | |
372 | 0 | if (era == GregorianCalendar::BC) { |
373 | 0 | year = -year; |
374 | 0 | } |
375 | |
|
376 | 0 | if (finalZone != nullptr && year >= finalStartYear) { |
377 | 0 | return finalZone->getOffset(era, year, month, dom, dow, |
378 | 0 | millis, monthLength, ec); |
379 | 0 | } |
380 | | |
381 | | // Compute local epoch millis from input fields |
382 | 0 | UDate date = static_cast<UDate>(Grego::fieldsToDay(year, month, dom) * U_MILLIS_PER_DAY + millis); |
383 | 0 | int32_t rawoff, dstoff; |
384 | 0 | getHistoricalOffset(date, true, kDaylight, kStandard, rawoff, dstoff); |
385 | 0 | return rawoff + dstoff; |
386 | 0 | } |
387 | | |
388 | | /** |
389 | | * TimeZone API. |
390 | | */ |
391 | | void OlsonTimeZone::getOffset(UDate date, UBool local, int32_t& rawoff, |
392 | 1.02M | int32_t& dstoff, UErrorCode& ec) const { |
393 | 1.02M | if (U_FAILURE(ec)) { |
394 | 0 | return; |
395 | 0 | } |
396 | 1.02M | if (finalZone != nullptr && date >= finalStartMillis) { |
397 | 310k | finalZone->getOffset(date, local, rawoff, dstoff, ec); |
398 | 718k | } else { |
399 | 718k | getHistoricalOffset(date, local, kFormer, kLatter, rawoff, dstoff); |
400 | 718k | } |
401 | 1.02M | } |
402 | | |
403 | | void OlsonTimeZone::getOffsetFromLocal(UDate date, UTimeZoneLocalOption nonExistingTimeOpt, |
404 | | UTimeZoneLocalOption duplicatedTimeOpt, |
405 | 887k | int32_t& rawoff, int32_t& dstoff, UErrorCode& ec) const { |
406 | 887k | if (U_FAILURE(ec)) { |
407 | 0 | return; |
408 | 0 | } |
409 | 887k | if (finalZone != nullptr && date >= finalStartMillis) { |
410 | 298k | finalZone->getOffsetFromLocal(date, nonExistingTimeOpt, duplicatedTimeOpt, rawoff, dstoff, ec); |
411 | 588k | } else { |
412 | 588k | getHistoricalOffset(date, true, nonExistingTimeOpt, duplicatedTimeOpt, rawoff, dstoff); |
413 | 588k | } |
414 | 887k | } |
415 | | |
416 | | |
417 | | /** |
418 | | * TimeZone API. |
419 | | */ |
420 | 0 | void OlsonTimeZone::setRawOffset(int32_t /*offsetMillis*/) { |
421 | | // We don't support this operation, since OlsonTimeZones are |
422 | | // immutable (except for the ID, which is in the base class). |
423 | | |
424 | | // Nothing to do! |
425 | 0 | } |
426 | | |
427 | | /** |
428 | | * TimeZone API. |
429 | | */ |
430 | 7 | int32_t OlsonTimeZone::getRawOffset() const { |
431 | 7 | UErrorCode ec = U_ZERO_ERROR; |
432 | 7 | int32_t raw, dst; |
433 | 7 | getOffset(uprv_getUTCtime(), false, raw, dst, ec); |
434 | 7 | return raw; |
435 | 7 | } |
436 | | |
437 | | #if defined U_DEBUG_TZ |
438 | | void printTime(double ms) { |
439 | | int32_t year; |
440 | | int8_t month, dom, dow; |
441 | | int32_t millis=0; |
442 | | UErrorCode status = U_ZERO_ERROR; |
443 | | Grego::timeToFields(ms, year, month, dom, dow, millis, status); |
444 | | U_DEBUG_TZ_MSG((" getHistoricalOffset: time %.1f (%04d.%02d.%02d+%.1fh)\n", ms, |
445 | | year, month+1, dom, (millis/kOneHour))); |
446 | | } |
447 | | #endif |
448 | | |
449 | | int64_t |
450 | 5.70M | OlsonTimeZone::transitionTimeInSeconds(int16_t transIdx) const { |
451 | 5.70M | U_ASSERT(transIdx >= 0 && transIdx < transitionCount()); |
452 | | |
453 | 5.70M | if (transIdx < transitionCountPre32) { |
454 | 437k | return (static_cast<int64_t>(static_cast<uint32_t>(transitionTimesPre32[transIdx << 1])) << 32) |
455 | 437k | | static_cast<int64_t>(static_cast<uint32_t>(transitionTimesPre32[(transIdx << 1) + 1])); |
456 | 437k | } |
457 | | |
458 | 5.26M | transIdx -= transitionCountPre32; |
459 | 5.26M | if (transIdx < transitionCount32) { |
460 | 5.16M | return static_cast<int64_t>(transitionTimes32[transIdx]); |
461 | 5.16M | } |
462 | | |
463 | 102k | transIdx -= transitionCount32; |
464 | 102k | return (static_cast<int64_t>(static_cast<uint32_t>(transitionTimesPost32[transIdx << 1])) << 32) |
465 | 102k | | static_cast<int64_t>(static_cast<uint32_t>(transitionTimesPost32[(transIdx << 1) + 1])); |
466 | 5.26M | } |
467 | | |
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 | 3.70M | #define MAX_OFFSET_SECONDS 86400 |
472 | | |
473 | | void |
474 | | OlsonTimeZone::getHistoricalOffset(UDate date, UBool local, |
475 | | int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt, |
476 | 1.30M | int32_t& rawoff, int32_t& dstoff) const { |
477 | 1.30M | U_DEBUG_TZ_MSG(("getHistoricalOffset(%.1f, %s, %d, %d, raw, dst)\n", |
478 | 1.30M | date, local?"T":"F", NonExistingTimeOpt, DuplicatedTimeOpt)); |
479 | | #if defined U_DEBUG_TZ |
480 | | printTime(date*1000.0); |
481 | | #endif |
482 | 1.30M | int16_t transCount = transitionCount(); |
483 | | |
484 | 1.30M | if (transCount > 0) { |
485 | 1.12M | double sec = uprv_floor(date / U_MILLIS_PER_SECOND); |
486 | 1.12M | if (!local && sec < transitionTimeInSeconds(0)) { |
487 | | // Before the first transition time |
488 | 95.4k | rawoff = initialRawOffset() * U_MILLIS_PER_SECOND; |
489 | 95.4k | dstoff = initialDstOffset() * U_MILLIS_PER_SECOND; |
490 | 1.02M | } else { |
491 | | // Linear search from the end is the fastest approach, since |
492 | | // most lookups will happen at/near the end. |
493 | 1.02M | int16_t transIdx; |
494 | 5.14M | for (transIdx = transCount - 1; transIdx >= 0; transIdx--) { |
495 | 5.10M | int64_t transition = transitionTimeInSeconds(transIdx); |
496 | | |
497 | 5.10M | if (local && (sec >= (transition - MAX_OFFSET_SECONDS))) { |
498 | 477k | int32_t offsetBefore = zoneOffsetAt(transIdx - 1); |
499 | 477k | UBool dstBefore = dstOffsetAt(transIdx - 1) != 0; |
500 | | |
501 | 477k | int32_t offsetAfter = zoneOffsetAt(transIdx); |
502 | 477k | UBool dstAfter = dstOffsetAt(transIdx) != 0; |
503 | | |
504 | 477k | UBool dstToStd = dstBefore && !dstAfter; |
505 | 477k | UBool stdToDst = !dstBefore && dstAfter; |
506 | | |
507 | 477k | if (offsetAfter - offsetBefore >= 0) { |
508 | | // Positive transition, which makes a non-existing local time range |
509 | 102k | if (((NonExistingTimeOpt & kStdDstMask) == kStandard && dstToStd) |
510 | 102k | || ((NonExistingTimeOpt & kStdDstMask) == kDaylight && stdToDst)) { |
511 | 0 | transition += offsetBefore; |
512 | 102k | } else if (((NonExistingTimeOpt & kStdDstMask) == kStandard && stdToDst) |
513 | 102k | || ((NonExistingTimeOpt & kStdDstMask) == kDaylight && dstToStd)) { |
514 | 0 | transition += offsetAfter; |
515 | 102k | } else if ((NonExistingTimeOpt & kFormerLatterMask) == kLatter) { |
516 | 0 | transition += offsetBefore; |
517 | 102k | } else { |
518 | | // Interprets the time with rule before the transition, |
519 | | // default for non-existing time range |
520 | 102k | transition += offsetAfter; |
521 | 102k | } |
522 | 374k | } else { |
523 | | // Negative transition, which makes a duplicated local time range |
524 | 374k | if (((DuplicatedTimeOpt & kStdDstMask) == kStandard && dstToStd) |
525 | 374k | || ((DuplicatedTimeOpt & kStdDstMask) == kDaylight && stdToDst)) { |
526 | 0 | transition += offsetAfter; |
527 | 374k | } else if (((DuplicatedTimeOpt & kStdDstMask) == kStandard && stdToDst) |
528 | 374k | || ((DuplicatedTimeOpt & kStdDstMask) == kDaylight && dstToStd)) { |
529 | 0 | transition += offsetBefore; |
530 | 374k | } else if ((DuplicatedTimeOpt & kFormerLatterMask) == kFormer) { |
531 | 0 | transition += offsetBefore; |
532 | 374k | } else { |
533 | | // Interprets the time with rule after the transition, |
534 | | // default for duplicated local time range |
535 | 374k | transition += offsetAfter; |
536 | 374k | } |
537 | 374k | } |
538 | 477k | } |
539 | 5.10M | if (sec >= transition) { |
540 | 981k | break; |
541 | 981k | } |
542 | 5.10M | } |
543 | | // transIdx could be -1 when local=true |
544 | 1.02M | rawoff = rawOffsetAt(transIdx) * U_MILLIS_PER_SECOND; |
545 | 1.02M | dstoff = dstOffsetAt(transIdx) * U_MILLIS_PER_SECOND; |
546 | 1.02M | } |
547 | 1.12M | } else { |
548 | | // No transitions, single pair of offsets only |
549 | 185k | rawoff = initialRawOffset() * U_MILLIS_PER_SECOND; |
550 | 185k | dstoff = initialDstOffset() * U_MILLIS_PER_SECOND; |
551 | 185k | } |
552 | 1.30M | U_DEBUG_TZ_MSG(("getHistoricalOffset(%.1f, %s, %d, %d, raw, dst) - raw=%d, dst=%d\n", |
553 | 1.30M | date, local?"T":"F", NonExistingTimeOpt, DuplicatedTimeOpt, rawoff, dstoff)); |
554 | 1.30M | } |
555 | | |
556 | | /** |
557 | | * TimeZone API. |
558 | | */ |
559 | 0 | 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. |
565 | |
|
566 | 0 | UDate current = uprv_getUTCtime(); |
567 | 0 | if (finalZone != nullptr && current >= finalStartMillis) { |
568 | 0 | return finalZone->useDaylightTime(); |
569 | 0 | } |
570 | | |
571 | 0 | UErrorCode status = U_ZERO_ERROR; |
572 | 0 | int32_t year = Grego::timeToYear(current, status); |
573 | 0 | U_ASSERT(U_SUCCESS(status)); |
574 | 0 | if (U_FAILURE(status)) return false; // If error, just return false. |
575 | | |
576 | | // Find start of this year, and start of next year |
577 | 0 | double start = Grego::fieldsToDay(year, 0, 1) * SECONDS_PER_DAY; |
578 | 0 | double limit = Grego::fieldsToDay(year+1, 0, 1) * SECONDS_PER_DAY; |
579 | | |
580 | | // Return true if DST is observed at any time during the current |
581 | | // year. |
582 | 0 | for (int16_t i = 0; i < transitionCount(); ++i) { |
583 | 0 | double transition = static_cast<double>(transitionTimeInSeconds(i)); |
584 | 0 | if (transition >= limit) { |
585 | 0 | break; |
586 | 0 | } |
587 | 0 | if ((transition >= start && dstOffsetAt(i) != 0) |
588 | 0 | || (transition > start && dstOffsetAt(i - 1) != 0)) { |
589 | 0 | return true; |
590 | 0 | } |
591 | 0 | } |
592 | 0 | return false; |
593 | 0 | } |
594 | | int32_t |
595 | 0 | OlsonTimeZone::getDSTSavings() const{ |
596 | 0 | if (finalZone != nullptr){ |
597 | 0 | return finalZone->getDSTSavings(); |
598 | 0 | } |
599 | 0 | return TimeZone::getDSTSavings(); |
600 | 0 | } |
601 | | /** |
602 | | * TimeZone API. |
603 | | */ |
604 | 3.93k | UBool OlsonTimeZone::inDaylightTime(UDate date, UErrorCode& ec) const { |
605 | 3.93k | int32_t raw, dst; |
606 | 3.93k | getOffset(date, false, raw, dst, ec); |
607 | 3.93k | return dst != 0; |
608 | 3.93k | } |
609 | | |
610 | | UBool |
611 | 0 | OlsonTimeZone::hasSameRules(const TimeZone &other) const { |
612 | 0 | if (this == &other) { |
613 | 0 | return true; |
614 | 0 | } |
615 | 0 | const OlsonTimeZone* z = dynamic_cast<const OlsonTimeZone*>(&other); |
616 | 0 | if (z == nullptr) { |
617 | 0 | return false; |
618 | 0 | } |
619 | | |
620 | | // [sic] pointer comparison: typeMapData points into |
621 | | // memory-mapped or DLL space, so if two zones have the same |
622 | | // pointer, they are equal. |
623 | 0 | if (typeMapData == z->typeMapData) { |
624 | 0 | return true; |
625 | 0 | } |
626 | | |
627 | | // If the pointers are not equal, the zones may still |
628 | | // be equal if their rules and transitions are equal |
629 | 0 | if ((finalZone == nullptr && z->finalZone != nullptr) |
630 | 0 | || (finalZone != nullptr && z->finalZone == nullptr) |
631 | 0 | || (finalZone != nullptr && z->finalZone != nullptr && *finalZone != *z->finalZone)) { |
632 | 0 | return false; |
633 | 0 | } |
634 | | |
635 | 0 | if (finalZone != nullptr) { |
636 | 0 | if (finalStartYear != z->finalStartYear || finalStartMillis != z->finalStartMillis) { |
637 | 0 | return false; |
638 | 0 | } |
639 | 0 | } |
640 | 0 | if (typeCount != z->typeCount |
641 | 0 | || transitionCountPre32 != z->transitionCountPre32 |
642 | 0 | || transitionCount32 != z->transitionCount32 |
643 | 0 | || transitionCountPost32 != z->transitionCountPost32) { |
644 | 0 | return false; |
645 | 0 | } |
646 | | |
647 | 0 | return |
648 | 0 | arrayEqual(transitionTimesPre32, z->transitionTimesPre32, sizeof(transitionTimesPre32[0]) * transitionCountPre32 << 1) |
649 | 0 | && arrayEqual(transitionTimes32, z->transitionTimes32, sizeof(transitionTimes32[0]) * transitionCount32) |
650 | 0 | && arrayEqual(transitionTimesPost32, z->transitionTimesPost32, sizeof(transitionTimesPost32[0]) * transitionCountPost32 << 1) |
651 | 0 | && arrayEqual(typeOffsets, z->typeOffsets, sizeof(typeOffsets[0]) * typeCount << 1) |
652 | 0 | && arrayEqual(typeMapData, z->typeMapData, sizeof(typeMapData[0]) * transitionCount()); |
653 | 0 | } |
654 | | |
655 | | void |
656 | 1.50M | OlsonTimeZone::clearTransitionRules() { |
657 | 1.50M | initialRule = nullptr; |
658 | 1.50M | firstTZTransition = nullptr; |
659 | 1.50M | firstFinalTZTransition = nullptr; |
660 | 1.50M | historicRules = nullptr; |
661 | 1.50M | historicRuleCount = 0; |
662 | 1.50M | finalZoneWithStartYear = nullptr; |
663 | 1.50M | firstTZTransitionIdx = 0; |
664 | 1.50M | transitionRulesInitOnce.reset(); |
665 | 1.50M | } |
666 | | |
667 | | void |
668 | 749k | OlsonTimeZone::deleteTransitionRules() { |
669 | 749k | delete initialRule; |
670 | 749k | delete firstTZTransition; |
671 | 749k | delete firstFinalTZTransition; |
672 | 749k | delete finalZoneWithStartYear; |
673 | 749k | if (historicRules != nullptr) { |
674 | 0 | for (int i = 0; i < historicRuleCount; i++) { |
675 | 0 | if (historicRules[i] != nullptr) { |
676 | 0 | delete historicRules[i]; |
677 | 0 | } |
678 | 0 | } |
679 | 0 | uprv_free(historicRules); |
680 | 0 | } |
681 | 749k | clearTransitionRules(); |
682 | 749k | } |
683 | | |
684 | | /* |
685 | | * Lazy transition rules initializer |
686 | | */ |
687 | | |
688 | 0 | static void U_CALLCONV initRules(OlsonTimeZone *This, UErrorCode &status) { |
689 | 0 | This->initTransitionRules(status); |
690 | 0 | } |
691 | | |
692 | | void |
693 | 0 | OlsonTimeZone::checkTransitionRules(UErrorCode& status) const { |
694 | 0 | OlsonTimeZone *ncThis = const_cast<OlsonTimeZone *>(this); |
695 | 0 | umtx_initOnce(ncThis->transitionRulesInitOnce, &initRules, ncThis, status); |
696 | 0 | } |
697 | | |
698 | | void |
699 | 0 | OlsonTimeZone::initTransitionRules(UErrorCode& status) { |
700 | 0 | if(U_FAILURE(status)) { |
701 | 0 | return; |
702 | 0 | } |
703 | 0 | deleteTransitionRules(); |
704 | 0 | UnicodeString tzid; |
705 | 0 | getID(tzid); |
706 | |
|
707 | 0 | UnicodeString stdName = tzid + UNICODE_STRING_SIMPLE("(STD)"); |
708 | 0 | UnicodeString dstName = tzid + UNICODE_STRING_SIMPLE("(DST)"); |
709 | |
|
710 | 0 | int32_t raw, dst; |
711 | | |
712 | | // Create initial rule |
713 | 0 | raw = initialRawOffset() * U_MILLIS_PER_SECOND; |
714 | 0 | dst = initialDstOffset() * U_MILLIS_PER_SECOND; |
715 | 0 | initialRule = new InitialTimeZoneRule((dst == 0 ? stdName : dstName), raw, dst); |
716 | | // Check to make sure initialRule was created |
717 | 0 | if (initialRule == nullptr) { |
718 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
719 | 0 | deleteTransitionRules(); |
720 | 0 | return; |
721 | 0 | } |
722 | | |
723 | 0 | int32_t transCount = transitionCount(); |
724 | 0 | if (transCount > 0) { |
725 | 0 | int16_t transitionIdx, typeIdx; |
726 | | |
727 | | // We probably no longer need to check the first "real" transition |
728 | | // here, because the new tzcode remove such transitions already. |
729 | | // For now, keeping this code for just in case. Feb 19, 2010 Yoshito |
730 | 0 | firstTZTransitionIdx = 0; |
731 | 0 | for (transitionIdx = 0; transitionIdx < transCount; transitionIdx++) { |
732 | 0 | if (typeMapData[transitionIdx] != 0) { // type 0 is the initial type |
733 | 0 | break; |
734 | 0 | } |
735 | 0 | firstTZTransitionIdx++; |
736 | 0 | } |
737 | 0 | if (transitionIdx == transCount) { |
738 | | // Actually no transitions... |
739 | 0 | } else { |
740 | | // Build historic rule array |
741 | 0 | UDate* times = static_cast<UDate*>(uprv_malloc(sizeof(UDate) * transCount)); /* large enough to store all transition times */ |
742 | 0 | if (times == nullptr) { |
743 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
744 | 0 | deleteTransitionRules(); |
745 | 0 | return; |
746 | 0 | } |
747 | 0 | for (typeIdx = 0; typeIdx < typeCount; typeIdx++) { |
748 | | // Gather all start times for each pair of offsets |
749 | 0 | int32_t nTimes = 0; |
750 | 0 | for (transitionIdx = firstTZTransitionIdx; transitionIdx < transCount; transitionIdx++) { |
751 | 0 | if (typeIdx == static_cast<int16_t>(typeMapData[transitionIdx])) { |
752 | 0 | UDate tt = static_cast<UDate>(transitionTime(transitionIdx)); |
753 | 0 | if (finalZone == nullptr || tt <= finalStartMillis) { |
754 | | // Exclude transitions after finalMillis |
755 | 0 | times[nTimes++] = tt; |
756 | 0 | } |
757 | 0 | } |
758 | 0 | } |
759 | 0 | if (nTimes > 0) { |
760 | | // Create a TimeArrayTimeZoneRule |
761 | 0 | raw = typeOffsets[typeIdx << 1] * U_MILLIS_PER_SECOND; |
762 | 0 | dst = typeOffsets[(typeIdx << 1) + 1] * U_MILLIS_PER_SECOND; |
763 | 0 | if (historicRules == nullptr) { |
764 | 0 | historicRuleCount = typeCount; |
765 | 0 | historicRules = static_cast<TimeArrayTimeZoneRule**>(uprv_malloc(sizeof(TimeArrayTimeZoneRule*) * historicRuleCount)); |
766 | 0 | if (historicRules == nullptr) { |
767 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
768 | 0 | deleteTransitionRules(); |
769 | 0 | uprv_free(times); |
770 | 0 | return; |
771 | 0 | } |
772 | 0 | for (int i = 0; i < historicRuleCount; i++) { |
773 | | // Initialize TimeArrayTimeZoneRule pointers as nullptr |
774 | 0 | historicRules[i] = nullptr; |
775 | 0 | } |
776 | 0 | } |
777 | 0 | historicRules[typeIdx] = new TimeArrayTimeZoneRule((dst == 0 ? stdName : dstName), |
778 | 0 | raw, dst, times, nTimes, DateTimeRule::UTC_TIME); |
779 | | // Check for memory allocation error |
780 | 0 | if (historicRules[typeIdx] == nullptr) { |
781 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
782 | 0 | deleteTransitionRules(); |
783 | 0 | return; |
784 | 0 | } |
785 | 0 | } |
786 | 0 | } |
787 | 0 | uprv_free(times); |
788 | | |
789 | | // Create initial transition |
790 | 0 | typeIdx = static_cast<int16_t>(typeMapData[firstTZTransitionIdx]); |
791 | 0 | firstTZTransition = new TimeZoneTransition(static_cast<UDate>(transitionTime(firstTZTransitionIdx)), |
792 | 0 | *initialRule, *historicRules[typeIdx]); |
793 | | // Check to make sure firstTZTransition was created. |
794 | 0 | if (firstTZTransition == nullptr) { |
795 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
796 | 0 | deleteTransitionRules(); |
797 | 0 | return; |
798 | 0 | } |
799 | 0 | } |
800 | 0 | } |
801 | 0 | if (finalZone != nullptr) { |
802 | | // Get the first occurrence of final rule starts |
803 | 0 | UDate startTime = static_cast<UDate>(finalStartMillis); |
804 | 0 | TimeZoneRule *firstFinalRule = nullptr; |
805 | |
|
806 | 0 | if (finalZone->useDaylightTime()) { |
807 | | /* |
808 | | * Note: When an OlsonTimeZone is constructed, we should set the final year |
809 | | * as the start year of finalZone. However, the boundary condition used for |
810 | | * getting offset from finalZone has some problems. |
811 | | * For now, we do not set the valid start year when the construction time |
812 | | * and create a clone and set the start year when extracting rules. |
813 | | */ |
814 | 0 | finalZoneWithStartYear = finalZone->clone(); |
815 | | // Check to make sure finalZone was actually cloned. |
816 | 0 | if (finalZoneWithStartYear == nullptr) { |
817 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
818 | 0 | deleteTransitionRules(); |
819 | 0 | return; |
820 | 0 | } |
821 | 0 | finalZoneWithStartYear->setStartYear(finalStartYear); |
822 | |
|
823 | 0 | TimeZoneTransition tzt; |
824 | 0 | finalZoneWithStartYear->getNextTransition(startTime, false, tzt); |
825 | 0 | firstFinalRule = tzt.getTo()->clone(); |
826 | | // Check to make sure firstFinalRule received proper clone. |
827 | 0 | if (firstFinalRule == nullptr) { |
828 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
829 | 0 | deleteTransitionRules(); |
830 | 0 | return; |
831 | 0 | } |
832 | 0 | startTime = tzt.getTime(); |
833 | 0 | } else { |
834 | | // final rule with no transitions |
835 | 0 | finalZoneWithStartYear = finalZone->clone(); |
836 | | // Check to make sure finalZone was actually cloned. |
837 | 0 | if (finalZoneWithStartYear == nullptr) { |
838 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
839 | 0 | deleteTransitionRules(); |
840 | 0 | return; |
841 | 0 | } |
842 | 0 | finalZone->getID(tzid); |
843 | 0 | firstFinalRule = new TimeArrayTimeZoneRule(tzid, |
844 | 0 | finalZone->getRawOffset(), 0, &startTime, 1, DateTimeRule::UTC_TIME); |
845 | | // Check firstFinalRule was properly created. |
846 | 0 | if (firstFinalRule == nullptr) { |
847 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
848 | 0 | deleteTransitionRules(); |
849 | 0 | return; |
850 | 0 | } |
851 | 0 | } |
852 | 0 | TimeZoneRule *prevRule = nullptr; |
853 | 0 | if (transCount > 0) { |
854 | 0 | prevRule = historicRules[typeMapData[transCount - 1]]; |
855 | 0 | } |
856 | 0 | if (prevRule == nullptr) { |
857 | | // No historic transitions, but only finalZone available |
858 | 0 | prevRule = initialRule; |
859 | 0 | } |
860 | 0 | firstFinalTZTransition = new TimeZoneTransition(); |
861 | | // Check to make sure firstFinalTZTransition was created before dereferencing |
862 | 0 | if (firstFinalTZTransition == nullptr) { |
863 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
864 | 0 | deleteTransitionRules(); |
865 | 0 | return; |
866 | 0 | } |
867 | 0 | firstFinalTZTransition->setTime(startTime); |
868 | 0 | firstFinalTZTransition->adoptFrom(prevRule->clone()); |
869 | 0 | firstFinalTZTransition->adoptTo(firstFinalRule); |
870 | 0 | } |
871 | 0 | } |
872 | | |
873 | | UBool |
874 | 0 | OlsonTimeZone::getNextTransition(UDate base, UBool inclusive, TimeZoneTransition& result) const { |
875 | 0 | UErrorCode status = U_ZERO_ERROR; |
876 | 0 | checkTransitionRules(status); |
877 | 0 | if (U_FAILURE(status)) { |
878 | 0 | return false; |
879 | 0 | } |
880 | | |
881 | 0 | if (finalZone != nullptr) { |
882 | 0 | if (inclusive && base == firstFinalTZTransition->getTime()) { |
883 | 0 | result = *firstFinalTZTransition; |
884 | 0 | return true; |
885 | 0 | } else if (base >= firstFinalTZTransition->getTime()) { |
886 | 0 | if (finalZone->useDaylightTime()) { |
887 | | //return finalZone->getNextTransition(base, inclusive, result); |
888 | 0 | return finalZoneWithStartYear->getNextTransition(base, inclusive, result); |
889 | 0 | } else { |
890 | | // No more transitions |
891 | 0 | return false; |
892 | 0 | } |
893 | 0 | } |
894 | 0 | } |
895 | 0 | if (historicRules != nullptr) { |
896 | | // Find a historical transition |
897 | 0 | int16_t transCount = transitionCount(); |
898 | 0 | int16_t ttidx = transCount - 1; |
899 | 0 | for (; ttidx >= firstTZTransitionIdx; ttidx--) { |
900 | 0 | UDate t = static_cast<UDate>(transitionTime(ttidx)); |
901 | 0 | if (base > t || (!inclusive && base == t)) { |
902 | 0 | break; |
903 | 0 | } |
904 | 0 | } |
905 | 0 | if (ttidx == transCount - 1) { |
906 | 0 | if (firstFinalTZTransition != nullptr) { |
907 | 0 | result = *firstFinalTZTransition; |
908 | 0 | return true; |
909 | 0 | } else { |
910 | 0 | return false; |
911 | 0 | } |
912 | 0 | } else if (ttidx < firstTZTransitionIdx) { |
913 | 0 | result = *firstTZTransition; |
914 | 0 | return true; |
915 | 0 | } else { |
916 | | // Create a TimeZoneTransition |
917 | 0 | TimeZoneRule *to = historicRules[typeMapData[ttidx + 1]]; |
918 | 0 | TimeZoneRule *from = historicRules[typeMapData[ttidx]]; |
919 | 0 | UDate startTime = static_cast<UDate>(transitionTime(ttidx + 1)); |
920 | | |
921 | | // The transitions loaded from zoneinfo.res may contain non-transition data |
922 | 0 | UnicodeString fromName, toName; |
923 | 0 | from->getName(fromName); |
924 | 0 | to->getName(toName); |
925 | 0 | if (fromName == toName && from->getRawOffset() == to->getRawOffset() |
926 | 0 | && from->getDSTSavings() == to->getDSTSavings()) { |
927 | 0 | return getNextTransition(startTime, false, result); |
928 | 0 | } |
929 | 0 | result.setTime(startTime); |
930 | 0 | result.adoptFrom(from->clone()); |
931 | 0 | result.adoptTo(to->clone()); |
932 | 0 | return true; |
933 | 0 | } |
934 | 0 | } |
935 | 0 | return false; |
936 | 0 | } |
937 | | |
938 | | UBool |
939 | 0 | OlsonTimeZone::getPreviousTransition(UDate base, UBool inclusive, TimeZoneTransition& result) const { |
940 | 0 | UErrorCode status = U_ZERO_ERROR; |
941 | 0 | checkTransitionRules(status); |
942 | 0 | if (U_FAILURE(status)) { |
943 | 0 | return false; |
944 | 0 | } |
945 | | |
946 | 0 | if (finalZone != nullptr) { |
947 | 0 | if (inclusive && base == firstFinalTZTransition->getTime()) { |
948 | 0 | result = *firstFinalTZTransition; |
949 | 0 | return true; |
950 | 0 | } else if (base > firstFinalTZTransition->getTime()) { |
951 | 0 | if (finalZone->useDaylightTime()) { |
952 | | //return finalZone->getPreviousTransition(base, inclusive, result); |
953 | 0 | return finalZoneWithStartYear->getPreviousTransition(base, inclusive, result); |
954 | 0 | } else { |
955 | 0 | result = *firstFinalTZTransition; |
956 | 0 | return true; |
957 | 0 | } |
958 | 0 | } |
959 | 0 | } |
960 | | |
961 | 0 | if (historicRules != nullptr) { |
962 | | // Find a historical transition |
963 | 0 | int16_t ttidx = transitionCount() - 1; |
964 | 0 | for (; ttidx >= firstTZTransitionIdx; ttidx--) { |
965 | 0 | UDate t = static_cast<UDate>(transitionTime(ttidx)); |
966 | 0 | if (base > t || (inclusive && base == t)) { |
967 | 0 | break; |
968 | 0 | } |
969 | 0 | } |
970 | 0 | if (ttidx < firstTZTransitionIdx) { |
971 | | // No more transitions |
972 | 0 | return false; |
973 | 0 | } else if (ttidx == firstTZTransitionIdx) { |
974 | 0 | result = *firstTZTransition; |
975 | 0 | return true; |
976 | 0 | } else { |
977 | | // Create a TimeZoneTransition |
978 | 0 | TimeZoneRule *to = historicRules[typeMapData[ttidx]]; |
979 | 0 | TimeZoneRule *from = historicRules[typeMapData[ttidx-1]]; |
980 | 0 | UDate startTime = static_cast<UDate>(transitionTime(ttidx)); |
981 | | |
982 | | // The transitions loaded from zoneinfo.res may contain non-transition data |
983 | 0 | UnicodeString fromName, toName; |
984 | 0 | from->getName(fromName); |
985 | 0 | to->getName(toName); |
986 | 0 | if (fromName == toName && from->getRawOffset() == to->getRawOffset() |
987 | 0 | && from->getDSTSavings() == to->getDSTSavings()) { |
988 | 0 | return getPreviousTransition(startTime, false, result); |
989 | 0 | } |
990 | 0 | result.setTime(startTime); |
991 | 0 | result.adoptFrom(from->clone()); |
992 | 0 | result.adoptTo(to->clone()); |
993 | 0 | return true; |
994 | 0 | } |
995 | 0 | } |
996 | 0 | return false; |
997 | 0 | } |
998 | | |
999 | | int32_t |
1000 | 0 | OlsonTimeZone::countTransitionRules(UErrorCode& status) const { |
1001 | 0 | if (U_FAILURE(status)) { |
1002 | 0 | return 0; |
1003 | 0 | } |
1004 | 0 | checkTransitionRules(status); |
1005 | 0 | if (U_FAILURE(status)) { |
1006 | 0 | return 0; |
1007 | 0 | } |
1008 | | |
1009 | 0 | int32_t count = 0; |
1010 | 0 | if (historicRules != nullptr) { |
1011 | | // historicRules may contain null entries when original zoneinfo data |
1012 | | // includes non transition data. |
1013 | 0 | for (int32_t i = 0; i < historicRuleCount; i++) { |
1014 | 0 | if (historicRules[i] != nullptr) { |
1015 | 0 | count++; |
1016 | 0 | } |
1017 | 0 | } |
1018 | 0 | } |
1019 | 0 | if (finalZone != nullptr) { |
1020 | 0 | if (finalZone->useDaylightTime()) { |
1021 | 0 | count += 2; |
1022 | 0 | } else { |
1023 | 0 | count++; |
1024 | 0 | } |
1025 | 0 | } |
1026 | 0 | return count; |
1027 | 0 | } |
1028 | | |
1029 | | void |
1030 | | OlsonTimeZone::getTimeZoneRules(const InitialTimeZoneRule*& initial, |
1031 | | const TimeZoneRule* trsrules[], |
1032 | | int32_t& trscount, |
1033 | 0 | UErrorCode& status) const { |
1034 | 0 | if (U_FAILURE(status)) { |
1035 | 0 | return; |
1036 | 0 | } |
1037 | 0 | checkTransitionRules(status); |
1038 | 0 | if (U_FAILURE(status)) { |
1039 | 0 | return; |
1040 | 0 | } |
1041 | | |
1042 | | // Initial rule |
1043 | 0 | initial = initialRule; |
1044 | | |
1045 | | // Transition rules |
1046 | 0 | int32_t cnt = 0; |
1047 | 0 | if (historicRules != nullptr && trscount > cnt) { |
1048 | | // historicRules may contain null entries when original zoneinfo data |
1049 | | // includes non transition data. |
1050 | 0 | for (int32_t i = 0; i < historicRuleCount; i++) { |
1051 | 0 | if (historicRules[i] != nullptr) { |
1052 | 0 | trsrules[cnt++] = historicRules[i]; |
1053 | 0 | if (cnt >= trscount) { |
1054 | 0 | break; |
1055 | 0 | } |
1056 | 0 | } |
1057 | 0 | } |
1058 | 0 | } |
1059 | 0 | if (finalZoneWithStartYear != nullptr && trscount > cnt) { |
1060 | 0 | const InitialTimeZoneRule *tmpini; |
1061 | 0 | int32_t tmpcnt = trscount - cnt; |
1062 | 0 | finalZoneWithStartYear->getTimeZoneRules(tmpini, &trsrules[cnt], tmpcnt, status); |
1063 | 0 | if (U_FAILURE(status)) { |
1064 | 0 | return; |
1065 | 0 | } |
1066 | 0 | cnt += tmpcnt; |
1067 | 0 | } |
1068 | | // Set the result length |
1069 | 0 | trscount = cnt; |
1070 | 0 | } |
1071 | | |
1072 | | U_NAMESPACE_END |
1073 | | |
1074 | | #endif // !UCONFIG_NO_FORMATTING |
1075 | | |
1076 | | //eof |