Skip to content

Commit

Permalink
Add Date#next_year and Date#prev_year
Browse files Browse the repository at this point in the history
See ruby-doc.org/stdlib-2.5.0/libdoc/date/rdoc/Date.html#method-i-next_year
  • Loading branch information
jgaskins authored and elia committed Sep 12, 2018
1 parent f21e5bc commit e5d2534
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 0 additions & 2 deletions spec/filters/bugs/date.rb
Expand Up @@ -23,11 +23,9 @@
fails "Date#mjd determines the Modified Julian day"
fails "Date#mon determines the month"
fails "Date#new_start converts a date object into another with a new calendar reform"
fails "Date#next_year returns the day of the reform if date falls within calendar reform"
fails "Date#parse parses a day name into a Date object"
fails "Date#parse parses a month day into a Date object"
fails "Date#parse parses a month name into a Date object"
fails "Date#prev_year returns the day of the reform if date falls within calendar reform"
fails "Date#strftime should be able to print the commercial year with leading zeroes"
fails "Date#strftime should be able to print the commercial year with only two digits"
fails "Date#strftime should be able to print the julian day with leading zeroes"
Expand Down
15 changes: 15 additions & 0 deletions stdlib/date.rb
Expand Up @@ -347,6 +347,13 @@ def gregorian_leap?(year)
end

def initialize(year = -4712, month = 1, day = 1, start = ITALY)
%x{
// Because of Gregorian reform calendar goes from 1582-10-04 to 1582-10-15.
// All days in between end up as 4 october.
if (year === 1582 && month === 10 && day > 4 && day < 15) {
day = 4;
}
}
@date = `new Date(year, month - 1, day)`
end

Expand Down Expand Up @@ -545,6 +552,10 @@ def next_month(n = 1)
}
end

def next_year(years = 1)
self.class.new(year + years, month, day)
end

def prev_day(n = 1)
self - n
end
Expand All @@ -559,6 +570,10 @@ def prev_month(n = 1)
}
end

def prev_year(years = 1)
self.class.new(year - years, month, day)
end

def saturday?
wday == 6
end
Expand Down

0 comments on commit e5d2534

Please sign in to comment.