aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/icu/i18n/tzrule.cpp
blob: a60fffbe020295da53b62391bddaaace609c9db4 (plain) (blame)
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
* Copyright (C) 2007-2012, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/

#include "utypeinfo.h"  // for 'typeid' to work

#include "unicode/utypes.h"

#if !UCONFIG_NO_FORMATTING

#include "unicode/tzrule.h"
#include "unicode/ucal.h"
#include "gregoimp.h"
#include "cmemory.h"
#include "uarrsort.h"

U_CDECL_BEGIN
// UComparator function for sorting start times
static int32_t U_CALLCONV
compareDates(const void * /*context*/, const void *left, const void *right) {
    UDate l = *((UDate*)left);
    UDate r = *((UDate*)right);
    int32_t res = l < r ? -1 : (l == r ? 0 : 1);
    return res;
}
U_CDECL_END

U_NAMESPACE_BEGIN

TimeZoneRule::TimeZoneRule(const UnicodeString& name, int32_t rawOffset, int32_t dstSavings)
: UObject(), fName(name), fRawOffset(rawOffset), fDSTSavings(dstSavings) {
}

TimeZoneRule::TimeZoneRule(const TimeZoneRule& source)
: UObject(source), fName(source.fName), fRawOffset(source.fRawOffset), fDSTSavings(source.fDSTSavings) {
}

TimeZoneRule::~TimeZoneRule() {
}

TimeZoneRule&
TimeZoneRule::operator=(const TimeZoneRule& right) {
    if (this != &right) {
        fName = right.fName;
        fRawOffset = right.fRawOffset;
        fDSTSavings = right.fDSTSavings;
    }
    return *this;
}

bool
TimeZoneRule::operator==(const TimeZoneRule& that) const {
    return ((this == &that) ||
            (typeid(*this) == typeid(that) &&
            fName == that.fName &&
            fRawOffset == that.fRawOffset &&
            fDSTSavings == that.fDSTSavings));
}

bool
TimeZoneRule::operator!=(const TimeZoneRule& that) const {
    return !operator==(that);
}

UnicodeString&
TimeZoneRule::getName(UnicodeString& name) const {
    name = fName;
    return name;
}

int32_t
TimeZoneRule::getRawOffset(void) const {
    return fRawOffset;
}

int32_t
TimeZoneRule::getDSTSavings(void) const {
    return fDSTSavings;
}

UBool
TimeZoneRule::isEquivalentTo(const TimeZoneRule& other) const {
    return ((this == &other) ||
            (typeid(*this) == typeid(other) &&
            fRawOffset == other.fRawOffset &&
            fDSTSavings == other.fDSTSavings));
}


UOBJECT_DEFINE_RTTI_IMPLEMENTATION(InitialTimeZoneRule)

InitialTimeZoneRule::InitialTimeZoneRule(const UnicodeString& name,
                                         int32_t rawOffset,
                                         int32_t dstSavings)
: TimeZoneRule(name, rawOffset, dstSavings) {
}

InitialTimeZoneRule::InitialTimeZoneRule(const InitialTimeZoneRule& source)
: TimeZoneRule(source) {
}

InitialTimeZoneRule::~InitialTimeZoneRule() {
}

InitialTimeZoneRule*
InitialTimeZoneRule::clone() const {
    return new InitialTimeZoneRule(*this);
}

InitialTimeZoneRule&
InitialTimeZoneRule::operator=(const InitialTimeZoneRule& right) {
    if (this != &right) {
        TimeZoneRule::operator=(right);
    }
    return *this;
}

bool
InitialTimeZoneRule::operator==(const TimeZoneRule& that) const {
    return ((this == &that) ||
            (typeid(*this) == typeid(that) &&
            TimeZoneRule::operator==(that)));
}

bool
InitialTimeZoneRule::operator!=(const TimeZoneRule& that) const {
    return !operator==(that);
}

UBool
InitialTimeZoneRule::isEquivalentTo(const TimeZoneRule& other) const {
    if (this == &other) {
        return TRUE;
    }
    if (typeid(*this) != typeid(other) || TimeZoneRule::isEquivalentTo(other) == FALSE) {
        return FALSE;
    }
    return TRUE;
}

