Test more thoroughly gnc-timezone's parsing of the zoneinfo database.

Then fix the resulting problems.
This commit is contained in:
John Ralls 2017-12-09 15:36:43 -08:00
parent 98d41bc374
commit ec9f60d3fd
2 changed files with 244 additions and 27 deletions

View File

@ -603,6 +603,8 @@ void
TimeZoneProvider::parse_file(const std::string& tzname) TimeZoneProvider::parse_file(const std::string& tzname)
{ {
IANAParser::IANAParser parser(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(), auto last_info = std::find_if(parser.tzinfo.begin(), parser.tzinfo.end(),
[](IANAParser::TZInfo tz) [](IANAParser::TZInfo tz)
{return !tz.info.isdst;}); {return !tz.info.isdst;});
@ -620,34 +622,48 @@ TimeZoneProvider::parse_file(const std::string& tzname)
auto this_time = ptime(date(1970, 1, 1), auto this_time = ptime(date(1970, 1, 1),
time_duration(txi->timestamp / 3600, 0, time_duration(txi->timestamp / 3600, 0,
txi->timestamp % 3600)); 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 try
{ {
auto this_year = this_time.date().year(); auto this_year = this_time.date().year();
//Initial case //Initial case
if (last_time.is_not_a_date_time()) 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));
} }
// No change in is_dst means a permanent zone change.
else 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, DSTRule::DSTRule new_rule(last_info, this_info,
last_time, this_time); last_time, this_time);
if (new_rule != last_rule) if (new_rule != last_rule)
{ {
last_rule = new_rule; last_rule = new_rule;
zone_vector.push_back(zone_from_rule (this_time.date().year(), auto year = last_time.date().year();
new_rule)); 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_time = this_time;
last_info = this_info; 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() || if (last_time.is_not_a_date_time() ||
last_time.date().year() < parser.last_year) last_time.date().year() < parser.last_year)
zone_vector.push_back(zone_no_dst(max_year, last_info)); zone_vector.push_back(zone_no_dst(last_time.date().year(), last_info));
else //Last DST rule forever after.
zone_vector.push_back(zone_from_rule(max_year, last_rule));
} }
bool bool
@ -714,13 +730,11 @@ TimeZoneProvider::TimeZoneProvider(const std::string& tzname) : zone_vector {}
TZ_Ptr TZ_Ptr
TimeZoneProvider::get(int year) const noexcept 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(), auto iter = find_if(zone_vector.rbegin(), zone_vector.rend(),
[=](TZ_Entry e) { return e.first <= year; }); [=](TZ_Entry e) { return e.first <= year; });
if (iter == zone_vector.rend()) if (iter == zone_vector.rend())
{
if (!zone_vector.empty())
return zone_vector.front().second; return zone_vector.front().second;
return TZ_Ptr(new PTZ("UTC0"));
}
return iter->second; return iter->second;
} }

View File

@ -65,10 +65,213 @@ TEST(gnc_timezone_constructors, test_posix_timezone)
std::string timezone("FST08FDT07,M4.1.0,M10.31.0"); std::string timezone("FST08FDT07,M4.1.0,M10.31.0");
TimeZoneProvider tzp(timezone); TimeZoneProvider tzp(timezone);
TZ_Ptr tz = tzp.get(2006); TZ_Ptr tz = tzp.get(2006);
EXPECT_TRUE(tz->std_zone_abbrev() == "FST"); EXPECT_EQ(tz->std_zone_abbrev(), "FST");
EXPECT_TRUE(tz->dst_zone_abbrev() == "FDT"); EXPECT_EQ(tz->dst_zone_abbrev(), "FDT");
EXPECT_TRUE(tz->base_utc_offset().hours() == 8L); EXPECT_EQ(tz->base_utc_offset().hours(), 8L);
EXPECT_TRUE(tz->dst_offset().hours() == 7L); 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 #endif