Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix year calculation for the last day of a leap year. #1550

Merged
merged 1 commit into from
Dec 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Release/src/utilities/asyncrt_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,12 @@ static compute_year_result compute_year(int64_t secondsSince1900)
int secondsInt = static_cast<int>(secondsLeft - year4 * SecondsIn4Years);

int year1 = secondsInt / SecondsInYear;
if (year1 == 4)
{
// this is the last day in a leap year
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is a little confusing since we are talking about a day, but the variable is year.

year1 = 3;
}
Comment on lines +739 to +743
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given this is being ported here, I want to understand it a bit better:
Azure/azure-sdk-for-cpp#1254

However, it has an edge case where the current year is the last day of a leap year, such that SecondsIn4Years contains that extra leap day but the calculation of year didn't account for the current date being exactly that extra day.

I don't fully follow what you mean here. Can you please elaborate, with a concrete example. Also, what do you mean by the "current year is the last day of a leap year"?

Lastly, what is an example impact of this bug exactly to an end user?

cc @antkmsft

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't fully follow what you mean here. Can you please elaborate, with a concrete example.

2020-12-31 results in year1 being 4, despite this code expecting all blocks of 4 years to be handled by SecondsIn4Years.

Also, what do you mean by the "current year is the last day of a leap year"?

Should be current "current day is the last day of a leap year"

Lastly, what is an example impact of this bug exactly to an end user?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lastly, what is an example impact of this bug exactly to an end user?

It incorrectly prints Thursday 2020-12-31 as Thu 2021-01-01


secondsInt -= year1 * SecondsInYear;

// shift back to 1900 base from 1601:
Expand Down
15 changes: 15 additions & 0 deletions Release/tests/functional/utils/datetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down