UBool
InitialTimeZoneRule::getFirstStart(int32_t /*prevRawOffset*/,
                                  int32_t /*prevDSTSavings*/,
                                  UDate& /*result*/) const {
    return FALSE;
}

UBool
InitialTimeZoneRule::getFinalStart(int32_t /*prevRawOffset*/,
                                  int32_t /*prevDSTSavings*/,
                                  UDate& /*result*/) const {
    return FALSE;
}

UBool
InitialTimeZoneRule::getNextStart(UDate /*base*/,
                                 int32_t /*prevRawOffset*/,
                                 int32_t /*prevDSTSavings*/,
                                 UBool /*inclusive*/,
                                 UDate& /*result*/) const {
    return FALSE;
}

UBool
InitialTimeZoneRule::getPreviousStart(UDate /*base*/,
                                     int32_t /*prevRawOffset*/,
                                     int32_t /*prevDSTSavings*/,
                                     UBool /*inclusive*/,
                                     UDate& /*result*/) const {
    return FALSE;
}


UOBJECT_DEFINE_RTTI_IMPLEMENTATION(AnnualTimeZoneRule)

const int32_t AnnualTimeZoneRule::MAX_YEAR = 0x7FFFFFFF; /* max signed int32 */

AnnualTimeZoneRule::AnnualTimeZoneRule(const UnicodeString& name,
                                       int32_t rawOffset,
                                       int32_t dstSavings, 
                                       const DateTimeRule& dateTimeRule,
                                       int32_t startYear,
                                       int32_t endYear)
: TimeZoneRule(name, rawOffset, dstSavings), fDateTimeRule(new DateTimeRule(dateTimeRule)),
  fStartYear(startYear), fEndYear(endYear) {
}

AnnualTimeZoneRule::AnnualTimeZoneRule(const UnicodeString& name,
                                       int32_t rawOffset,
                                       int32_t dstSavings, 
                                       DateTimeRule* dateTimeRule,
                                       int32_t startYear,
                                       int32_t endYear)
: TimeZoneRule(name, rawOffset, dstSavings), fDateTimeRule(dateTimeRule),
  fStartYear(startYear), fEndYear(endYear) {
}

AnnualTimeZoneRule::AnnualTimeZoneRule(const AnnualTimeZoneRule& source)
: TimeZoneRule(source), fDateTimeRule(new DateTimeRule(*(source.fDateTimeRule))),
  fStartYear(source.fStartYear), fEndYear(source.fEndYear) {
}

AnnualTimeZoneRule::~AnnualTimeZoneRule() {
    delete fDateTimeRule;
}

AnnualTimeZoneRule*
AnnualTimeZoneRule::clone(void) const {
    return new AnnualTimeZoneRule(*this);
}

AnnualTimeZoneRule&
AnnualTimeZoneRule::operator=(const AnnualTimeZoneRule& right) {
    if (this != &right) {
        TimeZoneRule::operator=(right);
        delete fDateTimeRule;
        fDateTimeRule = right.fDateTimeRule->clone();
        fStartYear = right.fStartYear;
        fEndYear = right.fEndYear;
    }
    return *this;
}

bool
AnnualTimeZoneRule::operator==(const TimeZoneRule& that) const {
    if (this == &that) {
        return true;
    }
    if (typeid(*this) != typeid(that)) {
        return false;
    }
    AnnualTimeZoneRule *atzr = (AnnualTimeZoneRule*)&that;
    return (*fDateTimeRule == *(atzr->fDateTimeRule) &&
            fStartYear == atzr->fStartYear &&
            fEndYear == atzr->fEndYear);
}

bool
AnnualTimeZoneRule::operator!=(const TimeZoneRule& that) const {
    return !operator==(that);
}

const DateTimeRule*
AnnualTimeZoneRule::getRule() const {
    return fDateTimeRule;
}

int32_t
AnnualTimeZoneRule::getStartYear() const {
    return fStartYear;
}

int32_t
AnnualTimeZoneRule::getEndYear() const {
    return fEndYear;
}

