From 198b034679cf7f962e242977b99ea5c63ad9eb03 Mon Sep 17 00:00:00 2001 From: Billy Robert O'Neal III Date: Wed, 30 Dec 2020 19:46:21 -0800 Subject: [PATCH] Fix year calculation for the last day of a leap year. --- Release/src/utilities/asyncrt_utils.cpp | 6 ++++++ Release/tests/functional/utils/datetime.cpp | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/Release/src/utilities/asyncrt_utils.cpp b/Release/src/utilities/asyncrt_utils.cpp index 16d7e687b9..579fedc25f 100644 --- a/Release/src/utilities/asyncrt_utils.cpp +++ b/Release/src/utilities/asyncrt_utils.cpp @@ -736,6 +736,12 @@ static compute_year_result compute_year(int64_t secondsSince1900) int secondsInt = static_cast(secondsLeft - year4 * SecondsIn4Years); int year1 = secondsInt / SecondsInYear; + if (year1 == 4) + { + // this is the last day in a leap year + year1 = 3; + } + secondsInt -= year1 * SecondsInYear; // shift back to 1900 base from 1601: diff --git a/Release/tests/functional/utils/datetime.cpp b/Release/tests/functional/utils/datetime.cpp index aac5b16714..a584b1e3c2 100644 --- a/Release/tests/functional/utils/datetime.cpp +++ b/Release/tests/functional/utils/datetime.cpp @@ -133,6 +133,21 @@ SUITE(datetime) TestDateTimeRoundtrip(_XPLATSTR("9999-12-31T23:59:59Z")); } + TEST(parsing_time_roundtrip_year_2016) + { + TestDateTimeRoundtrip(_XPLATSTR("2016-12-31T20:59:59Z")); + } + + TEST(parsing_time_roundtrip_year_2020) + { + TestDateTimeRoundtrip(_XPLATSTR("2020-12-31T20:59:59Z")); + } + + TEST(parsing_time_roundtrip_year_2021) + { + TestDateTimeRoundtrip(_XPLATSTR("2021-01-01T20:59:59Z")); + } + TEST(emitting_time_correct_day) { const auto test = utility::datetime() + UINT64_C(132004507640000000); // 2019-04-22T23:52:44 is a Monday const auto actual = test.to_string(utility::datetime::RFC_1123);