From ec9f60d3fd1dd2dbcecba8017225f40763f3f862 Mon Sep 17 00:00:00 2001 From: John Ralls Date: Sat, 9 Dec 2017 15:36:43 -0800 Subject: [PATCH] Test more thoroughly gnc-timezone's parsing of the zoneinfo database. Then fix the resulting problems. --- libgnucash/engine/gnc-timezone.cpp | 60 +++-- libgnucash/engine/test/gtest-gnc-timezone.cpp | 211 +++++++++++++++++- 2 files changed, 244 insertions(+), 27 deletions(-) diff --git a/libgnucash/engine/gnc-timezone.cpp b/libgnucash/engine/gnc-timezone.cpp index d0ad216832..0a26b484be 100644 --- a/libgnucash/engine/gnc-timezone.cpp +++ b/libgnucash/engine/gnc-timezone.cpp @@ -603,6 +603,8 @@ void TimeZoneProvider::parse_file(const std::string& tzname) { IANAParser::IANAParser parser(tzname); + using boost::posix_time::hours; + const auto one_year = hours(366 * 24); //Might be a leap year. auto last_info = std::find_if(parser.tzinfo.begin(), parser.tzinfo.end(), [](IANAParser::TZInfo tz) {return !tz.info.isdst;}); @@ -620,34 +622,48 @@ TimeZoneProvider::parse_file(const std::string& tzname) auto this_time = ptime(date(1970, 1, 1), time_duration(txi->timestamp / 3600, 0, txi->timestamp % 3600)); + /* Note: The "get" function retrieves the last zone with a + * year *earlier* than the requested year: Zone periods run + * from the saved year to the beginning year of the next zone. + */ try { auto this_year = this_time.date().year(); //Initial case if (last_time.is_not_a_date_time()) - zone_vector.push_back(zone_no_dst(this_year - 1, last_info)); - //gap in transitions > 1 year, non-dst zone - //change. In the last case the exact date of the change will be - //wrong because boost::local_date::timezone isn't able to - //represent it. For GnuCash's purposes this isn't likely to be - //important as the last time this sort of transition happened - //was 1946, but we have to handle the case in order to parse - //the tz file. - else if (this_year - last_time.date().year() > 1 || - last_info->info.isdst == this_info->info.isdst) { - zone_vector.push_back(zone_no_dst(this_year, last_info)); + zone_vector.push_back(zone_no_dst(this_year - 1, last_info)); + zone_vector.push_back(zone_no_dst(this_year, this_info)); } - - else + // No change in is_dst means a permanent zone change. + else if (last_info->info.isdst == this_info->info.isdst) + { + zone_vector.push_back(zone_no_dst(this_year, this_info)); + } + /* If there have been no transitions in at least a year + * then we need to create a no-DST rule with last_info to + * reflect the frozen timezone. + */ + else if (this_time - last_time > one_year) + { + auto year = last_time.date().year(); + if (zone_vector.back().first == year) + year = year + 1; // no operator ++ or +=, sigh. + zone_vector.push_back(zone_no_dst(year, last_info)); + } + /* It's been less than a year, so it's probably a DST + * cycle. This consumes two transitions so we want only + * the return-to-standard-time one to make a DST rule. + */ + else if (!this_info->info.isdst) { DSTRule::DSTRule new_rule(last_info, this_info, last_time, this_time); if (new_rule != last_rule) { last_rule = new_rule; - zone_vector.push_back(zone_from_rule (this_time.date().year(), - new_rule)); + auto year = last_time.date().year(); + zone_vector.push_back(zone_from_rule(year, new_rule)); } } } @@ -658,12 +674,12 @@ TimeZoneProvider::parse_file(const std::string& tzname) last_time = this_time; last_info = this_info; } - +/* if the transitions end before the end of the zoneinfo coverage + * period then the zone rescinded DST and we need a final no-dstzone. + */ if (last_time.is_not_a_date_time() || last_time.date().year() < parser.last_year) - zone_vector.push_back(zone_no_dst(max_year, last_info)); - else //Last DST rule forever after. - zone_vector.push_back(zone_from_rule(max_year, last_rule)); + zone_vector.push_back(zone_no_dst(last_time.date().year(), last_info)); } bool @@ -714,13 +730,11 @@ TimeZoneProvider::TimeZoneProvider(const std::string& tzname) : zone_vector {} TZ_Ptr TimeZoneProvider::get(int year) const noexcept { + if (zone_vector.empty()) + return TZ_Ptr(new PTZ("UTC0")); auto iter = find_if(zone_vector.rbegin(), zone_vector.rend(), [=](TZ_Entry e) { return e.first <= year; }); if (iter == zone_vector.rend()) - { - if (!zone_vector.empty()) return zone_vector.front().second; - return TZ_Ptr(new PTZ("UTC0")); - } return iter->second; } diff --git a/libgnucash/engine/test/gtest-gnc-timezone.cpp b/libgnucash/engine/test/gtest-gnc-timezone.cpp index b73b085a05..0703333012 100644 --- a/libgnucash/engine/test/gtest-gnc-timezone.cpp +++ b/libgnucash/engine/test/gtest-gnc-timezone.cpp @@ -65,10 +65,213 @@ TEST(gnc_timezone_constructors, test_posix_timezone) std::string timezone("FST08FDT07,M4.1.0,M10.31.0"); TimeZoneProvider tzp(timezone); TZ_Ptr tz = tzp.get(2006); - EXPECT_TRUE(tz->std_zone_abbrev() == "FST"); - EXPECT_TRUE(tz->dst_zone_abbrev() == "FDT"); - EXPECT_TRUE(tz->base_utc_offset().hours() == 8L); - EXPECT_TRUE(tz->dst_offset().hours() == 7L); + EXPECT_EQ(tz->std_zone_abbrev(), "FST"); + EXPECT_EQ(tz->dst_zone_abbrev(), "FDT"); + EXPECT_EQ(tz->base_utc_offset().hours(), 8L); + EXPECT_EQ(tz->dst_offset().hours(), 7L); +} + +TEST(gnc_timezone_constructors, test_IANA_Belize_tz) +{ + TimeZoneProvider tzp("America/Belize"); + for (int year = 1908; year < 1990; ++year) + { + auto tz = tzp.get(year); + if (year < 1912) + { + EXPECT_EQ(tz->std_zone_abbrev(), "LMT"); + EXPECT_FALSE(tz->has_dst()); + EXPECT_EQ(tz->base_utc_offset().total_seconds(), -21168); + } + else if (year < 1918) + { + EXPECT_EQ(tz->std_zone_abbrev(), "CST"); + EXPECT_FALSE(tz->has_dst()); + EXPECT_EQ(tz->base_utc_offset().total_seconds(), -21600); + } + else if (year < 1943) + { + EXPECT_EQ(tz->std_zone_abbrev(), "CST"); + EXPECT_TRUE(tz->has_dst()); + EXPECT_EQ(tz->base_utc_offset().total_seconds(), -21600); + EXPECT_EQ(tz->dst_zone_abbrev(), "-0530"); + EXPECT_EQ(tz->dst_offset().total_seconds(), 1800); + } + else if (year == 1973 || year == 1982) + { + EXPECT_EQ(tz->std_zone_abbrev(), "CST"); + EXPECT_TRUE(tz->has_dst()); + EXPECT_EQ(tz->base_utc_offset().total_seconds(), -21600); + EXPECT_EQ(tz->dst_zone_abbrev(), "CDT"); + EXPECT_EQ(tz->dst_offset().total_seconds(), 3600); + } + else + { + EXPECT_EQ(tz->std_zone_abbrev(), "CST"); + EXPECT_FALSE(tz->has_dst()); + EXPECT_EQ(tz->base_utc_offset().total_seconds(), -21600); + } + } +} + +TEST(gnc_timezone_constructors, test_IANA_Perth_tz) +{ + TimeZoneProvider tzp("Australia/Perth"); + for (int year = 1893; year < 2048; ++year) + { + auto tz = tzp.get(year); + if (year < 1895) + { + EXPECT_EQ(tz->std_zone_abbrev(), "LMT"); + EXPECT_FALSE(tz->has_dst()); + EXPECT_EQ(tz->base_utc_offset().total_seconds(), 27804); + } + else if (year < 1916) + { + EXPECT_EQ(tz->std_zone_abbrev(), "AWST"); + EXPECT_FALSE(tz->has_dst()); + EXPECT_EQ(tz->base_utc_offset().total_seconds(), 28800); + } + else if (year < 1917) + { + EXPECT_EQ(tz->std_zone_abbrev(), "AWST"); + EXPECT_TRUE(tz->has_dst()); + EXPECT_EQ(tz->base_utc_offset().total_seconds(), 28800); + EXPECT_EQ(tz->dst_zone_abbrev(), "AWDT"); + EXPECT_EQ(tz->dst_offset().total_seconds(), 3600); + } + else if (year < 1941) + { + EXPECT_EQ(tz->std_zone_abbrev(), "AWST"); + EXPECT_FALSE(tz->has_dst()); + EXPECT_EQ(tz->base_utc_offset().total_seconds(), 28800); + } + else if (year < 1943) + { + EXPECT_EQ(tz->std_zone_abbrev(), "AWST"); + EXPECT_TRUE(tz->has_dst()); + EXPECT_EQ(tz->base_utc_offset().total_seconds(), 28800); + EXPECT_EQ(tz->dst_zone_abbrev(), "AWDT"); + EXPECT_EQ(tz->dst_offset().total_seconds(), 3600); + } + else if (year == 1974 || year == 1983 || year == 1991 || + (year > 2005 && year < 2009)) + { + EXPECT_EQ(tz->std_zone_abbrev(), "AWST"); + EXPECT_TRUE(tz->has_dst()); + EXPECT_EQ(tz->base_utc_offset().total_seconds(), 28800); + EXPECT_EQ(tz->dst_zone_abbrev(), "AWDT"); + EXPECT_EQ(tz->dst_offset().total_seconds(), 3600); + } + else + { + EXPECT_EQ(tz->std_zone_abbrev(), "AWST"); + EXPECT_FALSE(tz->has_dst()); + EXPECT_EQ(tz->base_utc_offset().total_seconds(), 28800); + } + } +} + +TEST(gnc_timezone_constructors, test_IANA_Minsk_tz) +{ + TimeZoneProvider tzp("Europe/Minsk"); + for (int year = 1870; year < 2020; ++year) + { + auto tz = tzp.get(year); + if (year < 1879) + { + EXPECT_EQ(tz->std_zone_abbrev(), "LMT"); + EXPECT_FALSE(tz->has_dst()); + EXPECT_EQ(tz->base_utc_offset().total_seconds(), 6616); + } + else if (year < 1924) + { + EXPECT_EQ(tz->std_zone_abbrev(), "MMT"); + EXPECT_FALSE(tz->has_dst()); + EXPECT_EQ(tz->base_utc_offset().total_seconds(), 6600); + } + else if (year < 1930) + { + EXPECT_EQ(tz->std_zone_abbrev(), "EET"); + EXPECT_FALSE(tz->has_dst()); + EXPECT_EQ(tz->base_utc_offset().total_seconds(), 7200); + } + else if (year < 1941) + { + EXPECT_EQ(tz->std_zone_abbrev(), "MSK"); + EXPECT_FALSE(tz->has_dst()); + EXPECT_EQ(tz->base_utc_offset().total_seconds(), 10800); + } + /* The TZInfo says Minsk had DST from June 1941 - Nov + * 1942. Boost::date_time doesn't know how to model that so we + * just pretend that it was a weird standard time. Note that + * Minsk was under German occupation and got shifted to Berlin + * time, sort of. + */ + else if (year < 1943) + { + EXPECT_EQ(tz->std_zone_abbrev(), "CEST"); + EXPECT_FALSE(tz->has_dst()); + EXPECT_EQ(tz->base_utc_offset().total_seconds(), 7200); + EXPECT_EQ(tz->dst_zone_abbrev(), ""); + EXPECT_EQ(tz->dst_offset().total_seconds(), 0); + } + else if (year == 1943) + { + EXPECT_EQ(tz->std_zone_abbrev(), "CET"); + EXPECT_TRUE(tz->has_dst()); + EXPECT_EQ(tz->base_utc_offset().total_seconds(), 3600); + EXPECT_EQ(tz->dst_zone_abbrev(), "CEST"); + EXPECT_EQ(tz->dst_offset().total_seconds(), 3600); + } + /* Minsk was "liberated" by the Soviets 2 Jul 1944 and went + * back to a more reasonable local time with no DST. Another + * case that's too hard for boost::timezone to model correctly + * so we fudge. + */ + else if (year == 1944) + { + EXPECT_EQ(tz->std_zone_abbrev(), "MSK"); + EXPECT_TRUE(tz->has_dst()); + EXPECT_EQ(tz->base_utc_offset().total_seconds(), 10800); + EXPECT_EQ(tz->dst_zone_abbrev(), "CEST"); + EXPECT_EQ(tz->dst_offset().total_seconds(), -3600); + } + else if (year < 1981) + { + EXPECT_EQ(tz->std_zone_abbrev(), "MSK"); + EXPECT_FALSE(tz->has_dst()); + EXPECT_EQ(tz->base_utc_offset().total_seconds(), 10800); + } + else if (year < 1989) + { + EXPECT_EQ(tz->std_zone_abbrev(), "MSK"); + EXPECT_TRUE(tz->has_dst()); + EXPECT_EQ(tz->base_utc_offset().total_seconds(), 10800); + EXPECT_EQ(tz->dst_zone_abbrev(), "MSD"); + EXPECT_EQ(tz->dst_offset().total_seconds(), 3600); + } + else if (year < 1991) + { + EXPECT_EQ(tz->std_zone_abbrev(), "MSK"); + EXPECT_FALSE(tz->has_dst()); + EXPECT_EQ(tz->base_utc_offset().total_seconds(), 10800); + } + else if (year < 2011) + { + EXPECT_EQ(tz->std_zone_abbrev(), "EET"); + EXPECT_TRUE(tz->has_dst()); + EXPECT_EQ(tz->base_utc_offset().total_seconds(), 7200); + EXPECT_EQ(tz->dst_zone_abbrev(), "EEST"); + EXPECT_EQ(tz->dst_offset().total_seconds(), 3600); + } + else + { + EXPECT_EQ(tz->std_zone_abbrev(), "+03"); + EXPECT_FALSE(tz->has_dst()); + EXPECT_EQ(tz->base_utc_offset().total_seconds(), 10800); + } + } } #endif