UBool
AnnualTimeZoneRule::getStartInYear(int32_t year,
                                   int32_t prevRawOffset,
                                   int32_t prevDSTSavings,
                                   UDate &result) const {
    if (year < fStartYear || year > fEndYear) {
        return FALSE;
    }
    double ruleDay;
    DateTimeRule::DateRuleType type = fDateTimeRule->getDateRuleType();
    if (type == DateTimeRule::DOM) {
        ruleDay = Grego::fieldsToDay(year, fDateTimeRule->getRuleMonth(), fDateTimeRule->getRuleDayOfMonth());
    } else {
        UBool after = TRUE;
        if (type == DateTimeRule::DOW) {
            // Normalize DOW rule into DOW_GEQ_DOM or DOW_LEQ_DOM
            int32_t weeks = fDateTimeRule->getRuleWeekInMonth();
            if (weeks > 0) {
                ruleDay = Grego::fieldsToDay(year, fDateTimeRule->getRuleMonth(), 1);
                ruleDay += 7 * (weeks - 1);
            } else {
                after = FALSE;
                ruleDay = Grego::fieldsToDay(year, fDateTimeRule->getRuleMonth(),
                    Grego::monthLength(year, fDateTimeRule->getRuleMonth()));
                ruleDay += 7 * (weeks + 1);
           }
        } else {
            int32_t month = fDateTimeRule->getRuleMonth();
            int32_t dom = fDateTimeRule->getRuleDayOfMonth();
            if (type == DateTimeRule::DOW_LEQ_DOM) {
                after = FALSE;
                // Handle Feb <=29
                if (month == UCAL_FEBRUARY && dom == 29 && !Grego::isLeapYear(year)) {
                    dom--;
                }
            }
            ruleDay = Grego::fieldsToDay(year, month, dom);
        }
        int32_t dow = Grego::dayOfWeek(ruleDay);
        int32_t delta = fDateTimeRule->getRuleDayOfWeek() - dow;
        if (after) {
            delta = delta < 0 ? delta + 7 : delta;
        } else {
            delta = delta > 0 ? delta - 7 : delta;
        }
        ruleDay += delta;
    }

    result = ruleDay*U_MILLIS_PER_DAY + fDateTimeRule->getRuleMillisInDay();
    if (fDateTimeRule->getTimeRuleType() != DateTimeRule::UTC_TIME) {
        result -= prevRawOffset;
    }
    if (fDateTimeRule->getTimeRuleType() == DateTimeRule::WALL_TIME) {
        result -= prevDSTSavings;
    }
    return TRUE;
}

UBool
AnnualTimeZoneRule::isEquivalentTo(const TimeZoneRule& other) const {
    if (this == &other) {
        return TRUE;
    }
    if (typeid(*this) != typeid(other) || TimeZoneRule::isEquivalentTo(other) == FALSE) {
        return FALSE;
    }
    AnnualTimeZoneRule* that = (AnnualTimeZoneRule*)&other;
    return (*fDateTimeRule == *(that->fDateTimeRule) &&
            fStartYear == that->fStartYear &&
            fEndYear == that->fEndYear);
}

UBool
AnnualTimeZoneRule::getFirstStart(int32_t prevRawOffset,
                                  int32_t prevDSTSavings,
                                  UDate& result) const {
    return getStartInYear(fStartYear, prevRawOffset, prevDSTSavings, result);
}

UBool
AnnualTimeZoneRule::getFinalStart(int32_t prevRawOffset,
                                  int32_t prevDSTSavings,
                                  UDate& result) const {
    if (fEndYear == MAX_YEAR) {
        return FALSE;
    }
    return getStartInYear(fEndYear, prevRawOffset, prevDSTSavings, result);
}

UBool
AnnualTimeZoneRule::getNextStart(UDate base,
                                 int32_t prevRawOffset,
                                 int32_t prevDSTSavings,
                                 UBool inclusive,
                                 UDate& result) const {
    int32_t year, month, dom, dow, doy, mid;
    Grego::timeToFields(base, year, month, dom, dow, doy, mid);
    if (year < fStartYear) {
        return getFirstStart(prevRawOffset, prevDSTSavings, result);
    }
    UDate tmp;
    if (getStartInYear(year, prevRawOffset, prevDSTSavings, tmp)) {
        if (tmp < base || (!inclusive && (tmp == base))) {
            // Return the next one
            return getStartInYear(year + 1, prevRawOffset, prevDSTSavings, result);
        } else {
            result = tmp;
            return TRUE;
        }
    }
    return FALSE;
}

