summaryrefslogtreecommitdiff
path: root/absl/time/internal/cctz/src
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2018-06-29 14:00:35 -0700
committerGravatar Titus Winters <titus@google.com>2018-07-02 09:58:35 -0400
commit134496a31d8b324f762de3bee9a002658c984456 (patch)
treeeb3d81ec6bb667c8f360e6898411a275a19f46c5 /absl/time/internal/cctz/src
parentba8d6cf07766263723e86736f20a51c1c9c67b19 (diff)
Export of internal Abseil changes.
-- aa9e2bff92652605b8244677058be787c872f99c by Abseil Team <absl-team@google.com>: Import of CCTZ from GitHub. PiperOrigin-RevId: 202702969 -- d26c857c203589892a84bc44d789f2a15a60f234 by Abseil Team <absl-team@google.com>: Cleans up the FixedArray code (formatting, renames, etc) without changing the functionality PiperOrigin-RevId: 202538159 GitOrigin-RevId: aa9e2bff92652605b8244677058be787c872f99c Change-Id: I6561257232c6cc8e1cbf51d7e26bae5f8760551e
Diffstat (limited to 'absl/time/internal/cctz/src')
-rw-r--r--absl/time/internal/cctz/src/cctz_benchmark.cc14
-rw-r--r--absl/time/internal/cctz/src/time_zone_format.cc3
-rw-r--r--absl/time/internal/cctz/src/time_zone_format_test.cc25
-rw-r--r--absl/time/internal/cctz/src/time_zone_if.h8
-rw-r--r--absl/time/internal/cctz/src/time_zone_impl.cc9
-rw-r--r--absl/time/internal/cctz/src/time_zone_impl.h38
-rw-r--r--absl/time/internal/cctz/src/time_zone_info.cc74
-rw-r--r--absl/time/internal/cctz/src/time_zone_info.h8
-rw-r--r--absl/time/internal/cctz/src/time_zone_libc.cc16
-rw-r--r--absl/time/internal/cctz/src/time_zone_libc.h7
-rw-r--r--absl/time/internal/cctz/src/time_zone_lookup.cc33
-rw-r--r--absl/time/internal/cctz/src/time_zone_lookup_test.cc155
-rw-r--r--absl/time/internal/cctz/src/zone_info_source.cc1
13 files changed, 259 insertions, 132 deletions
diff --git a/absl/time/internal/cctz/src/cctz_benchmark.cc b/absl/time/internal/cctz/src/cctz_benchmark.cc
index f13cb4ee..c97df78c 100644
--- a/absl/time/internal/cctz/src/cctz_benchmark.cc
+++ b/absl/time/internal/cctz/src/cctz_benchmark.cc
@@ -754,23 +754,21 @@ void BM_Zone_LoadAllTimeZonesCached(benchmark::State& state) {
}
BENCHMARK(BM_Zone_LoadAllTimeZonesCached);
-void BM_Zone_TimeZoneImplGetImplicit(benchmark::State& state) {
+void BM_Zone_TimeZoneEqualityImplicit(benchmark::State& state) {
cctz::time_zone tz; // implicit UTC
- cctz::time_zone::Impl::get(tz);
while (state.KeepRunning()) {
- cctz::time_zone::Impl::get(tz);
+ benchmark::DoNotOptimize(tz == tz);
}
}
-BENCHMARK(BM_Zone_TimeZoneImplGetImplicit);
+BENCHMARK(BM_Zone_TimeZoneEqualityImplicit);
-void BM_Zone_TimeZoneImplGetExplicit(benchmark::State& state) {
+void BM_Zone_TimeZoneEqualityExplicit(benchmark::State& state) {
cctz::time_zone tz = cctz::utc_time_zone(); // explicit UTC
- cctz::time_zone::Impl::get(tz);
while (state.KeepRunning()) {
- cctz::time_zone::Impl::get(tz);
+ benchmark::DoNotOptimize(tz == tz);
}
}
-BENCHMARK(BM_Zone_TimeZoneImplGetExplicit);
+BENCHMARK(BM_Zone_TimeZoneEqualityExplicit);
void BM_Zone_UTCTimeZone(benchmark::State& state) {
cctz::time_zone tz;
diff --git a/absl/time/internal/cctz/src/time_zone_format.cc b/absl/time/internal/cctz/src/time_zone_format.cc
index 592ab7d3..1b023848 100644
--- a/absl/time/internal/cctz/src/time_zone_format.cc
+++ b/absl/time/internal/cctz/src/time_zone_format.cc
@@ -141,6 +141,9 @@ char* Format02d(char* ep, int v) {
// Formats a UTC offset, like +00:00.
char* FormatOffset(char* ep, int offset, const char* mode) {
+ // TODO: Follow the RFC3339 "Unknown Local Offset Convention" and
+ // generate a "negative zero" when we're formatting a zero offset
+ // as the result of a failed load_time_zone().
char sign = '+';
if (offset < 0) {
offset = -offset; // bounded by 24h so no overflow
diff --git a/absl/time/internal/cctz/src/time_zone_format_test.cc b/absl/time/internal/cctz/src/time_zone_format_test.cc
index 33c23984..a90dda76 100644
--- a/absl/time/internal/cctz/src/time_zone_format_test.cc
+++ b/absl/time/internal/cctz/src/time_zone_format_test.cc
@@ -64,6 +64,17 @@ void TestFormatSpecifier(time_point<D> tp, time_zone tz, const std::string& fmt,
EXPECT_EQ("xxx " + ans + " yyy", format("xxx " + fmt + " yyy", tp, tz));
}
+// These tests sometimes run on platforms that have zoneinfo data so old
+// that the transition we are attempting to check does not exist, most
+// notably Android emulators. Fortunately, AndroidZoneInfoSource supports
+// time_zone::version() so, in cases where we've learned that it matters,
+// we can make the check conditionally.
+int VersionCmp(time_zone tz, const std::string& target) {
+ std::string version = tz.version();
+ if (version.empty() && !target.empty()) return 1; // unknown > known
+ return version.compare(target);
+}
+
} // namespace
//
@@ -453,8 +464,8 @@ TEST(Format, ExtendedSecondOffset) {
EXPECT_TRUE(load_time_zone("America/New_York", &tz));
tp = convert(civil_second(1883, 11, 18, 16, 59, 59), utc);
if (tz.lookup(tp).offset == -5 * 60 * 60) {
- // We're likely dealing with zoneinfo that doesn't support really old
- // timestamps, so America/New_York never looks to be on local mean time.
+ // It looks like the tzdata is only 32 bit (probably macOS),
+ // which bottoms out at 1901-12-13T20:45:52+00:00.
} else {
TestFormatSpecifier(tp, tz, "%E*z", "-04:56:02");
TestFormatSpecifier(tp, tz, "%Ez", "-04:56");
@@ -464,12 +475,10 @@ TEST(Format, ExtendedSecondOffset) {
EXPECT_TRUE(load_time_zone("Europe/Moscow", &tz));
tp = convert(civil_second(1919, 6, 30, 23, 59, 59), utc);
-#if defined(__ANDROID__) && __ANDROID_API__ < 25
- // Only Android 'N'.1 and beyond have this tz2016g transition.
-#else
- TestFormatSpecifier(tp, tz, "%E*z", "+04:31:19");
- TestFormatSpecifier(tp, tz, "%Ez", "+04:31");
-#endif
+ if (VersionCmp(tz, "2016g") >= 0) {
+ TestFormatSpecifier(tp, tz, "%E*z", "+04:31:19");
+ TestFormatSpecifier(tp, tz, "%Ez", "+04:31");
+ }
tp += chrono::seconds(1);
TestFormatSpecifier(tp, tz, "%E*z", "+04:00:00");
}
diff --git a/absl/time/internal/cctz/src/time_zone_if.h b/absl/time/internal/cctz/src/time_zone_if.h
index f10972ae..e4bd3866 100644
--- a/absl/time/internal/cctz/src/time_zone_if.h
+++ b/absl/time/internal/cctz/src/time_zone_if.h
@@ -41,9 +41,13 @@ class TimeZoneIf {
virtual time_zone::civil_lookup MakeTime(
const civil_second& cs) const = 0;
+ virtual bool NextTransition(const time_point<seconds>& tp,
+ time_zone::civil_transition* trans) const = 0;
+ virtual bool PrevTransition(const time_point<seconds>& tp,
+ time_zone::civil_transition* trans) const = 0;
+
+ virtual std::string Version() const = 0;
virtual std::string Description() const = 0;
- virtual bool NextTransition(time_point<seconds>* tp) const = 0;
- virtual bool PrevTransition(time_point<seconds>* tp) const = 0;
protected:
TimeZoneIf() {}
diff --git a/absl/time/internal/cctz/src/time_zone_impl.cc b/absl/time/internal/cctz/src/time_zone_impl.cc
index eb96c7ef..3062ccd3 100644
--- a/absl/time/internal/cctz/src/time_zone_impl.cc
+++ b/absl/time/internal/cctz/src/time_zone_impl.cc
@@ -83,15 +83,6 @@ bool time_zone::Impl::LoadTimeZone(const std::string& name, time_zone* tz) {
return impl != utc_impl;
}
-const time_zone::Impl& time_zone::Impl::get(const time_zone& tz) {
- if (tz.impl_ == nullptr) {
- // Dereferencing an implicit-UTC time_zone is expected to be
- // rare, so we don't mind paying a small synchronization cost.
- return *UTCImpl();
- }
- return *tz.impl_;
-}
-
void time_zone::Impl::ClearTimeZoneMapTestOnly() {
std::lock_guard<std::mutex> lock(time_zone_mutex);
if (time_zone_map != nullptr) {
diff --git a/absl/time/internal/cctz/src/time_zone_impl.h b/absl/time/internal/cctz/src/time_zone_impl.h
index fef7f226..14965ef5 100644
--- a/absl/time/internal/cctz/src/time_zone_impl.h
+++ b/absl/time/internal/cctz/src/time_zone_impl.h
@@ -37,15 +37,15 @@ class time_zone::Impl {
// some other kind of error occurs. Note that loading "UTC" never fails.
static bool LoadTimeZone(const std::string& name, time_zone* tz);
- // Dereferences the time_zone to obtain its Impl.
- static const time_zone::Impl& get(const time_zone& tz);
-
// Clears the map of cached time zones. Primarily for use in benchmarks
// that gauge the performance of loading/parsing the time-zone data.
static void ClearTimeZoneMapTestOnly();
// The primary key is the time-zone ID (e.g., "America/New_York").
- const std::string& name() const { return name_; }
+ const std::string& Name() const {
+ // TODO: It would nice if the zoneinfo data included the zone name.
+ return name_;
+ }
// Breaks a time_point down to civil-time components in this time zone.
time_zone::absolute_lookup BreakTime(const time_point<seconds>& tp) const {
@@ -59,28 +59,22 @@ class time_zone::Impl {
return zone_->MakeTime(cs);
}
- // Returns an implementation-specific description of this time zone.
- std::string Description() const { return zone_->Description(); }
-
// Finds the time of the next/previous offset change in this time zone.
- //
- // By definition, NextTransition(&tp) returns false when tp has its
- // maximum value, and PrevTransition(&tp) returns false when tp has its
- // mimimum value. If the zone has no transitions, the result will also
- // be false no matter what the argument.
- //
- // Otherwise, when tp has its mimimum value, NextTransition(&tp) returns
- // true and sets tp to the first recorded transition. Chains of calls
- // to NextTransition()/PrevTransition() will eventually return false,
- // but it is unspecified exactly when NextTransition(&tp) jumps to false,
- // or what time is set by PrevTransition(&tp) for a very distant tp.
- bool NextTransition(time_point<seconds>* tp) const {
- return zone_->NextTransition(tp);
+ bool NextTransition(const time_point<seconds>& tp,
+ time_zone::civil_transition* trans) const {
+ return zone_->NextTransition(tp, trans);
}
- bool PrevTransition(time_point<seconds>* tp) const {
- return zone_->PrevTransition(tp);
+ bool PrevTransition(const time_point<seconds>& tp,
+ time_zone::civil_transition* trans) const {
+ return zone_->PrevTransition(tp, trans);
}
+ // Returns an implementation-defined version std::string for this time zone.
+ std::string Version() const { return zone_->Version(); }
+
+ // Returns an implementation-defined description of this time zone.
+ std::string Description() const { return zone_->Description(); }
+
private:
explicit Impl(const std::string& name);
static const Impl* UTCImpl();
diff --git a/absl/time/internal/cctz/src/time_zone_info.cc b/absl/time/internal/cctz/src/time_zone_info.cc
index cdd11810..bf73635d 100644
--- a/absl/time/internal/cctz/src/time_zone_info.cc
+++ b/absl/time/internal/cctz/src/time_zone_info.cc
@@ -186,14 +186,13 @@ bool TimeZoneInfo::ResetToBuiltinUTC(const seconds& offset) {
tt.is_dst = false;
tt.abbr_index = 0;
- // We temporarily add some redundant, contemporary (2012 through 2021)
+ // We temporarily add some redundant, contemporary (2013 through 2023)
// transitions for performance reasons. See TimeZoneInfo::LocalTime().
// TODO: Fix the performance issue and remove the extra transitions.
transitions_.clear();
transitions_.reserve(12);
for (const std::int_fast64_t unix_time : {
-(1LL << 59), // BIG_BANG
- 1325376000LL, // 2012-01-01T00:00:00+00:00
1356998400LL, // 2013-01-01T00:00:00+00:00
1388534400LL, // 2014-01-01T00:00:00+00:00
1420070400LL, // 2015-01-01T00:00:00+00:00
@@ -203,6 +202,8 @@ bool TimeZoneInfo::ResetToBuiltinUTC(const seconds& offset) {
1546300800LL, // 2019-01-01T00:00:00+00:00
1577836800LL, // 2020-01-01T00:00:00+00:00
1609459200LL, // 2021-01-01T00:00:00+00:00
+ 1640995200LL, // 2022-01-01T00:00:00+00:00
+ 1672531200LL, // 2023-01-01T00:00:00+00:00
2147483647LL, // 2^31 - 1
}) {
Transition& tr(*transitions_.emplace(transitions_.end()));
@@ -519,6 +520,13 @@ bool TimeZoneInfo::Load(const std::string& name, ZoneInfoSource* zip) {
// We don't check for EOF so that we're forwards compatible.
+ // If we did not find version information during the standard loading
+ // process (as of tzh_version '3' that is unsupported), then ask the
+ // ZoneInfoSource for any out-of-bound version std::string it may be privy to.
+ if (version_.empty()) {
+ version_ = zip->Version();
+ }
+
// Trim redundant transitions. zic may have added these to work around
// differences between the glibc and reference implementations (see
// zic.c:dontmerge) and the Qt library (see zic.c:WORK_AROUND_QTBUG_53071).
@@ -605,6 +613,10 @@ class FileZoneInfoSource : public ZoneInfoSource {
if (rc == 0) len_ -= offset;
return rc;
}
+ std::string Version() const override {
+ // TODO: It would nice if the zoneinfo data included the tzdb version.
+ return std::string();
+ }
protected:
explicit FileZoneInfoSource(
@@ -654,14 +666,15 @@ std::unique_ptr<ZoneInfoSource> FileZoneInfoSource::Open(
return std::unique_ptr<ZoneInfoSource>(new FileZoneInfoSource(fp, length));
}
-#if defined(__ANDROID__)
class AndroidZoneInfoSource : public FileZoneInfoSource {
public:
static std::unique_ptr<ZoneInfoSource> Open(const std::string& name);
+ std::string Version() const override { return version_; }
private:
- explicit AndroidZoneInfoSource(FILE* fp, std::size_t len)
- : FileZoneInfoSource(fp, len) {}
+ explicit AndroidZoneInfoSource(FILE* fp, std::size_t len, const char* vers)
+ : FileZoneInfoSource(fp, len), version_(vers) {}
+ std::string version_;
};
std::unique_ptr<ZoneInfoSource> AndroidZoneInfoSource::Open(
@@ -669,6 +682,7 @@ std::unique_ptr<ZoneInfoSource> AndroidZoneInfoSource::Open(
// Use of the "file:" prefix is intended for testing purposes only.
if (name.compare(0, 5, "file:") == 0) return Open(name.substr(5));
+#if defined(__ANDROID__)
// See Android's libc/tzcode/bionic.cpp for additional information.
for (const char* tzdata : {"/data/misc/zoneinfo/current/tzdata",
"/system/usr/share/zoneinfo/tzdata"}) {
@@ -678,6 +692,7 @@ std::unique_ptr<ZoneInfoSource> AndroidZoneInfoSource::Open(
char hbuf[24]; // covers header.zonetab_offset too
if (fread(hbuf, 1, sizeof(hbuf), fp.get()) != sizeof(hbuf)) continue;
if (strncmp(hbuf, "tzdata", 6) != 0) continue;
+ const char* vers = (hbuf[11] == '\0') ? hbuf + 6 : "";
const std::int_fast32_t index_offset = Decode32(hbuf + 12);
const std::int_fast32_t data_offset = Decode32(hbuf + 16);
if (index_offset < 0 || data_offset < index_offset) continue;
@@ -698,13 +713,13 @@ std::unique_ptr<ZoneInfoSource> AndroidZoneInfoSource::Open(
if (strcmp(name.c_str(), ebuf) == 0) {
if (fseek(fp.get(), static_cast<long>(start), SEEK_SET) != 0) break;
return std::unique_ptr<ZoneInfoSource>(new AndroidZoneInfoSource(
- fp.release(), static_cast<std::size_t>(length)));
+ fp.release(), static_cast<std::size_t>(length), vers));
}
}
}
+#endif // __ANDROID__
return nullptr;
}
-#endif
} // namespace
@@ -722,9 +737,7 @@ bool TimeZoneInfo::Load(const std::string& name) {
auto zip = cctz_extension::zone_info_source_factory(
name, [](const std::string& name) -> std::unique_ptr<ZoneInfoSource> {
if (auto zip = FileZoneInfoSource::Open(name)) return zip;
-#if defined(__ANDROID__)
if (auto zip = AndroidZoneInfoSource::Open(name)) return zip;
-#endif
return nullptr;
});
return zip != nullptr && Load(name, zip.get());
@@ -885,17 +898,20 @@ time_zone::civil_lookup TimeZoneInfo::MakeTime(const civil_second& cs) const {
return MakeUnique(tr->unix_time + (cs - tr->civil_sec));
}
+std::string TimeZoneInfo::Version() const {
+ return version_;
+}
+
std::string TimeZoneInfo::Description() const {
std::ostringstream oss;
- // TODO: It would nice if the zoneinfo data included the zone name.
- // TODO: It would nice if the zoneinfo data included the tzdb version.
oss << "#trans=" << transitions_.size();
oss << " #types=" << transition_types_.size();
oss << " spec='" << future_spec_ << "'";
return oss.str();
}
-bool TimeZoneInfo::NextTransition(time_point<seconds>* tp) const {
+bool TimeZoneInfo::NextTransition(const time_point<seconds>& tp,
+ time_zone::civil_transition* trans) const {
if (transitions_.empty()) return false;
const Transition* begin = &transitions_[0];
const Transition* end = begin + transitions_.size();
@@ -904,22 +920,24 @@ bool TimeZoneInfo::NextTransition(time_point<seconds>* tp) const {
// really a sentinel, not a transition. See tz/zic.c.
++begin;
}
- std::int_fast64_t unix_time = ToUnixSeconds(*tp);
+ std::int_fast64_t unix_time = ToUnixSeconds(tp);
const Transition target = { unix_time };
const Transition* tr = std::upper_bound(begin, end, target,
Transition::ByUnixTime());
- if (tr != begin) { // skip no-op transitions
- for (; tr != end; ++tr) {
- if (!EquivTransitions(tr[-1].type_index, tr[0].type_index)) break;
- }
+ for (; tr != end; ++tr) { // skip no-op transitions
+ std::uint_fast8_t prev_type_index =
+ (tr == begin) ? default_transition_type_ : tr[-1].type_index;
+ if (!EquivTransitions(prev_type_index, tr[0].type_index)) break;
}
// When tr == end we return false, ignoring future_spec_.
if (tr == end) return false;
- *tp = FromUnixSeconds(tr->unix_time);
+ trans->from = tr->prev_civil_sec + 1;
+ trans->to = tr->civil_sec;
return true;
}
-bool TimeZoneInfo::PrevTransition(time_point<seconds>* tp) const {
+bool TimeZoneInfo::PrevTransition(const time_point<seconds>& tp,
+ time_zone::civil_transition* trans) const {
if (transitions_.empty()) return false;
const Transition* begin = &transitions_[0];
const Transition* end = begin + transitions_.size();
@@ -928,11 +946,12 @@ bool TimeZoneInfo::PrevTransition(time_point<seconds>* tp) const {
// really a sentinel, not a transition. See tz/zic.c.
++begin;
}
- std::int_fast64_t unix_time = ToUnixSeconds(*tp);
- if (FromUnixSeconds(unix_time) != *tp) {
+ std::int_fast64_t unix_time = ToUnixSeconds(tp);
+ if (FromUnixSeconds(unix_time) != tp) {
if (unix_time == std::numeric_limits<std::int_fast64_t>::max()) {
if (end == begin) return false; // Ignore future_spec_.
- *tp = FromUnixSeconds((--end)->unix_time);
+ trans->from = (--end)->prev_civil_sec + 1;
+ trans->to = end->civil_sec;
return true;
}
unix_time += 1; // ceils
@@ -940,14 +959,15 @@ bool TimeZoneInfo::PrevTransition(time_point<seconds>* tp) const {
const Transition target = { unix_time };
const Transition* tr = std::lower_bound(begin, end, target,
Transition::ByUnixTime());
- if (tr != begin) { // skip no-op transitions
- for (; tr - 1 != begin; --tr) {
- if (!EquivTransitions(tr[-2].type_index, tr[-1].type_index)) break;
- }
+ for (; tr != begin; --tr) { // skip no-op transitions
+ std::uint_fast8_t prev_type_index =
+ (tr - 1 == begin) ? default_transition_type_ : tr[-2].type_index;
+ if (!EquivTransitions(prev_type_index, tr[-1].type_index)) break;
}
// When tr == end we return the "last" transition, ignoring future_spec_.
if (tr == begin) return false;
- *tp = FromUnixSeconds((--tr)->unix_time);
+ trans->from = (--tr)->prev_civil_sec + 1;
+ trans->to = tr->civil_sec;
return true;
}
diff --git a/absl/time/internal/cctz/src/time_zone_info.h b/absl/time/internal/cctz/src/time_zone_info.h
index d28443e2..958e9b6b 100644
--- a/absl/time/internal/cctz/src/time_zone_info.h
+++ b/absl/time/internal/cctz/src/time_zone_info.h
@@ -74,9 +74,12 @@ class TimeZoneInfo : public TimeZoneIf {
const time_point<seconds>& tp) const override;
time_zone::civil_lookup MakeTime(
const civil_second& cs) const override;
+ bool NextTransition(const time_point<seconds>& tp,
+ time_zone::civil_transition* trans) const override;
+ bool PrevTransition(const time_point<seconds>& tp,
+ time_zone::civil_transition* trans) const override;
+ std::string Version() const override;
std::string Description() const override;
- bool NextTransition(time_point<seconds>* tp) const override;
- bool PrevTransition(time_point<seconds>* tp) const override;
private:
struct Header { // counts of:
@@ -114,6 +117,7 @@ class TimeZoneInfo : public TimeZoneIf {
std::uint_fast8_t default_transition_type_; // for before first transition
std::string abbreviations_; // all the NUL-terminated abbreviations
+ std::string version_; // the tzdata version if available
std::string future_spec_; // for after the last zic transition
bool extended_; // future_spec_ was used to generate transitions
year_t last_year_; // the final year of the generated transitions
diff --git a/absl/time/internal/cctz/src/time_zone_libc.cc b/absl/time/internal/cctz/src/time_zone_libc.cc
index 1d727bde..074c8d0a 100644
--- a/absl/time/internal/cctz/src/time_zone_libc.cc
+++ b/absl/time/internal/cctz/src/time_zone_libc.cc
@@ -139,16 +139,22 @@ time_zone::civil_lookup TimeZoneLibC::MakeTime(const civil_second& cs) const {
return cl;
}
-std::string TimeZoneLibC::Description() const {
- return local_ ? "localtime" : "UTC";
+bool TimeZoneLibC::NextTransition(const time_point<seconds>& tp,
+ time_zone::civil_transition* trans) const {
+ return false;
}
-bool TimeZoneLibC::NextTransition(time_point<seconds>* tp) const {
+bool TimeZoneLibC::PrevTransition(const time_point<seconds>& tp,
+ time_zone::civil_transition* trans) const {
return false;
}
-bool TimeZoneLibC::PrevTransition(time_point<seconds>* tp) const {
- return false;
+std::string TimeZoneLibC::Version() const {
+ return std::string(); // unknown
+}
+
+std::string TimeZoneLibC::Description() const {
+ return local_ ? "localtime" : "UTC";
}
} // namespace cctz
diff --git a/absl/time/internal/cctz/src/time_zone_libc.h b/absl/time/internal/cctz/src/time_zone_libc.h
index 4c64cd34..4e40c61a 100644
--- a/absl/time/internal/cctz/src/time_zone_libc.h
+++ b/absl/time/internal/cctz/src/time_zone_libc.h
@@ -35,9 +35,12 @@ class TimeZoneLibC : public TimeZoneIf {
const time_point<seconds>& tp) const override;
time_zone::civil_lookup MakeTime(
const civil_second& cs) const override;
+ bool NextTransition(const time_point<seconds>& tp,
+ time_zone::civil_transition* trans) const override;
+ bool PrevTransition(const time_point<seconds>& tp,
+ time_zone::civil_transition* trans) const override;
+ std::string Version() const override;
std::string Description() const override;
- bool NextTransition(time_point<seconds>* tp) const override;
- bool PrevTransition(time_point<seconds>* tp) const override;
private:
const bool local_; // localtime or UTC
diff --git a/absl/time/internal/cctz/src/time_zone_lookup.cc b/absl/time/internal/cctz/src/time_zone_lookup.cc
index 2f6cd98b..f2d151e4 100644
--- a/absl/time/internal/cctz/src/time_zone_lookup.cc
+++ b/absl/time/internal/cctz/src/time_zone_lookup.cc
@@ -61,20 +61,43 @@ int __system_property_get(const char* name, char* value) {
#endif
std::string time_zone::name() const {
- return time_zone::Impl::get(*this).name();
+ return effective_impl().Name();
}
time_zone::absolute_lookup time_zone::lookup(
const time_point<seconds>& tp) const {
- return time_zone::Impl::get(*this).BreakTime(tp);
+ return effective_impl().BreakTime(tp);
}
time_zone::civil_lookup time_zone::lookup(const civil_second& cs) const {
- return time_zone::Impl::get(*this).MakeTime(cs);
+ return effective_impl().MakeTime(cs);
}
-bool operator==(time_zone lhs, time_zone rhs) {
- return &time_zone::Impl::get(lhs) == &time_zone::Impl::get(rhs);
+bool time_zone::next_transition(const time_point<seconds>& tp,
+ civil_transition* trans) const {
+ return effective_impl().NextTransition(tp, trans);
+}
+
+bool time_zone::prev_transition(const time_point<seconds>& tp,
+ civil_transition* trans) const {
+ return effective_impl().PrevTransition(tp, trans);
+}
+
+std::string time_zone::version() const {
+ return effective_impl().Version();
+}
+
+std::string time_zone::description() const {
+ return effective_impl().Description();
+}
+
+const time_zone::Impl& time_zone::effective_impl() const {
+ if (impl_ == nullptr) {
+ // Dereferencing an implicit-UTC time_zone is expected to be
+ // rare, so we don't mind paying a small synchronization cost.
+ return *time_zone::Impl::UTC().impl_;
+ }
+ return *impl_;
}
bool load_time_zone(const std::string& name, time_zone* tz) {
diff --git a/absl/time/internal/cctz/src/time_zone_lookup_test.cc b/absl/time/internal/cctz/src/time_zone_lookup_test.cc
index cd9fc236..551292fb 100644
--- a/absl/time/internal/cctz/src/time_zone_lookup_test.cc
+++ b/absl/time/internal/cctz/src/time_zone_lookup_test.cc
@@ -651,6 +651,17 @@ time_zone LoadZone(const std::string& name) {
/* EXPECT_STREQ(zone, al.abbr); */ \
} while (0)
+// These tests sometimes run on platforms that have zoneinfo data so old
+// that the transition we are attempting to check does not exist, most
+// notably Android emulators. Fortunately, AndroidZoneInfoSource supports
+// time_zone::version() so, in cases where we've learned that it matters,
+// we can make the check conditionally.
+int VersionCmp(time_zone tz, const std::string& target) {
+ std::string version = tz.version();
+ if (version.empty() && !target.empty()) return 1; // unknown > known
+ return version.compare(target);
+}
+
} // namespace
TEST(TimeZones, LoadZonesConcurrently) {
@@ -981,6 +992,69 @@ TEST(MakeTime, SysSecondsLimits) {
EXPECT_EQ(time_point<absl::time_internal::cctz::seconds>::min(), tp);
}
+TEST(NextTransition, UTC) {
+ const auto tz = utc_time_zone();
+ time_zone::civil_transition trans;
+
+ auto tp = time_point<absl::time_internal::cctz::seconds>::min();
+ EXPECT_FALSE(tz.next_transition(tp, &trans));
+
+ tp = time_point<absl::time_internal::cctz::seconds>::max();
+ EXPECT_FALSE(tz.next_transition(tp, &trans));
+}
+
+TEST(PrevTransition, UTC) {
+ const auto tz = utc_time_zone();
+ time_zone::civil_transition trans;
+
+ auto tp = time_point<absl::time_internal::cctz::seconds>::max();
+ EXPECT_FALSE(tz.prev_transition(tp, &trans));
+
+ tp = time_point<absl::time_internal::cctz::seconds>::min();
+ EXPECT_FALSE(tz.prev_transition(tp, &trans));
+}
+
+TEST(NextTransition, AmericaNewYork) {
+ const auto tz = LoadZone("America/New_York");
+ time_zone::civil_transition trans;
+
+ auto tp = convert(civil_second(2018, 6, 30, 0, 0, 0), tz);
+ EXPECT_TRUE(tz.next_transition(tp, &trans));
+ EXPECT_EQ(civil_second(2018, 11, 4, 2, 0, 0), trans.from);
+ EXPECT_EQ(civil_second(2018, 11, 4, 1, 0, 0), trans.to);
+
+ tp = time_point<absl::time_internal::cctz::seconds>::max();
+ EXPECT_FALSE(tz.next_transition(tp, &trans));
+
+ tp = time_point<absl::time_internal::cctz::seconds>::min();
+ EXPECT_TRUE(tz.next_transition(tp, &trans));
+ if (trans.from == civil_second(1918, 3, 31, 2, 0, 0)) {
+ // It looks like the tzdata is only 32 bit (probably macOS),
+ // which bottoms out at 1901-12-13T20:45:52+00:00.
+ EXPECT_EQ(civil_second(1918, 3, 31, 3, 0, 0), trans.to);
+ } else {
+ EXPECT_EQ(civil_second(1883, 11, 18, 12, 3, 58), trans.from);
+ EXPECT_EQ(civil_second(1883, 11, 18, 12, 0, 0), trans.to);
+ }
+}
+
+TEST(PrevTransition, AmericaNewYork) {
+ const auto tz = LoadZone("America/New_York");
+ time_zone::civil_transition trans;
+
+ auto tp = convert(civil_second(2018, 6, 30, 0, 0, 0), tz);
+ EXPECT_TRUE(tz.prev_transition(tp, &trans));
+ EXPECT_EQ(civil_second(2018, 3, 11, 2, 0, 0), trans.from);
+ EXPECT_EQ(civil_second(2018, 3, 11, 3, 0, 0), trans.to);
+
+ tp = time_point<absl::time_internal::cctz::seconds>::min();
+ EXPECT_FALSE(tz.prev_transition(tp, &trans));
+
+ tp = time_point<absl::time_internal::cctz::seconds>::max();
+ EXPECT_TRUE(tz.prev_transition(tp, &trans));
+ // We have a transition but we don't know which one.
+}
+
TEST(TimeZoneEdgeCase, AmericaNewYork) {
const time_zone tz = LoadZone("America/New_York");
@@ -1104,35 +1178,31 @@ TEST(TimeZoneEdgeCase, PacificApia) {
TEST(TimeZoneEdgeCase, AfricaCairo) {
const time_zone tz = LoadZone("Africa/Cairo");
-#if defined(__ANDROID__) && __ANDROID_API__ < 21
- // Only Android 'L' and beyond have this tz2014c transition.
-#else
- // An interesting case of midnight not existing.
- //
- // 1400191199 == Thu, 15 May 2014 23:59:59 +0200 (EET)
- // 1400191200 == Fri, 16 May 2014 01:00:00 +0300 (EEST)
- auto tp = convert(civil_second(2014, 5, 15, 23, 59, 59), tz);
- ExpectTime(tp, tz, 2014, 5, 15, 23, 59, 59, 2 * 3600, false, "EET");
- tp += absl::time_internal::cctz::seconds(1);
- ExpectTime(tp, tz, 2014, 5, 16, 1, 0, 0, 3 * 3600, true, "EEST");
-#endif
+ if (VersionCmp(tz, "2014c") >= 0) {
+ // An interesting case of midnight not existing.
+ //
+ // 1400191199 == Thu, 15 May 2014 23:59:59 +0200 (EET)
+ // 1400191200 == Fri, 16 May 2014 01:00:00 +0300 (EEST)
+ auto tp = convert(civil_second(2014, 5, 15, 23, 59, 59), tz);
+ ExpectTime(tp, tz, 2014, 5, 15, 23, 59, 59, 2 * 3600, false, "EET");
+ tp += absl::time_internal::cctz::seconds(1);
+ ExpectTime(tp, tz, 2014, 5, 16, 1, 0, 0, 3 * 3600, true, "EEST");
+ }
}
TEST(TimeZoneEdgeCase, AfricaMonrovia) {
const time_zone tz = LoadZone("Africa/Monrovia");
-#if defined(__ANDROID__) && __ANDROID_API__ < 26
- // Only Android 'O' and beyond have this tz2017b transition.
-#else
- // Strange offset change -00:44:30 -> +00:00:00 (non-DST)
- //
- // 63593069 == Thu, 6 Jan 1972 23:59:59 -0044 (MMT)
- // 63593070 == Fri, 7 Jan 1972 00:44:30 +0000 (GMT)
- auto tp = convert(civil_second(1972, 1, 6, 23, 59, 59), tz);
- ExpectTime(tp, tz, 1972, 1, 6, 23, 59, 59, -44.5 * 60, false, "MMT");
- tp += absl::time_internal::cctz::seconds(1);
- ExpectTime(tp, tz, 1972, 1, 7, 0, 44, 30, 0 * 60, false, "GMT");
-#endif
+ if (VersionCmp(tz, "2017b") >= 0) {
+ // Strange offset change -00:44:30 -> +00:00:00 (non-DST)
+ //
+ // 63593069 == Thu, 6 Jan 1972 23:59:59 -0044 (MMT)
+ // 63593070 == Fri, 7 Jan 1972 00:44:30 +0000 (GMT)
+ auto tp = convert(civil_second(1972, 1, 6, 23, 59, 59), tz);
+ ExpectTime(tp, tz, 1972, 1, 6, 23, 59, 59, -44.5 * 60, false, "MMT");
+ tp += absl::time_internal::cctz::seconds(1);
+ ExpectTime(tp, tz, 1972, 1, 7, 0, 44, 30, 0 * 60, false, "GMT");
+ }
}
TEST(TimeZoneEdgeCase, AmericaJamaica) {
@@ -1144,28 +1214,29 @@ TEST(TimeZoneEdgeCase, AmericaJamaica) {
const time_zone tz = LoadZone("America/Jamaica");
// Before the first transition.
- auto tp = convert(civil_second(1889, 12, 31, 0, 0, 0), tz);
-#if AMERICA_JAMAICA_PRE_1913_OFFSET_FIX
- // Commit 907241e: Fix off-by-1 error for Jamaica and T&C before 1913.
- // Until that commit has made its way into a full release we avoid the
- // expectations on the -18430 offset below. TODO: Uncomment these.
- ExpectTime(tp, tz, 1889, 12, 31, 0, 0, 0, -18430, false,
- tz.lookup(tp).abbr);
-
- // Over the first (abbreviation-change only) transition.
- // -2524503170 == Tue, 31 Dec 1889 23:59:59 -0507 (LMT)
- // -2524503169 == Wed, 1 Jan 1890 00:00:00 -0507 (KMT)
- tp = convert(civil_second(1889, 12, 31, 23, 59, 59), tz);
- ExpectTime(tp, tz, 1889, 12, 31, 23, 59, 59, -18430, false,
- tz.lookup(tp).abbr);
- tp += absl::time_internal::cctz::seconds(1);
- ExpectTime(tp, tz, 1890, 1, 1, 0, 0, 0, -18430, false, "KMT");
-#endif
+ if (!tz.version().empty() && VersionCmp(tz, "2018d") >= 0) {
+ // We avoid the expectations on the -18430 offset below unless we are
+ // certain we have commit 907241e (Fix off-by-1 error for Jamaica and
+ // T&C before 1913) from 2018d. TODO: Remove the "version() not empty"
+ // part when 2018d is generally available from /usr/share/zoneinfo.
+ auto tp = convert(civil_second(1889, 12, 31, 0, 0, 0), tz);
+ ExpectTime(tp, tz, 1889, 12, 31, 0, 0, 0, -18430, false,
+ tz.lookup(tp).abbr);
+
+ // Over the first (abbreviation-change only) transition.
+ // -2524503170 == Tue, 31 Dec 1889 23:59:59 -0507 (LMT)
+ // -2524503169 == Wed, 1 Jan 1890 00:00:00 -0507 (KMT)
+ tp = convert(civil_second(1889, 12, 31, 23, 59, 59), tz);
+ ExpectTime(tp, tz, 1889, 12, 31, 23, 59, 59, -18430, false,
+ tz.lookup(tp).abbr);
+ tp += absl::time_internal::cctz::seconds(1);
+ ExpectTime(tp, tz, 1890, 1, 1, 0, 0, 0, -18430, false, "KMT");
+ }
// Over the last (DST) transition.
// 436341599 == Sun, 30 Oct 1983 01:59:59 -0400 (EDT)
// 436341600 == Sun, 30 Oct 1983 01:00:00 -0500 (EST)
- tp = convert(civil_second(1983, 10, 30, 1, 59, 59), tz);
+ auto tp = convert(civil_second(1983, 10, 30, 1, 59, 59), tz);
ExpectTime(tp, tz, 1983, 10, 30, 1, 59, 59, -4 * 3600, true, "EDT");
tp += absl::time_internal::cctz::seconds(1);
ExpectTime(tp, tz, 1983, 10, 30, 1, 0, 0, -5 * 3600, false, "EST");
diff --git a/absl/time/internal/cctz/src/zone_info_source.cc b/absl/time/internal/cctz/src/zone_info_source.cc
index ee7500b6..bf2d2d2d 100644
--- a/absl/time/internal/cctz/src/zone_info_source.cc
+++ b/absl/time/internal/cctz/src/zone_info_source.cc
@@ -20,6 +20,7 @@ namespace cctz {
// Defined out-of-line to avoid emitting a weak vtable in all TUs.
ZoneInfoSource::~ZoneInfoSource() {}
+std::string ZoneInfoSource::Version() const { return std::string(); }
} // namespace cctz
} // namespace time_internal