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

Alter logic in Time.leap_year? for performance #5575

Merged
merged 2 commits into from
Apr 9, 2018
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
26 changes: 26 additions & 0 deletions spec/std/time/time_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,32 @@ describe Time do
Time.days_in_year(1990).should eq(365)
end

describe ".leap_year?" do
it "knows that 400-year centuries are leap years" do
{1600, 2000, 2400}.each do |year|
Time.leap_year?(year).should be_true
end
end

it "knows that 100-year centuries are normal years" do
{1700, 1800, 1900, 2100, 2200, 2300}.each do |year|
Time.leap_year?(year).should be_false
end
end

it "knows that typical non-century leap years are divisibly by 4" do
{1968, 1972, 2004, 2020}.each do |year|
Time.leap_year?(year).should be_true
end
end

it "knows years *not* divisible by 4 are normal" do
{1965, 1999, 2001, 2018, 2019, 2021, 2099, 2101}.each do |year|
Time.leap_year?(year).should be_false
end
end
end

typeof(Time.now.year)
typeof(1.minute.from_now.year)
typeof(1.minute.ago.year)
Expand Down
2 changes: 1 addition & 1 deletion src/time.cr
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ struct Time
raise ArgumentError.new "Invalid year"
end

(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
end

def inspect(io : IO)
Expand Down