UBool
AnnualTimeZoneRule::getPreviousStart(UDate base,
                                     int32_t prevRawOffset,
                                     int32_t prevDSTSavings,
                                     UBool inclusive,
                                     UDate& result) const {
    int32_t year, month, dom, dow, doy, mid;
    Grego::timeToFields(base, year, month, dom, dow, doy, mid);
    if (year > fEndYear) {
        return getFinalStart(prevRawOffset, prevDSTSavings, result);
    }
    UDate tmp;
    if (getStartInYear(year, prevRawOffset, prevDSTSavings, tmp)) {
        if (tmp > base || (!inclusive && (tmp == base))) {
            // Return the previous one
            return getStartInYear(year - 1, prevRawOffset, prevDSTSavings, result);
        } else {
            result = tmp;
            return TRUE;
        }
    }
    return FALSE;
}

UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TimeArrayTimeZoneRule)

TimeArrayTimeZoneRule::TimeArrayTimeZoneRule(const UnicodeString& name,
                                             int32_t rawOffset,
                                             int32_t dstSavings,
                                             const UDate* startTimes,
                                             int32_t numStartTimes,
                                             DateTimeRule::TimeRuleType timeRuleType)
: TimeZoneRule(name, rawOffset, dstSavings), fTimeRuleType(timeRuleType),
  fStartTimes(NULL) {
    UErrorCode status = U_ZERO_ERROR;
    initStartTimes(startTimes, numStartTimes, status);
    //TODO - status?
}


TimeArrayTimeZoneRule::TimeArrayTimeZoneRule(const TimeArrayTimeZoneRule& source)
: TimeZoneRule(source), fTimeRuleType(source.fTimeRuleType), fStartTimes(NULL) {
    UErrorCode status = U_ZERO_ERROR;
    initStartTimes(source.fStartTimes, source.fNumStartTimes, status);
    //TODO - status?
}


TimeArrayTimeZoneRule::~TimeArrayTimeZoneRule() {
    if (fStartTimes != NULL && fStartTimes != fLocalStartTimes) {
        uprv_free(fStartTimes);
    }
}

TimeArrayTimeZoneRule*
TimeArrayTimeZoneRule::clone(void) const {
    return new TimeArrayTimeZoneRule(*this);
}


TimeArrayTimeZoneRule&
TimeArrayTimeZoneRule::operator=(const TimeArrayTimeZoneRule& right) {
    if (this != &right) {
        TimeZoneRule::operator=(right);
        UErrorCode status = U_ZERO_ERROR;
        initStartTimes(right.fStartTimes, right.fNumStartTimes, status);
        //TODO - status?
        fTimeRuleType = right.fTimeRuleType;        
    }
    return *this;
}

bool
TimeArrayTimeZoneRule::operator==(const TimeZoneRule& that) const {
    if (this == &that) {
        return true;
    }
    if (typeid(*this) != typeid(that) || !TimeZoneRule::operator==(that)) {
        return false;
    }
    TimeArrayTimeZoneRule *tatzr = (TimeArrayTimeZoneRule*)&that;
    if (fTimeRuleType != tatzr->fTimeRuleType ||
        fNumStartTimes != tatzr->fNumStartTimes) {
        return false;
    }
    // Compare start times
    bool res = true;
    for (int32_t i = 0; i < fNumStartTimes; i++) {
        if (fStartTimes[i] != tatzr->fStartTimes[i]) {
            res = false;
            break;
        }
    }
    return res;
}

bool
TimeArrayTimeZoneRule::operator!=(const TimeZoneRule& that) const {
    return !operator==(that);
}

DateTimeRule::TimeRuleType
TimeArrayTimeZoneRule::getTimeType(void) const {
    return fTimeRuleType;
}

UBool
TimeArrayTimeZoneRule::getStartTimeAt(int32_t index, UDate& result) const {
    if (index >= fNumStartTimes || index < 0) {
        return FALSE;
    }
    result = fStartTimes[index];
    return TRUE;
}

int32_t
TimeArrayTimeZoneRule::countStartTimes(void) const {
    return fNumStartTimes;
}

