floraison / fugit

time tools (cron, parsing, durations, ...) for Ruby, rufus-scheduler, and flor
MIT License
355 stars 29 forks source link

Fix dec_month implementation to make previous_time work for last days of months #51

Closed solteszad closed 3 years ago

solteszad commented 3 years ago

previous_time did not work properly for the last days of months (not just for 29th of Februray, but for like 31st of October as well), for example:

Fugit::Cron.parse('0 20 31 oct *').previous_time(Time.parse('2019-11-01 04:00')).to_s
=> "2018-10-31 20:00:00 +0100"

As you can see the year is off by one. This is because dec_month had a bug, where it decreased the current time with one more day then necessary (this is tricky, because hours are indexed from 0, but days are from 1). Also the current dec_month implementation did not set the hours, minutes and seconds, so anything after '2019-10-31 04:00' was inaccessible with previous_time.

After this change, previous_time works properly:

Fugit::Cron.parse('0 20 31 oct *').previous_time(Time.parse('2019-11-01 04:00')).to_s
=> "2019-10-31 20:00:00 +0100"
jmettraux commented 3 years ago

Excellent, thanks a lot!