Resolves the leap year last day calculation issue. (#1254)

This commit is contained in:
Kan Tang 2021-01-05 09:45:21 +08:00 committed by GitHub
parent da86ed7ec5
commit fb869f10af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 0 deletions

View File

@ -92,6 +92,11 @@ constexpr ComputeYearResult ComputeYear(int64_t secondsSince1601)
int secondsInt = static_cast<int>(secondsLeft - static_cast<int64_t>(year4) * SecondsIn4Years);
int year1 = secondsInt / SecondsInYear;
if (year1 == 4)
{
// this is the last day in a leap year
year1 = 3;
}
secondsInt -= year1 * SecondsInYear;
return {year400 * 400 + year100 * 100 + year4 * 4 + year1, secondsInt};

View File

@ -409,6 +409,13 @@ TEST(DateTime, ParseTimeRfc3339AcceptsLowercaseTZ)
TestDateTimeRoundtrip("1970-01-01T00:00:00z", "1970-01-01T00:00:00Z");
}
TEST(DateTime, ParsingTimeRoundtripLeapYearLastDay)
{
TestDateTimeRoundtrip("2016-12-31T20:59:59Z");
TestDateTimeRoundtrip("2020-12-31T20:59:59Z");
TestDateTimeRoundtrip("2021-01-01T20:59:59Z");
}
TEST(DateTime, ParseTimeRoundtripAcceptsInvalidNoTrailingTimezone)
{
// No digits after the dot, or non-digits. This is not a valid input, but we should not choke on