UBool
TimeArrayTimeZoneRule::isEquivalentTo(const TimeZoneRule& other) const {
    if (this == &other) {
        return TRUE;
    }
    if (typeid(*this) != typeid(other) || TimeZoneRule::isEquivalentTo(other) == FALSE) {
        return FALSE;
    }
    TimeArrayTimeZoneRule* that = (TimeArrayTimeZoneRule*)&other;
    if (fTimeRuleType != that->fTimeRuleType ||
        fNumStartTimes != that->fNumStartTimes) {
        return FALSE;
    }
    // Compare start times
    UBool res = TRUE;
    for (int32_t i = 0; i < fNumStartTimes; i++) {
        if (fStartTimes[i] != that->fStartTimes[i]) {
            res = FALSE;
            break;
        }
    }
    return res;
}

UBool
TimeArrayTimeZoneRule::getFirstStart(int32_t prevRawOffset,
                                             int32_t prevDSTSavings,
                                             UDate& result) const {
    if (fNumStartTimes <= 0 || fStartTimes == NULL) {
        return FALSE;
    }
    result = getUTC(fStartTimes[0], prevRawOffset, prevDSTSavings);
    return TRUE;
}

UBool
TimeArrayTimeZoneRule::getFinalStart(int32_t prevRawOffset,
                                     int32_t prevDSTSavings,
                                     UDate& result) const {
    if (fNumStartTimes <= 0 || fStartTimes == NULL) {
        return FALSE;
    }
    result = getUTC(fStartTimes[fNumStartTimes - 1], prevRawOffset, prevDSTSavings);
    return TRUE;
}

UBool
TimeArrayTimeZoneRule::getNextStart(UDate base,
                                    int32_t prevRawOffset,
                                    int32_t prevDSTSavings,
                                    UBool inclusive,
                                    UDate& result) const {
    int32_t i = fNumStartTimes - 1;
    for (; i >= 0; i--) {
        UDate time = getUTC(fStartTimes[i], prevRawOffset, prevDSTSavings);
        if (time < base || (!inclusive && time == base)) {
            break;
        }
        result = time;
    }
    if (i == fNumStartTimes - 1) {
        return FALSE;
    }
    return TRUE;
}

UBool
TimeArrayTimeZoneRule::getPreviousStart(UDate base,
                                        int32_t prevRawOffset,
                                        int32_t prevDSTSavings,
                                        UBool inclusive,
                                        UDate& result) const {
    int32_t i = fNumStartTimes - 1;
    for (; i >= 0; i--) {
        UDate time = getUTC(fStartTimes[i], prevRawOffset, prevDSTSavings);
        if (time < base || (inclusive && time == base)) {
            result = time;
            return TRUE;
        }
    }
    return FALSE;
}


// ---- private methods ------

UBool
TimeArrayTimeZoneRule::initStartTimes(const UDate source[], int32_t size, UErrorCode& status) {
    // Free old array
    if (fStartTimes != NULL && fStartTimes != fLocalStartTimes) {
        uprv_free(fStartTimes);
    }
    // Allocate new one if needed
    if (size > TIMEARRAY_STACK_BUFFER_SIZE) {
        fStartTimes = (UDate*)uprv_malloc(sizeof(UDate)*size);
        if (fStartTimes == NULL) {
            status = U_MEMORY_ALLOCATION_ERROR;
            fNumStartTimes = 0;
            return FALSE;
        }
    } else {
        fStartTimes = (UDate*)fLocalStartTimes;
    }
    uprv_memcpy(fStartTimes, source, sizeof(UDate)*size);
    fNumStartTimes = size;
    // Sort dates
    uprv_sortArray(fStartTimes, fNumStartTimes, (int32_t)sizeof(UDate), compareDates, NULL, TRUE, &status);
    if (U_FAILURE(status)) {
        if (fStartTimes != NULL && fStartTimes != fLocalStartTimes) {
            uprv_free(fStartTimes);
        }
        fNumStartTimes = 0;
        return FALSE;
    }
    return TRUE;
}

UDate
TimeArrayTimeZoneRule::getUTC(UDate time, int32_t raw, int32_t dst) const {
    if (fTimeRuleType != DateTimeRule::UTC_TIME) {
        time -= raw;
    }
    if (fTimeRuleType == DateTimeRule::WALL_TIME) {
        time -= dst;
    }
    return time;
}

U_NAMESPACE_END

#endif /* #if !UCONFIG_NO_FORMATTING */

//eof