Skip to content

Commit

Permalink
Fix leap years
Browse files Browse the repository at this point in the history
closes #13
  • Loading branch information
hzamani committed Nov 30, 2022
1 parent e23cdb4 commit 78600db
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
4 changes: 3 additions & 1 deletion lib/parsi-date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,13 @@ class Date
LD_EPOCH_IN_CJD = 2299160 # :nodoc:
DAYS_IN_MONTH = [nil, 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29] # :nodoc:

LEAP_REMINDERS = [1, 5, 9, 13, 17, 22, 26, 30].freeze

shared_methods = Module.new do

# Returns true if the given year is a leap year of the calendar.
def leap? year
((((((year - ((year > 0) ? 474 : 473)) % 2820) + 474) + 38) * 682) % 2816) < 682
LEAP_REMINDERS.include?(year - (year / 33).floor * 33)
end
alias_method :jalali_leap?, :leap?

Expand Down
14 changes: 8 additions & 6 deletions spec/parsi-date/leap_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
describe "Parsi::Date#leap?" do
it "returns true if a year is a leap year in the Parsi (Jalali) calendar" do
expect(Parsi::Date.leap?(1387)).to be_truthy
expect(Parsi::Date.leap?(1391)).to be_truthy
expect(Parsi::Date.leap?(1395)).to be_truthy
expect Parsi::Date.leap?(1387) == true
expect Parsi::Date.leap?(1391) == true
expect Parsi::Date.leap?(1395) == true
expect Parsi::Date.leap?(1403) == true
end

it "returns false if a year is not a leap year in the Parsi (Jalali) calendar" do
expect(Parsi::Date.leap?(1390)).to be_falsey
expect(Parsi::Date.leap?(1392)).to be_falsey
expect(Parsi::Date.leap?(1400)).to be_falsey
expect Parsi::Date.leap?(1390) == false
expect Parsi::Date.leap?(1392) == false
expect Parsi::Date.leap?(1400) == false
expect Parsi::Date.leap?(1404) == false
end
end

0 comments on commit 78600db

Please sign in to comment.