2017-01-20 00:20:31 +00:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
2016-06-15 18:58:17 +00:00
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
2007-06-29 16:27:12 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (C) 2007-2013, International Business Machines Corporation and
|
|
|
|
* others. All Rights Reserved.
|
2007-06-29 16:27:12 +00:00
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
2012-06-28 23:28:00 +00:00
|
|
|
#include "utypeinfo.h" // for 'typeid' to work
|
2010-05-19 17:29:33 +00:00
|
|
|
|
2007-06-29 16:27:12 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
|
|
|
#include "unicode/rbtz.h"
|
|
|
|
#include "unicode/gregocal.h"
|
|
|
|
#include "uvector.h"
|
|
|
|
#include "gregoimp.h"
|
|
|
|
#include "cmemory.h"
|
2013-02-01 07:39:04 +00:00
|
|
|
#include "umutex.h"
|
2007-06-29 16:27:12 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A struct representing a time zone transition
|
|
|
|
*/
|
2022-09-08 21:15:13 +00:00
|
|
|
struct Transition : public UMemory {
|
2007-06-29 16:27:12 +00:00
|
|
|
UDate time;
|
|
|
|
TimeZoneRule* from;
|
|
|
|
TimeZoneRule* to;
|
|
|
|
};
|
|
|
|
|
2022-09-08 21:15:13 +00:00
|
|
|
U_CDECL_BEGIN
|
|
|
|
static void U_CALLCONV
|
|
|
|
deleteTransition(void* obj) {
|
|
|
|
delete static_cast<Transition *>(obj);
|
|
|
|
}
|
|
|
|
U_CDECL_END
|
|
|
|
|
2007-06-29 16:27:12 +00:00
|
|
|
static UBool compareRules(UVector* rules1, UVector* rules2) {
|
|
|
|
if (rules1 == NULL && rules2 == NULL) {
|
2022-09-08 21:15:13 +00:00
|
|
|
return true;
|
2007-06-29 16:27:12 +00:00
|
|
|
} else if (rules1 == NULL || rules2 == NULL) {
|
2022-09-08 21:15:13 +00:00
|
|
|
return false;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
int32_t size = rules1->size();
|
|
|
|
if (size != rules2->size()) {
|
2022-09-08 21:15:13 +00:00
|
|
|
return false;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
for (int32_t i = 0; i < size; i++) {
|
|
|
|
TimeZoneRule *r1 = (TimeZoneRule*)rules1->elementAt(i);
|
|
|
|
TimeZoneRule *r2 = (TimeZoneRule*)rules2->elementAt(i);
|
2007-08-08 00:57:05 +00:00
|
|
|
if (*r1 != *r2) {
|
2022-09-08 21:15:13 +00:00
|
|
|
return false;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
return true;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(RuleBasedTimeZone)
|
|
|
|
|
|
|
|
RuleBasedTimeZone::RuleBasedTimeZone(const UnicodeString& id, InitialTimeZoneRule* initialRule)
|
|
|
|
: BasicTimeZone(id), fInitialRule(initialRule), fHistoricRules(NULL), fFinalRules(NULL),
|
2022-09-08 21:15:13 +00:00
|
|
|
fHistoricTransitions(NULL), fUpToDate(false) {
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RuleBasedTimeZone::RuleBasedTimeZone(const RuleBasedTimeZone& source)
|
|
|
|
: BasicTimeZone(source), fInitialRule(source.fInitialRule->clone()),
|
2022-09-08 21:15:13 +00:00
|
|
|
fHistoricTransitions(NULL), fUpToDate(false) {
|
2007-08-08 00:57:05 +00:00
|
|
|
fHistoricRules = copyRules(source.fHistoricRules);
|
|
|
|
fFinalRules = copyRules(source.fFinalRules);
|
|
|
|
if (source.fUpToDate) {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
complete(status);
|
|
|
|
}
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RuleBasedTimeZone::~RuleBasedTimeZone() {
|
|
|
|
deleteTransitions();
|
|
|
|
deleteRules();
|
|
|
|
}
|
|
|
|
|
|
|
|
RuleBasedTimeZone&
|
|
|
|
RuleBasedTimeZone::operator=(const RuleBasedTimeZone& right) {
|
|
|
|
if (*this != right) {
|
|
|
|
BasicTimeZone::operator=(right);
|
|
|
|
deleteRules();
|
|
|
|
fInitialRule = right.fInitialRule->clone();
|
|
|
|
fHistoricRules = copyRules(right.fHistoricRules);
|
|
|
|
fFinalRules = copyRules(right.fFinalRules);
|
|
|
|
deleteTransitions();
|
2022-09-08 21:15:13 +00:00
|
|
|
fUpToDate = false;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-09-08 21:15:13 +00:00
|
|
|
bool
|
2007-07-02 14:56:08 +00:00
|
|
|
RuleBasedTimeZone::operator==(const TimeZone& that) const {
|
2007-06-29 16:27:12 +00:00
|
|
|
if (this == &that) {
|
2022-09-08 21:15:13 +00:00
|
|
|
return true;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
if (typeid(*this) != typeid(that) || !BasicTimeZone::operator==(that)) {
|
|
|
|
return false;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
2007-07-02 14:56:08 +00:00
|
|
|
RuleBasedTimeZone *rbtz = (RuleBasedTimeZone*)&that;
|
|
|
|
if (*fInitialRule != *(rbtz->fInitialRule)) {
|
2022-09-08 21:15:13 +00:00
|
|
|
return false;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
2007-07-02 14:56:08 +00:00
|
|
|
if (compareRules(fHistoricRules, rbtz->fHistoricRules)
|
|
|
|
&& compareRules(fFinalRules, rbtz->fFinalRules)) {
|
2022-09-08 21:15:13 +00:00
|
|
|
return true;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
return false;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 21:15:13 +00:00
|
|
|
bool
|
2007-07-02 14:56:08 +00:00
|
|
|
RuleBasedTimeZone::operator!=(const TimeZone& that) const {
|
2007-06-29 16:27:12 +00:00
|
|
|
return !operator==(that);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RuleBasedTimeZone::addTransitionRule(TimeZoneRule* rule, UErrorCode& status) {
|
2022-09-08 21:15:13 +00:00
|
|
|
LocalPointer<TimeZoneRule>lpRule(rule);
|
2007-06-29 16:27:12 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2010-05-19 17:29:33 +00:00
|
|
|
AnnualTimeZoneRule* atzrule = dynamic_cast<AnnualTimeZoneRule*>(rule);
|
2022-09-08 21:15:13 +00:00
|
|
|
if (atzrule != nullptr && atzrule->getEndYear() == AnnualTimeZoneRule::MAX_YEAR) {
|
2007-06-29 16:27:12 +00:00
|
|
|
// A final rule
|
2022-09-08 21:15:13 +00:00
|
|
|
if (fFinalRules == nullptr) {
|
|
|
|
LocalPointer<UVector> lpFinalRules(new UVector(uprv_deleteUObject, nullptr, status), status);
|
2007-06-29 16:27:12 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
fFinalRules = lpFinalRules.orphan();
|
2007-06-29 16:27:12 +00:00
|
|
|
} else if (fFinalRules->size() >= 2) {
|
|
|
|
// Cannot handle more than two final rules
|
|
|
|
status = U_INVALID_STATE_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
fFinalRules->adoptElement(lpRule.orphan(), status);
|
2007-06-29 16:27:12 +00:00
|
|
|
} else {
|
|
|
|
// Non-final rule
|
2022-09-08 21:15:13 +00:00
|
|
|
if (fHistoricRules == nullptr) {
|
|
|
|
LocalPointer<UVector> lpHistoricRules(new UVector(uprv_deleteUObject, nullptr, status), status);
|
2007-06-29 16:27:12 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
fHistoricRules = lpHistoricRules.orphan();
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
fHistoricRules->adoptElement(lpRule.orphan(), status);
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
// Mark dirty, so transitions are recalculated at next complete() call
|
2022-09-08 21:15:13 +00:00
|
|
|
fUpToDate = false;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
|
2013-02-01 07:39:04 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
RuleBasedTimeZone::completeConst(UErrorCode& status) const {
|
2019-05-10 23:23:21 +00:00
|
|
|
static UMutex gLock;
|
2013-02-01 07:39:04 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-05-10 23:23:21 +00:00
|
|
|
umtx_lock(&gLock);
|
2013-05-31 23:50:15 +00:00
|
|
|
if (!fUpToDate) {
|
|
|
|
RuleBasedTimeZone *ncThis = const_cast<RuleBasedTimeZone*>(this);
|
|
|
|
ncThis->complete(status);
|
2013-02-01 07:39:04 +00:00
|
|
|
}
|
2019-05-10 23:23:21 +00:00
|
|
|
umtx_unlock(&gLock);
|
2013-02-01 07:39:04 +00:00
|
|
|
}
|
|
|
|
|
2007-06-29 16:27:12 +00:00
|
|
|
void
|
|
|
|
RuleBasedTimeZone::complete(UErrorCode& status) {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (fUpToDate) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Make sure either no final rules or a pair of AnnualTimeZoneRules
|
|
|
|
// are available.
|
|
|
|
if (fFinalRules != NULL && fFinalRules->size() != 2) {
|
|
|
|
status = U_INVALID_STATE_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a TimezoneTransition and add to the list
|
|
|
|
if (fHistoricRules != NULL || fFinalRules != NULL) {
|
|
|
|
TimeZoneRule *curRule = fInitialRule;
|
|
|
|
UDate lastTransitionTime = MIN_MILLIS;
|
|
|
|
|
|
|
|
// Build the transition array which represents historical time zone
|
|
|
|
// transitions.
|
|
|
|
if (fHistoricRules != NULL && fHistoricRules->size() > 0) {
|
|
|
|
int32_t i;
|
|
|
|
int32_t historicCount = fHistoricRules->size();
|
2022-09-08 21:15:13 +00:00
|
|
|
LocalMemory<bool> done((bool *)uprv_malloc(sizeof(bool) * historicCount));
|
2007-06-29 16:27:12 +00:00
|
|
|
if (done == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
for (i = 0; i < historicCount; i++) {
|
2022-09-08 21:15:13 +00:00
|
|
|
done[i] = false;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
while (true) {
|
2007-06-29 16:27:12 +00:00
|
|
|
int32_t curStdOffset = curRule->getRawOffset();
|
|
|
|
int32_t curDstSavings = curRule->getDSTSavings();
|
|
|
|
UDate nextTransitionTime = MAX_MILLIS;
|
|
|
|
TimeZoneRule *nextRule = NULL;
|
|
|
|
TimeZoneRule *r = NULL;
|
|
|
|
UBool avail;
|
|
|
|
UDate tt;
|
|
|
|
UnicodeString curName, name;
|
|
|
|
curRule->getName(curName);
|
|
|
|
|
|
|
|
for (i = 0; i < historicCount; i++) {
|
|
|
|
if (done[i]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
r = (TimeZoneRule*)fHistoricRules->elementAt(i);
|
|
|
|
avail = r->getNextStart(lastTransitionTime, curStdOffset, curDstSavings, false, tt);
|
|
|
|
if (!avail) {
|
|
|
|
// No more transitions from this rule - skip this rule next time
|
2022-09-08 21:15:13 +00:00
|
|
|
done[i] = true;
|
2007-06-29 16:27:12 +00:00
|
|
|
} else {
|
|
|
|
r->getName(name);
|
|
|
|
if (*r == *curRule ||
|
|
|
|
(name == curName && r->getRawOffset() == curRule->getRawOffset()
|
|
|
|
&& r->getDSTSavings() == curRule->getDSTSavings())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (tt < nextTransitionTime) {
|
|
|
|
nextTransitionTime = tt;
|
|
|
|
nextRule = r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nextRule == NULL) {
|
|
|
|
// Check if all historic rules are done
|
2022-09-08 21:15:13 +00:00
|
|
|
UBool bDoneAll = true;
|
2007-06-29 16:27:12 +00:00
|
|
|
for (int32_t j = 0; j < historicCount; j++) {
|
|
|
|
if (!done[j]) {
|
2022-09-08 21:15:13 +00:00
|
|
|
bDoneAll = false;
|
2007-06-29 16:27:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bDoneAll) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fFinalRules != NULL) {
|
|
|
|
// Check if one of final rules has earlier transition date
|
|
|
|
for (i = 0; i < 2 /* fFinalRules->size() */; i++) {
|
|
|
|
TimeZoneRule *fr = (TimeZoneRule*)fFinalRules->elementAt(i);
|
|
|
|
if (*fr == *curRule) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
r = (TimeZoneRule*)fFinalRules->elementAt(i);
|
|
|
|
avail = r->getNextStart(lastTransitionTime, curStdOffset, curDstSavings, false, tt);
|
|
|
|
if (avail) {
|
|
|
|
if (tt < nextTransitionTime) {
|
|
|
|
nextTransitionTime = tt;
|
|
|
|
nextRule = r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nextRule == NULL) {
|
|
|
|
// Nothing more
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fHistoricTransitions == NULL) {
|
2022-09-08 21:15:13 +00:00
|
|
|
LocalPointer<UVector> lpHistoricTransitions(
|
|
|
|
new UVector(deleteTransition, nullptr, status), status);
|
2007-06-29 16:27:12 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
fHistoricTransitions = lpHistoricTransitions.orphan();
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
LocalPointer<Transition> trst(new Transition, status);
|
|
|
|
if (U_FAILURE(status)) {
|
2007-06-29 16:27:12 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
trst->time = nextTransitionTime;
|
|
|
|
trst->from = curRule;
|
|
|
|
trst->to = nextRule;
|
2022-09-08 21:15:13 +00:00
|
|
|
fHistoricTransitions->adoptElement(trst.orphan(), status);
|
2007-06-29 16:27:12 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
lastTransitionTime = nextTransitionTime;
|
|
|
|
curRule = nextRule;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fFinalRules != NULL) {
|
|
|
|
if (fHistoricTransitions == NULL) {
|
2022-09-08 21:15:13 +00:00
|
|
|
LocalPointer<UVector> lpHistoricTransitions(
|
|
|
|
new UVector(deleteTransition, nullptr, status), status);
|
2007-06-29 16:27:12 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
fHistoricTransitions = lpHistoricTransitions.orphan();
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
// Append the first transition for each
|
|
|
|
TimeZoneRule *rule0 = (TimeZoneRule*)fFinalRules->elementAt(0);
|
|
|
|
TimeZoneRule *rule1 = (TimeZoneRule*)fFinalRules->elementAt(1);
|
|
|
|
UDate tt0, tt1;
|
|
|
|
UBool avail0 = rule0->getNextStart(lastTransitionTime, curRule->getRawOffset(), curRule->getDSTSavings(), false, tt0);
|
|
|
|
UBool avail1 = rule1->getNextStart(lastTransitionTime, curRule->getRawOffset(), curRule->getDSTSavings(), false, tt1);
|
|
|
|
if (!avail0 || !avail1) {
|
|
|
|
// Should not happen, because both rules are permanent
|
|
|
|
status = U_INVALID_STATE_ERROR;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
LocalPointer<Transition> final0(new Transition, status);
|
|
|
|
LocalPointer<Transition> final1(new Transition, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
goto cleanup;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
if (tt0 < tt1) {
|
|
|
|
final0->time = tt0;
|
|
|
|
final0->from = curRule;
|
|
|
|
final0->to = rule0;
|
|
|
|
rule1->getNextStart(tt0, rule0->getRawOffset(), rule0->getDSTSavings(), false, final1->time);
|
|
|
|
final1->from = rule0;
|
|
|
|
final1->to = rule1;
|
|
|
|
} else {
|
|
|
|
final0->time = tt1;
|
|
|
|
final0->from = curRule;
|
|
|
|
final0->to = rule1;
|
|
|
|
rule0->getNextStart(tt1, rule1->getRawOffset(), rule1->getDSTSavings(), false, final1->time);
|
|
|
|
final1->from = rule1;
|
|
|
|
final1->to = rule0;
|
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
fHistoricTransitions->adoptElement(final0.orphan(), status);
|
|
|
|
fHistoricTransitions->adoptElement(final1.orphan(), status);
|
2007-06-29 16:27:12 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
fUpToDate = true;
|
2007-06-29 16:27:12 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
deleteTransitions();
|
2022-09-08 21:15:13 +00:00
|
|
|
fUpToDate = false;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 00:13:18 +00:00
|
|
|
RuleBasedTimeZone*
|
|
|
|
RuleBasedTimeZone::clone() const {
|
2007-06-29 16:27:12 +00:00
|
|
|
return new RuleBasedTimeZone(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
RuleBasedTimeZone::getOffset(uint8_t era, int32_t year, int32_t month, int32_t day,
|
|
|
|
uint8_t dayOfWeek, int32_t millis, UErrorCode& status) const {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (month < UCAL_JANUARY || month > UCAL_DECEMBER) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return getOffset(era, year, month, day, dayOfWeek, millis,
|
|
|
|
Grego::monthLength(year, month), status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
RuleBasedTimeZone::getOffset(uint8_t era, int32_t year, int32_t month, int32_t day,
|
2007-07-05 17:17:59 +00:00
|
|
|
uint8_t /*dayOfWeek*/, int32_t millis,
|
|
|
|
int32_t /*monthLength*/, UErrorCode& status) const {
|
2007-06-29 16:27:12 +00:00
|
|
|
// dayOfWeek and monthLength are unused
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (era == GregorianCalendar::BC) {
|
|
|
|
// Convert to extended year
|
|
|
|
year = 1 - year;
|
|
|
|
}
|
|
|
|
int32_t rawOffset, dstOffset;
|
|
|
|
UDate time = (UDate)Grego::fieldsToDay(year, month, day) * U_MILLIS_PER_DAY + millis;
|
2022-09-08 21:15:13 +00:00
|
|
|
getOffsetInternal(time, true, kDaylight, kStandard, rawOffset, dstOffset, status);
|
2007-06-29 16:27:12 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return (rawOffset + dstOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RuleBasedTimeZone::getOffset(UDate date, UBool local, int32_t& rawOffset,
|
|
|
|
int32_t& dstOffset, UErrorCode& status) const {
|
2007-11-28 20:49:58 +00:00
|
|
|
getOffsetInternal(date, local, kFormer, kLatter, rawOffset, dstOffset, status);
|
|
|
|
}
|
|
|
|
|
2022-09-08 21:15:13 +00:00
|
|
|
void RuleBasedTimeZone::getOffsetFromLocal(UDate date, UTimeZoneLocalOption nonExistingTimeOpt,
|
|
|
|
UTimeZoneLocalOption duplicatedTimeOpt,
|
|
|
|
int32_t& rawOffset, int32_t& dstOffset, UErrorCode& status) const {
|
|
|
|
getOffsetInternal(date, true, nonExistingTimeOpt, duplicatedTimeOpt, rawOffset, dstOffset, status);
|
2007-11-28 20:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The internal getOffset implementation
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
RuleBasedTimeZone::getOffsetInternal(UDate date, UBool local,
|
|
|
|
int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt,
|
|
|
|
int32_t& rawOffset, int32_t& dstOffset,
|
|
|
|
UErrorCode& status) const {
|
2007-06-29 16:27:12 +00:00
|
|
|
rawOffset = 0;
|
|
|
|
dstOffset = 0;
|
|
|
|
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!fUpToDate) {
|
|
|
|
// Transitions are not yet resolved. We cannot do it here
|
|
|
|
// because this method is const. Thus, do nothing and return
|
|
|
|
// error status.
|
|
|
|
status = U_INVALID_STATE_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const TimeZoneRule *rule = NULL;
|
|
|
|
if (fHistoricTransitions == NULL) {
|
|
|
|
rule = fInitialRule;
|
|
|
|
} else {
|
2007-11-28 20:49:58 +00:00
|
|
|
UDate tstart = getTransitionTime((Transition*)fHistoricTransitions->elementAt(0),
|
|
|
|
local, NonExistingTimeOpt, DuplicatedTimeOpt);
|
2007-06-29 16:27:12 +00:00
|
|
|
if (date < tstart) {
|
|
|
|
rule = fInitialRule;
|
|
|
|
} else {
|
|
|
|
int32_t idx = fHistoricTransitions->size() - 1;
|
2007-11-28 20:49:58 +00:00
|
|
|
UDate tend = getTransitionTime((Transition*)fHistoricTransitions->elementAt(idx),
|
|
|
|
local, NonExistingTimeOpt, DuplicatedTimeOpt);
|
2007-06-29 16:27:12 +00:00
|
|
|
if (date > tend) {
|
|
|
|
if (fFinalRules != NULL) {
|
2007-11-28 20:49:58 +00:00
|
|
|
rule = findRuleInFinal(date, local, NonExistingTimeOpt, DuplicatedTimeOpt);
|
2011-12-02 07:02:54 +00:00
|
|
|
}
|
|
|
|
if (rule == NULL) {
|
|
|
|
// no final rules or the given time is before the first transition
|
|
|
|
// specified by the final rules -> use the last rule
|
2007-06-29 16:27:12 +00:00
|
|
|
rule = ((Transition*)fHistoricTransitions->elementAt(idx))->to;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Find a historical transition
|
|
|
|
while (idx >= 0) {
|
2007-11-28 20:49:58 +00:00
|
|
|
if (date >= getTransitionTime((Transition*)fHistoricTransitions->elementAt(idx),
|
|
|
|
local, NonExistingTimeOpt, DuplicatedTimeOpt)) {
|
2007-06-29 16:27:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
idx--;
|
|
|
|
}
|
|
|
|
rule = ((Transition*)fHistoricTransitions->elementAt(idx))->to;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rule != NULL) {
|
|
|
|
rawOffset = rule->getRawOffset();
|
|
|
|
dstOffset = rule->getDSTSavings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-07-05 17:17:59 +00:00
|
|
|
RuleBasedTimeZone::setRawOffset(int32_t /*offsetMillis*/) {
|
2007-06-29 16:27:12 +00:00
|
|
|
// We don't support this operation at this moment.
|
|
|
|
// Nothing to do!
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
RuleBasedTimeZone::getRawOffset(void) const {
|
|
|
|
// Note: This implementation returns standard GMT offset
|
|
|
|
// as of current time.
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
int32_t raw, dst;
|
2022-09-08 21:15:13 +00:00
|
|
|
getOffset(uprv_getUTCtime(), false, raw, dst, status);
|
2007-06-29 16:27:12 +00:00
|
|
|
return raw;
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
|
|
|
RuleBasedTimeZone::useDaylightTime(void) const {
|
|
|
|
// Note: This implementation returns true when
|
|
|
|
// daylight saving time is used as of now or
|
|
|
|
// after the next transition.
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2022-09-08 21:15:13 +00:00
|
|
|
UDate now = uprv_getUTCtime();
|
2007-06-29 16:27:12 +00:00
|
|
|
int32_t raw, dst;
|
2022-09-08 21:15:13 +00:00
|
|
|
getOffset(now, false, raw, dst, status);
|
2007-06-29 16:27:12 +00:00
|
|
|
if (dst != 0) {
|
2022-09-08 21:15:13 +00:00
|
|
|
return true;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
// If DST is not used now, check if DST is used after the next transition
|
|
|
|
UDate time;
|
|
|
|
TimeZoneRule *from, *to;
|
2022-09-08 21:15:13 +00:00
|
|
|
UBool avail = findNext(now, false, time, from, to);
|
2007-06-29 16:27:12 +00:00
|
|
|
if (avail && to->getDSTSavings() != 0) {
|
2022-09-08 21:15:13 +00:00
|
|
|
return true;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
return false;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
|
|
|
RuleBasedTimeZone::inDaylightTime(UDate date, UErrorCode& status) const {
|
|
|
|
if (U_FAILURE(status)) {
|
2022-09-08 21:15:13 +00:00
|
|
|
return false;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
int32_t raw, dst;
|
2022-09-08 21:15:13 +00:00
|
|
|
getOffset(date, false, raw, dst, status);
|
2007-06-29 16:27:12 +00:00
|
|
|
if (dst != 0) {
|
2022-09-08 21:15:13 +00:00
|
|
|
return true;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
return false;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
|
|
|
RuleBasedTimeZone::hasSameRules(const TimeZone& other) const {
|
|
|
|
if (this == &other) {
|
2022-09-08 21:15:13 +00:00
|
|
|
return true;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
2010-05-19 17:29:33 +00:00
|
|
|
if (typeid(*this) != typeid(other)) {
|
2022-09-08 21:15:13 +00:00
|
|
|
return false;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
const RuleBasedTimeZone& that = (const RuleBasedTimeZone&)other;
|
|
|
|
if (*fInitialRule != *(that.fInitialRule)) {
|
2022-09-08 21:15:13 +00:00
|
|
|
return false;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
if (compareRules(fHistoricRules, that.fHistoricRules)
|
|
|
|
&& compareRules(fFinalRules, that.fFinalRules)) {
|
2022-09-08 21:15:13 +00:00
|
|
|
return true;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
return false;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
2013-02-01 07:39:04 +00:00
|
|
|
RuleBasedTimeZone::getNextTransition(UDate base, UBool inclusive, TimeZoneTransition& result) const {
|
2007-06-29 16:27:12 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2013-02-01 07:39:04 +00:00
|
|
|
completeConst(status);
|
2007-06-29 16:27:12 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2022-09-08 21:15:13 +00:00
|
|
|
return false;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
UDate transitionTime;
|
|
|
|
TimeZoneRule *fromRule, *toRule;
|
|
|
|
UBool found = findNext(base, inclusive, transitionTime, fromRule, toRule);
|
|
|
|
if (found) {
|
|
|
|
result.setTime(transitionTime);
|
|
|
|
result.setFrom((const TimeZoneRule&)*fromRule);
|
|
|
|
result.setTo((const TimeZoneRule&)*toRule);
|
2022-09-08 21:15:13 +00:00
|
|
|
return true;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
return false;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
2013-02-01 07:39:04 +00:00
|
|
|
RuleBasedTimeZone::getPreviousTransition(UDate base, UBool inclusive, TimeZoneTransition& result) const {
|
2007-06-29 16:27:12 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2013-02-01 07:39:04 +00:00
|
|
|
completeConst(status);
|
2007-06-29 16:27:12 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2022-09-08 21:15:13 +00:00
|
|
|
return false;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
UDate transitionTime;
|
|
|
|
TimeZoneRule *fromRule, *toRule;
|
|
|
|
UBool found = findPrev(base, inclusive, transitionTime, fromRule, toRule);
|
|
|
|
if (found) {
|
|
|
|
result.setTime(transitionTime);
|
|
|
|
result.setFrom((const TimeZoneRule&)*fromRule);
|
|
|
|
result.setTo((const TimeZoneRule&)*toRule);
|
2022-09-08 21:15:13 +00:00
|
|
|
return true;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
return false;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
2013-02-01 07:39:04 +00:00
|
|
|
RuleBasedTimeZone::countTransitionRules(UErrorCode& /*status*/) const {
|
2007-06-29 16:27:12 +00:00
|
|
|
int32_t count = 0;
|
|
|
|
if (fHistoricRules != NULL) {
|
|
|
|
count += fHistoricRules->size();
|
|
|
|
}
|
|
|
|
if (fFinalRules != NULL) {
|
|
|
|
count += fFinalRules->size();
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RuleBasedTimeZone::getTimeZoneRules(const InitialTimeZoneRule*& initial,
|
|
|
|
const TimeZoneRule* trsrules[],
|
|
|
|
int32_t& trscount,
|
2013-02-01 07:39:04 +00:00
|
|
|
UErrorCode& status) const {
|
2007-06-29 16:27:12 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Initial rule
|
|
|
|
initial = fInitialRule;
|
|
|
|
|
|
|
|
// Transition rules
|
|
|
|
int32_t cnt = 0;
|
|
|
|
int32_t idx;
|
|
|
|
if (fHistoricRules != NULL && cnt < trscount) {
|
|
|
|
int32_t historicCount = fHistoricRules->size();
|
|
|
|
idx = 0;
|
|
|
|
while (cnt < trscount && idx < historicCount) {
|
2007-08-08 00:57:05 +00:00
|
|
|
trsrules[cnt++] = (const TimeZoneRule*)fHistoricRules->elementAt(idx++);
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fFinalRules != NULL && cnt < trscount) {
|
|
|
|
int32_t finalCount = fFinalRules->size();
|
|
|
|
idx = 0;
|
|
|
|
while (cnt < trscount && idx < finalCount) {
|
2007-08-08 00:57:05 +00:00
|
|
|
trsrules[cnt++] = (const TimeZoneRule*)fFinalRules->elementAt(idx++);
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Set the result length
|
|
|
|
trscount = cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RuleBasedTimeZone::deleteRules(void) {
|
|
|
|
delete fInitialRule;
|
|
|
|
fInitialRule = NULL;
|
|
|
|
if (fHistoricRules != NULL) {
|
|
|
|
delete fHistoricRules;
|
|
|
|
fHistoricRules = NULL;
|
|
|
|
}
|
|
|
|
if (fFinalRules != NULL) {
|
|
|
|
delete fFinalRules;
|
|
|
|
fFinalRules = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RuleBasedTimeZone::deleteTransitions(void) {
|
|
|
|
if (fHistoricTransitions != NULL) {
|
|
|
|
delete fHistoricTransitions;
|
|
|
|
}
|
|
|
|
fHistoricTransitions = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
UVector*
|
|
|
|
RuleBasedTimeZone::copyRules(UVector* source) {
|
2022-09-08 21:15:13 +00:00
|
|
|
if (source == nullptr) {
|
|
|
|
return nullptr;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
int32_t size = source->size();
|
2022-09-08 21:15:13 +00:00
|
|
|
LocalPointer<UVector> rules(new UVector(uprv_deleteUObject, nullptr, size, ec), ec);
|
2007-06-29 16:27:12 +00:00
|
|
|
if (U_FAILURE(ec)) {
|
2022-09-08 21:15:13 +00:00
|
|
|
return nullptr;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
int32_t i;
|
|
|
|
for (i = 0; i < size; i++) {
|
2022-09-08 21:15:13 +00:00
|
|
|
LocalPointer<TimeZoneRule> rule(((TimeZoneRule*)source->elementAt(i))->clone(), ec);
|
|
|
|
rules->adoptElement(rule.orphan(), ec);
|
2007-06-29 16:27:12 +00:00
|
|
|
if (U_FAILURE(ec)) {
|
2022-09-08 21:15:13 +00:00
|
|
|
return nullptr;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
return rules.orphan();
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TimeZoneRule*
|
2007-11-28 20:49:58 +00:00
|
|
|
RuleBasedTimeZone::findRuleInFinal(UDate date, UBool local,
|
|
|
|
int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt) const {
|
2007-06-29 16:27:12 +00:00
|
|
|
if (fFinalRules == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
AnnualTimeZoneRule* fr0 = (AnnualTimeZoneRule*)fFinalRules->elementAt(0);
|
|
|
|
AnnualTimeZoneRule* fr1 = (AnnualTimeZoneRule*)fFinalRules->elementAt(1);
|
|
|
|
if (fr0 == NULL || fr1 == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
UDate start0, start1;
|
|
|
|
UDate base;
|
2007-11-28 20:49:58 +00:00
|
|
|
int32_t localDelta;
|
2007-06-29 16:27:12 +00:00
|
|
|
|
2007-11-28 20:49:58 +00:00
|
|
|
base = date;
|
|
|
|
if (local) {
|
|
|
|
localDelta = getLocalDelta(fr1->getRawOffset(), fr1->getDSTSavings(),
|
|
|
|
fr0->getRawOffset(), fr0->getDSTSavings(),
|
|
|
|
NonExistingTimeOpt, DuplicatedTimeOpt);
|
|
|
|
base -= localDelta;
|
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
UBool avail0 = fr0->getPreviousStart(base, fr1->getRawOffset(), fr1->getDSTSavings(), true, start0);
|
2007-06-29 16:27:12 +00:00
|
|
|
|
2007-11-28 20:49:58 +00:00
|
|
|
base = date;
|
|
|
|
if (local) {
|
|
|
|
localDelta = getLocalDelta(fr0->getRawOffset(), fr0->getDSTSavings(),
|
|
|
|
fr1->getRawOffset(), fr1->getDSTSavings(),
|
|
|
|
NonExistingTimeOpt, DuplicatedTimeOpt);
|
|
|
|
base -= localDelta;
|
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
UBool avail1 = fr1->getPreviousStart(base, fr0->getRawOffset(), fr0->getDSTSavings(), true, start1);
|
2007-06-29 16:27:12 +00:00
|
|
|
|
2011-12-02 07:02:54 +00:00
|
|
|
if (!avail0 || !avail1) {
|
|
|
|
if (avail0) {
|
|
|
|
return fr0;
|
|
|
|
} else if (avail1) {
|
|
|
|
return fr1;
|
|
|
|
}
|
|
|
|
// Both rules take effect after the given time
|
|
|
|
return NULL;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
2011-12-02 07:02:54 +00:00
|
|
|
|
|
|
|
return (start0 > start1) ? fr0 : fr1;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
|
|
|
RuleBasedTimeZone::findNext(UDate base, UBool inclusive, UDate& transitionTime,
|
|
|
|
TimeZoneRule*& fromRule, TimeZoneRule*& toRule) const {
|
|
|
|
if (fHistoricTransitions == NULL) {
|
2022-09-08 21:15:13 +00:00
|
|
|
return false;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
UBool isFinal = false;
|
|
|
|
UBool found = false;
|
2007-06-29 16:27:12 +00:00
|
|
|
Transition result;
|
|
|
|
Transition *tzt = (Transition*)fHistoricTransitions->elementAt(0);
|
2007-11-28 20:49:58 +00:00
|
|
|
UDate tt = tzt->time;
|
2007-06-29 16:27:12 +00:00
|
|
|
if (tt > base || (inclusive && tt == base)) {
|
|
|
|
result = *tzt;
|
2022-09-08 21:15:13 +00:00
|
|
|
found = true;
|
2007-06-29 16:27:12 +00:00
|
|
|
} else {
|
|
|
|
int32_t idx = fHistoricTransitions->size() - 1;
|
|
|
|
tzt = (Transition*)fHistoricTransitions->elementAt(idx);
|
2007-11-28 20:49:58 +00:00
|
|
|
tt = tzt->time;
|
2007-06-29 16:27:12 +00:00
|
|
|
if (inclusive && tt == base) {
|
|
|
|
result = *tzt;
|
2022-09-08 21:15:13 +00:00
|
|
|
found = true;
|
2007-06-29 16:27:12 +00:00
|
|
|
} else if (tt <= base) {
|
|
|
|
if (fFinalRules != NULL) {
|
|
|
|
// Find a transion time with finalRules
|
|
|
|
TimeZoneRule *r0 = (TimeZoneRule*)fFinalRules->elementAt(0);
|
|
|
|
TimeZoneRule *r1 = (TimeZoneRule*)fFinalRules->elementAt(1);
|
|
|
|
UDate start0, start1;
|
|
|
|
UBool avail0 = r0->getNextStart(base, r1->getRawOffset(), r1->getDSTSavings(), inclusive, start0);
|
|
|
|
UBool avail1 = r1->getNextStart(base, r0->getRawOffset(), r0->getDSTSavings(), inclusive, start1);
|
2022-09-08 21:15:13 +00:00
|
|
|
// avail0/avail1 should be always true
|
2007-07-05 17:17:59 +00:00
|
|
|
if (!avail0 && !avail1) {
|
2022-09-08 21:15:13 +00:00
|
|
|
return false;
|
2007-07-05 17:17:59 +00:00
|
|
|
}
|
|
|
|
if (!avail1 || start0 < start1) {
|
2007-06-29 16:27:12 +00:00
|
|
|
result.time = start0;
|
|
|
|
result.from = r1;
|
|
|
|
result.to = r0;
|
|
|
|
} else {
|
|
|
|
result.time = start1;
|
|
|
|
result.from = r0;
|
|
|
|
result.to = r1;
|
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
isFinal = true;
|
|
|
|
found = true;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Find a transition within the historic transitions
|
|
|
|
idx--;
|
|
|
|
Transition *prev = tzt;
|
|
|
|
while (idx > 0) {
|
|
|
|
tzt = (Transition*)fHistoricTransitions->elementAt(idx);
|
2007-11-28 20:49:58 +00:00
|
|
|
tt = tzt->time;
|
2007-06-29 16:27:12 +00:00
|
|
|
if (tt < base || (!inclusive && tt == base)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
idx--;
|
|
|
|
prev = tzt;
|
|
|
|
}
|
|
|
|
result.time = prev->time;
|
|
|
|
result.from = prev->from;
|
|
|
|
result.to = prev->to;
|
2022-09-08 21:15:13 +00:00
|
|
|
found = true;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found) {
|
|
|
|
// For now, this implementation ignore transitions with only zone name changes.
|
|
|
|
if (result.from->getRawOffset() == result.to->getRawOffset()
|
|
|
|
&& result.from->getDSTSavings() == result.to->getDSTSavings()) {
|
2007-08-08 00:57:05 +00:00
|
|
|
if (isFinal) {
|
2022-09-08 21:15:13 +00:00
|
|
|
return false;
|
2007-08-08 00:57:05 +00:00
|
|
|
} else {
|
|
|
|
// No offset changes. Try next one if not final
|
2022-09-08 21:15:13 +00:00
|
|
|
return findNext(result.time, false /* always exclusive */,
|
2007-08-08 00:57:05 +00:00
|
|
|
transitionTime, fromRule, toRule);
|
|
|
|
}
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
transitionTime = result.time;
|
|
|
|
fromRule = result.from;
|
|
|
|
toRule = result.to;
|
2022-09-08 21:15:13 +00:00
|
|
|
return true;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
return false;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
|
|
|
RuleBasedTimeZone::findPrev(UDate base, UBool inclusive, UDate& transitionTime,
|
|
|
|
TimeZoneRule*& fromRule, TimeZoneRule*& toRule) const {
|
|
|
|
if (fHistoricTransitions == NULL) {
|
2022-09-08 21:15:13 +00:00
|
|
|
return false;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
UBool found = false;
|
2007-06-29 16:27:12 +00:00
|
|
|
Transition result;
|
|
|
|
Transition *tzt = (Transition*)fHistoricTransitions->elementAt(0);
|
2007-11-28 20:49:58 +00:00
|
|
|
UDate tt = tzt->time;
|
2007-06-29 16:27:12 +00:00
|
|
|
if (inclusive && tt == base) {
|
|
|
|
result = *tzt;
|
2022-09-08 21:15:13 +00:00
|
|
|
found = true;
|
2007-06-29 16:27:12 +00:00
|
|
|
} else if (tt < base) {
|
|
|
|
int32_t idx = fHistoricTransitions->size() - 1;
|
|
|
|
tzt = (Transition*)fHistoricTransitions->elementAt(idx);
|
2007-11-28 20:49:58 +00:00
|
|
|
tt = tzt->time;
|
2007-06-29 16:27:12 +00:00
|
|
|
if (inclusive && tt == base) {
|
|
|
|
result = *tzt;
|
2022-09-08 21:15:13 +00:00
|
|
|
found = true;
|
2007-06-29 16:27:12 +00:00
|
|
|
} else if (tt < base) {
|
|
|
|
if (fFinalRules != NULL) {
|
|
|
|
// Find a transion time with finalRules
|
|
|
|
TimeZoneRule *r0 = (TimeZoneRule*)fFinalRules->elementAt(0);
|
|
|
|
TimeZoneRule *r1 = (TimeZoneRule*)fFinalRules->elementAt(1);
|
|
|
|
UDate start0, start1;
|
|
|
|
UBool avail0 = r0->getPreviousStart(base, r1->getRawOffset(), r1->getDSTSavings(), inclusive, start0);
|
|
|
|
UBool avail1 = r1->getPreviousStart(base, r0->getRawOffset(), r0->getDSTSavings(), inclusive, start1);
|
2022-09-08 21:15:13 +00:00
|
|
|
// avail0/avail1 should be always true
|
2007-07-05 17:17:59 +00:00
|
|
|
if (!avail0 && !avail1) {
|
2022-09-08 21:15:13 +00:00
|
|
|
return false;
|
2007-07-05 17:17:59 +00:00
|
|
|
}
|
|
|
|
if (!avail1 || start0 > start1) {
|
2007-06-29 16:27:12 +00:00
|
|
|
result.time = start0;
|
|
|
|
result.from = r1;
|
|
|
|
result.to = r0;
|
|
|
|
} else {
|
|
|
|
result.time = start1;
|
|
|
|
result.from = r0;
|
|
|
|
result.to = r1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = *tzt;
|
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
found = true;
|
2007-06-29 16:27:12 +00:00
|
|
|
} else {
|
|
|
|
// Find a transition within the historic transitions
|
|
|
|
idx--;
|
|
|
|
while (idx >= 0) {
|
|
|
|
tzt = (Transition*)fHistoricTransitions->elementAt(idx);
|
2007-11-28 20:49:58 +00:00
|
|
|
tt = tzt->time;
|
2007-06-29 16:27:12 +00:00
|
|
|
if (tt < base || (inclusive && tt == base)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
idx--;
|
|
|
|
}
|
|
|
|
result = *tzt;
|
2022-09-08 21:15:13 +00:00
|
|
|
found = true;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found) {
|
|
|
|
// For now, this implementation ignore transitions with only zone name changes.
|
|
|
|
if (result.from->getRawOffset() == result.to->getRawOffset()
|
|
|
|
&& result.from->getDSTSavings() == result.to->getDSTSavings()) {
|
|
|
|
// No offset changes. Try next one if not final
|
2022-09-08 21:15:13 +00:00
|
|
|
return findPrev(result.time, false /* always exclusive */,
|
2007-06-29 16:27:12 +00:00
|
|
|
transitionTime, fromRule, toRule);
|
|
|
|
}
|
|
|
|
transitionTime = result.time;
|
|
|
|
fromRule = result.from;
|
|
|
|
toRule = result.to;
|
2022-09-08 21:15:13 +00:00
|
|
|
return true;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
2022-09-08 21:15:13 +00:00
|
|
|
return false;
|
2007-06-29 16:27:12 +00:00
|
|
|
}
|
|
|
|
|
2007-11-28 20:49:58 +00:00
|
|
|
UDate
|
|
|
|
RuleBasedTimeZone::getTransitionTime(Transition* transition, UBool local,
|
|
|
|
int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt) const {
|
|
|
|
UDate time = transition->time;
|
|
|
|
if (local) {
|
|
|
|
time += getLocalDelta(transition->from->getRawOffset(), transition->from->getDSTSavings(),
|
|
|
|
transition->to->getRawOffset(), transition->to->getDSTSavings(),
|
|
|
|
NonExistingTimeOpt, DuplicatedTimeOpt);
|
|
|
|
}
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
RuleBasedTimeZone::getLocalDelta(int32_t rawBefore, int32_t dstBefore, int32_t rawAfter, int32_t dstAfter,
|
|
|
|
int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt) const {
|
|
|
|
int32_t delta = 0;
|
|
|
|
|
|
|
|
int32_t offsetBefore = rawBefore + dstBefore;
|
|
|
|
int32_t offsetAfter = rawAfter + dstAfter;
|
|
|
|
|
|
|
|
UBool dstToStd = (dstBefore != 0) && (dstAfter == 0);
|
|
|
|
UBool stdToDst = (dstBefore == 0) && (dstAfter != 0);
|
|
|
|
|
|
|
|
if (offsetAfter - offsetBefore >= 0) {
|
|
|
|
// Positive transition, which makes a non-existing local time range
|
|
|
|
if (((NonExistingTimeOpt & kStdDstMask) == kStandard && dstToStd)
|
|
|
|
|| ((NonExistingTimeOpt & kStdDstMask) == kDaylight && stdToDst)) {
|
|
|
|
delta = offsetBefore;
|
|
|
|
} else if (((NonExistingTimeOpt & kStdDstMask) == kStandard && stdToDst)
|
|
|
|
|| ((NonExistingTimeOpt & kStdDstMask) == kDaylight && dstToStd)) {
|
|
|
|
delta = offsetAfter;
|
|
|
|
} else if ((NonExistingTimeOpt & kFormerLatterMask) == kLatter) {
|
|
|
|
delta = offsetBefore;
|
|
|
|
} else {
|
|
|
|
// Interprets the time with rule before the transition,
|
|
|
|
// default for non-existing time range
|
|
|
|
delta = offsetAfter;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Negative transition, which makes a duplicated local time range
|
|
|
|
if (((DuplicatedTimeOpt & kStdDstMask) == kStandard && dstToStd)
|
|
|
|
|| ((DuplicatedTimeOpt & kStdDstMask) == kDaylight && stdToDst)) {
|
|
|
|
delta = offsetAfter;
|
|
|
|
} else if (((DuplicatedTimeOpt & kStdDstMask) == kStandard && stdToDst)
|
|
|
|
|| ((DuplicatedTimeOpt & kStdDstMask) == kDaylight && dstToStd)) {
|
|
|
|
delta = offsetBefore;
|
|
|
|
} else if ((DuplicatedTimeOpt & kFormerLatterMask) == kFormer) {
|
|
|
|
delta = offsetBefore;
|
|
|
|
} else {
|
|
|
|
// Interprets the time with rule after the transition,
|
|
|
|
// default for duplicated local time range
|
|
|
|
delta = offsetAfter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return delta;
|
|
|
|
}
|
|
|
|
|
2007-06-29 16:27:12 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|
|
|
|
|
|
|
|
//eof
|
|
|
|
|