1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
|
/*
**********************************************************************
* Copyright (c) 2003-2005, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
* Author: Alan Liu
* Created: July 21 2003
* Since: ICU 2.8
**********************************************************************
*/
#include "olsontz.h"
#if !UCONFIG_NO_FORMATTING
#include "unicode/ures.h"
#include "unicode/simpletz.h"
#include "unicode/gregocal.h"
#include "gregoimp.h"
#include "cmemory.h"
#include "uassert.h"
#include <float.h> // DBL_MAX
#ifdef U_DEBUG_TZ
# include <stdio.h>
# include "uresimp.h" // for debugging
static void debug_tz_loc(const char *f, int32_t l)
{
fprintf(stderr, "%s:%d: ", f, l);
}
static void debug_tz_msg(const char *pat, ...)
{
va_list ap;
va_start(ap, pat);
vfprintf(stderr, pat, ap);
fflush(stderr);
}
// must use double parens, i.e.: U_DEBUG_TZ_MSG(("four is: %d",4));
#define U_DEBUG_TZ_MSG(x) {debug_tz_loc(__FILE__,__LINE__);debug_tz_msg x;}
#else
#define U_DEBUG_TZ_MSG(x)
#endif
U_NAMESPACE_BEGIN
#define SECONDS_PER_DAY (24*60*60)
static const int32_t ZEROS[] = {0,0};
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(OlsonTimeZone)
/**
* Default constructor. Creates a time zone with an empty ID and
* a fixed GMT offset of zero.
*/
OlsonTimeZone::OlsonTimeZone() : finalYear(INT32_MAX), finalMillis(DBL_MAX), finalZone(0) {
constructEmpty();
}
/**
* Construct a GMT+0 zone with no transitions. This is done when a
* constructor fails so the resultant object is well-behaved.
*/
void OlsonTimeZone::constructEmpty() {
transitionCount = 0;
typeCount = 1;
transitionTimes = typeOffsets = ZEROS;
typeData = (const uint8_t*) ZEROS;
}
/**
* Construct from a resource bundle
* @param top the top-level zoneinfo resource bundle. This is used
* to lookup the rule that `res' may refer to, if there is one.
* @param res the resource bundle of the zone to be constructed
* @param ec input-output error code
*/
OlsonTimeZone::OlsonTimeZone(const UResourceBundle* top,
const UResourceBundle* res,
UErrorCode& ec) :
finalYear(INT32_MAX), finalMillis(DBL_MAX), finalZone(0)
{
U_DEBUG_TZ_MSG(("OlsonTimeZone(%s)\n", ures_getKey((UResourceBundle*)res)));
if ((top == NULL || res == NULL) && U_SUCCESS(ec)) {
ec = U_ILLEGAL_ARGUMENT_ERROR;
}
if (U_SUCCESS(ec)) {
// TODO -- clean up -- Doesn't work if res points to an alias
// // TODO remove nonconst casts below when ures_* API is fixed
// setID(ures_getKey((UResourceBundle*) res)); // cast away const
// Size 1 is an alias TO another zone (int)
// HOWEVER, the caller should dereference this and never pass it in to us
// Size 3 is a purely historical zone (no final rules)
// Size 4 is like size 3, but with an alias list at the end
// Size 5 is a hybrid zone, with historical and final elements
// Size 6 is like size 5, but with an alias list at the end
int32_t size = ures_getSize(res);
if (size < 3 || size > 6) {
ec = U_INVALID_FORMAT_ERROR;
}
// Transitions list may be empty
int32_t i;
UResourceBundle* r = ures_getByIndex(res, 0, NULL, &ec);
transitionTimes = ures_getIntVector(r, &i, &ec);
if ((i<0 || i>0x7FFF) && U_SUCCESS(ec)) {
ec = U_INVALID_FORMAT_ERROR;
}
transitionCount = (int16_t) i;
// Type offsets list must be of even size, with size >= 2
r = ures_getByIndex(res, 1, r, &ec);
typeOffsets = ures_getIntVector(r, &i, &ec);
if ((i<2 || i>0x7FFE || ((i&1)!=0)) && U_SUCCESS(ec)) {
ec = U_INVALID_FORMAT_ERROR;
}
typeCount = (int16_t) i >> 1;
// Type data must be of the same size as the transitions list
r = ures_getByIndex(res, 2, r, &ec);
int32_t len;
typeData = ures_getBinary(r, &len, &ec);
ures_close(r);
if (len != transitionCount && U_SUCCESS(ec)) {
ec = U_INVALID_FORMAT_ERROR;
}
#if defined (U_DEBUG_TZ)
U_DEBUG_TZ_MSG(("OlsonTimeZone(%s) - size = %d, typecount %d transitioncount %d - err %s\n", ures_getKey((UResourceBundle*)res), size, typeCount, transitionCount, u_errorName(ec)));
if(U_SUCCESS(ec)) {
int32_t jj;
for(jj=0;jj<transitionCount;jj++) {
U_DEBUG_TZ_MSG((" Transition %d: time %d, typedata%d\n", jj, transitionTimes[jj], typeData[jj]));
}
for(jj=0;jj<transitionCount;jj++) {
U_DEBUG_TZ_MSG((" Type %d: offset%d\n", jj, typeOffsets[jj]));
}
}
#endif
// Process final rule and data, if any
if (size >= 5) {
int32_t ruleidLen = 0;
const UChar* idUStr = ures_getStringByIndex(res, 3, &ruleidLen, &ec);
UnicodeString ruleid(TRUE, idUStr, ruleidLen);
r = ures_getByIndex(res, 4, NULL, &ec);
const int32_t* data = ures_getIntVector(r, &len, &ec);
#if defined U_DEBUG_TZ
const char *rKey = ures_getKey(r);
const char *zKey = ures_getKey((UResourceBundle*)res);
#endif
ures_close(r);
if (U_SUCCESS(ec)) {
if (data != 0 && len == 2) {
int32_t rawOffset = data[0] * U_MILLIS_PER_SECOND;
// Subtract one from the actual final year; we
// actually store final year - 1, and compare
// using > rather than >=. This allows us to use
// INT32_MAX as an exclusive upper limit for all
// years, including INT32_MAX.
U_ASSERT(data[1] > INT32_MIN);
finalYear = data[1] - 1;
// Also compute the millis for Jan 1, 0:00 GMT of the
// finalYear. This reduces runtime computations.
finalMillis = Grego::fieldsToDay(data[1], 0, 1) * U_MILLIS_PER_DAY;
U_DEBUG_TZ_MSG(("zone%s|%s: {%d,%d}, finalYear%d, finalMillis%.1lf\n",
zKey,rKey, data[0], data[1], finalYear, finalMillis));
r = TimeZone::loadRule(top, ruleid, NULL, ec);
if (U_SUCCESS(ec)) {
// 3, 1, -1, 7200, 0, 9, -31, -1, 7200, 0, 3600
data = ures_getIntVector(r, &len, &ec);
if (U_SUCCESS(ec) && len == 11) {
UnicodeString emptyStr;
U_DEBUG_TZ_MSG(("zone%s, rule%s: {%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d}", zKey, ures_getKey(r),
data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8], data[9], data[10]));
finalZone = new SimpleTimeZone(rawOffset, emptyStr,
(int8_t)data[0], (int8_t)data[1], (int8_t)data[2],
data[3] * U_MILLIS_PER_SECOND,
(SimpleTimeZone::TimeMode) data[4],
(int8_t)data[5], (int8_t)data[6], (int8_t)data[7],
data[8] * U_MILLIS_PER_SECOND,
(SimpleTimeZone::TimeMode) data[9],
data[10] * U_MILLIS_PER_SECOND, ec);
} else {
ec = U_INVALID_FORMAT_ERROR;
}
}
ures_close(r);
} else {
ec = U_INVALID_FORMAT_ERROR;
}
}
}
}
if (U_FAILURE(ec)) {
constructEmpty();
}
}
/**
* Copy constructor
*/
OlsonTimeZone::OlsonTimeZone(const OlsonTimeZone& other) :
TimeZone(other), finalZone(0) {
*this = other;
}
/**
* Assignment operator
*/
OlsonTimeZone& OlsonTimeZone::operator=(const OlsonTimeZone& other) {
transitionCount = other.transitionCount;
typeCount = other.typeCount;
transitionTimes = other.transitionTimes;
typeOffsets = other.typeOffsets;
typeData = other.typeData;
finalYear = other.finalYear;
finalMillis = other.finalMillis;
delete finalZone;
finalZone = (other.finalZone != 0) ?
(SimpleTimeZone*) other.finalZone->clone() : 0;
return *this;
}
/**
* Destructor
*/
OlsonTimeZone::~OlsonTimeZone() {
delete finalZone;
}
/**
* Returns true if the two TimeZone objects are equal.
*/
UBool OlsonTimeZone::operator==(const TimeZone& other) const {
const OlsonTimeZone* z = (const OlsonTimeZone*) &other;
return TimeZone::operator==(other) &&
// [sic] pointer comparison: typeData points into
// memory-mapped or DLL space, so if two zones have the same
// pointer, they are equal.
(typeData == z->typeData ||
// If the pointers are not equal, the zones may still
// be equal if their rules and transitions are equal
(finalYear == z->finalYear &&
// Don't compare finalMillis; if finalYear is ==, so is finalMillis
((finalZone == 0 && z->finalZone == 0) ||
(finalZone != 0 && z->finalZone != 0 &&
*finalZone == *z->finalZone)) &&
transitionCount == z->transitionCount &&
typeCount == z->typeCount &&
uprv_memcmp(transitionTimes, z->transitionTimes,
sizeof(transitionTimes[0]) * transitionCount) == 0 &&
uprv_memcmp(typeOffsets, z->typeOffsets,
(sizeof(typeOffsets[0]) * typeCount) << 1) == 0 &&
uprv_memcmp(typeData, z->typeData,
(sizeof(typeData[0]) * typeCount)) == 0
));
}
/**
* TimeZone API.
*/
TimeZone* OlsonTimeZone::clone() const {
return new OlsonTimeZone(*this);
}
/**
* TimeZone API.
*/
int32_t OlsonTimeZone::getOffset(uint8_t era, int32_t year, int32_t month,
int32_t dom, uint8_t dow,
int32_t millis, UErrorCode& ec) const {
if (month < UCAL_JANUARY || month > UCAL_DECEMBER) {
if (U_SUCCESS(ec)) {
ec = U_ILLEGAL_ARGUMENT_ERROR;
}
return 0;
} else {
return getOffset(era, year, month, dom, dow, millis,
Grego::monthLength(year, month),
ec);
}
}
/**
* TimeZone API.
*/
int32_t OlsonTimeZone::getOffset(uint8_t era, int32_t year, int32_t month,
int32_t dom, uint8_t dow,
int32_t millis, int32_t monthLength,
UErrorCode& ec) const {
if (U_FAILURE(ec)) {
return 0;
}
if ((era != GregorianCalendar::AD && era != GregorianCalendar::BC)
|| month < UCAL_JANUARY
|| month > UCAL_DECEMBER
|| dom < 1
|| dom > monthLength
|| dow < UCAL_SUNDAY
|| dow > UCAL_SATURDAY
|| millis < 0
|| millis >= U_MILLIS_PER_DAY
|| monthLength < 28
|| monthLength > 31) {
ec = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
if (era == GregorianCalendar::BC) {
year = -year;
}
if (year > finalYear) { // [sic] >, not >=; see above
U_ASSERT(finalZone != 0);
return finalZone->getOffset(era, year, month, dom, dow,
millis, monthLength, ec);
}
// Compute local epoch seconds from input fields
double time = Grego::fieldsToDay(year, month, dom) * SECONDS_PER_DAY +
uprv_floor(millis / (double) U_MILLIS_PER_SECOND);
return zoneOffset(findTransition(time, TRUE)) * U_MILLIS_PER_SECOND;
}
/**
* TimeZone API.
*/
void OlsonTimeZone::getOffset(UDate date, UBool local, int32_t& rawoff,
int32_t& dstoff, UErrorCode& ec) const {
if (U_FAILURE(ec)) {
return;
}
// The check against finalMillis will suffice most of the time, except
// for the case in which finalMillis == DBL_MAX, date == DBL_MAX,
// and finalZone == 0. For this case we add "&& finalZone != 0".
if (date >= finalMillis && finalZone != 0) {
int32_t year, month, dom, dow;
double millis;
double days = Math::floorDivide(date, (double)U_MILLIS_PER_DAY, millis);
Grego::dayToFields(days, year, month, dom, dow);
rawoff = finalZone->getRawOffset();
if (!local) {
// Adjust from GMT to local
date += rawoff;
double days2 = Math::floorDivide(date, (double)U_MILLIS_PER_DAY, millis);
if (days2 != days) {
Grego::dayToFields(days2, year, month, dom, dow);
}
}
dstoff = finalZone->getOffset(
GregorianCalendar::AD, year, month,
dom, (uint8_t) dow, (int32_t) millis, ec) - rawoff;
return;
}
double secs = uprv_floor(date / U_MILLIS_PER_SECOND);
int16_t i = findTransition(secs, local);
rawoff = rawOffset(i) * U_MILLIS_PER_SECOND;
dstoff = dstOffset(i) * U_MILLIS_PER_SECOND;
}
/**
* TimeZone API.
*/
void OlsonTimeZone::setRawOffset(int32_t /*offsetMillis*/) {
// We don't support this operation, since OlsonTimeZones are
// immutable (except for the ID, which is in the base class).
// Nothing to do!
}
/**
* TimeZone API.
*/
int32_t OlsonTimeZone::getRawOffset() const {
UErrorCode ec = U_ZERO_ERROR;
int32_t raw, dst;
getOffset((double) uprv_getUTCtime() * U_MILLIS_PER_SECOND,
FALSE, raw, dst, ec);
return raw;
}
/**
* Find the smallest i (in 0..transitionCount-1) such that time >=
* transition(i), where transition(i) is either the GMT or the local
* transition time, as specified by `local'.
* @param time epoch seconds, either GMT or local wall
* @param local if TRUE, `time' is in local wall units, otherwise it
* is GMT
* @return an index i, where 0 <= i < transitionCount, and
* transition(i) <= time < transition(i+1), or i == 0 if
* transitionCount == 0 or time < transition(0).
*/
int16_t OlsonTimeZone::findTransition(double time, UBool local) const {
int16_t i = 0;
if (transitionCount != 0) {
// Linear search from the end is the fastest approach, since
// most lookups will happen at/near the end.
for (i = transitionCount - 1; i > 0; --i) {
int32_t transition = transitionTimes[i];
if (local) {
transition += zoneOffset(typeData[i]);
}
if (time >= transition) {
break;
}
}
U_ASSERT(i>=0 && i<transitionCount);
// Check invariants for GMT times; if these pass for GMT times
// the local logic should be working too.
U_ASSERT(local || time < transitionTimes[0] || time >= transitionTimes[i]);
U_ASSERT(local || i == transitionCount-1 || time < transitionTimes[i+1]);
i = typeData[i];
}
U_ASSERT(i>=0 && i<typeCount);
return i;
}
/**
* TimeZone API.
*/
UBool OlsonTimeZone::useDaylightTime() const {
// If DST was observed in 1942 (for example) but has never been
// observed from 1943 to the present, most clients will expect
// this method to return FALSE. This method determines whether
// DST is in use in the current year (at any point in the year)
// and returns TRUE if so.
int32_t days = (int32_t)Math::floorDivide(uprv_getUTCtime(), (double)U_MILLIS_PER_DAY); // epoch days
int32_t year, month, dom, dow;
Grego::dayToFields(days, year, month, dom, dow);
if (year > finalYear) { // [sic] >, not >=; see above
U_ASSERT(finalZone != 0 && finalZone->useDaylightTime());
return TRUE;
}
// Find start of this year, and start of next year
int32_t start = (int32_t) Grego::fieldsToDay(year, 0, 1) * SECONDS_PER_DAY;
int32_t limit = (int32_t) Grego::fieldsToDay(year+1, 0, 1) * SECONDS_PER_DAY;
// Return TRUE if DST is observed at any time during the current
// year.
for (int16_t i=0; i<transitionCount; ++i) {
if (transitionTimes[i] >= limit) {
break;
}
if (transitionTimes[i] >= start &&
dstOffset(typeData[i]) != 0) {
return TRUE;
}
}
return FALSE;
}
/**
* TimeZone API.
*/
UBool OlsonTimeZone::inDaylightTime(UDate date, UErrorCode& ec) const {
int32_t raw, dst;
getOffset(date, FALSE, raw, dst, ec);
return dst != 0;
}
U_NAMESPACE_END
#endif // !UCONFIG_NO_FORMATTING
//eof
|