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

Restrict argument to isleapyear(::Integer) #55317

Merged
merged 1 commit into from
Jul 31, 2024
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
2 changes: 1 addition & 1 deletion stdlib/Dates/src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ function totaldays(y, m, d)
end

# If the year is divisible by 4, except for every 100 years, except for every 400 years
isleapyear(y) = (y % 4 == 0) && ((y % 100 != 0) || (y % 400 == 0))
isleapyear(y::Integer) = (y % 4 == 0) && ((y % 100 != 0) || (y % 400 == 0))
Copy link
Contributor

Choose a reason for hiding this comment

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

An alternative would be to change it to

Suggested change
isleapyear(y::Integer) = (y % 4 == 0) && ((y % 100 != 0) || (y % 400 == 0))
isleapyear(y:) = iszero(y % 4) && (!iszero(y % 100) || iszero(y % 400))

which would work for Year(1992). Alas, then someone might come with another date-like object that again triggers a similar issue... so perhaps best to restrict it to Integer

(explicit type annotations like this are good for avoiding many type invalidation issues, so I think this is a good idea anyway)


# Number of days in month
const DAYSINMONTH = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
Expand Down
1 change: 1 addition & 0 deletions stdlib/Dates/test/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ end
@test Dates.isleapyear(-1) == false
@test Dates.isleapyear(4) == true
@test Dates.isleapyear(-4) == true
@test_throws MethodError Dates.isleapyear(Dates.Year(1992))
end
# Create "test" check manually
y = Dates.Year(1)
Expand Down