floraison / fugit

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

weekday cron does not run today when started before execution time #52

Closed grosser closed 3 years ago

grosser commented 3 years ago

fugit 1.4.2

the second Tuesday of the week was today, we started the cron earlier today, but it never executed, because it thinks the next execution is next month and not today ... so seems like the weekday calculation needs to take time of day into account

TZ=utc bundle exec ruby -r bundler/setup -r fugit -e 'puts Time.now'
2021-02-09 17:41:10 +0000
TZ=utc bundle exec ruby -r bundler/setup -r fugit -e 'puts Fugit.do_parse_cron("55 18 * * 2#1").next_time.to_s'
2021-03-02 18:55:00 +0000
  it "calculates weekdays correctly when starting at the same day" do
    Time.stubs(:now).returns(Time.parse("2021-02-02 00:00:00")) # tuesday morning
    Fugit.do_parse_cron("59 23 * * 1").next_time.to_s.must_equal "2021-02-02 23:59:00 -0000"
  end
jmettraux commented 3 years ago
  it "calculates weekdays correctly when starting at the same day" do
    Time.stubs(:now).returns(Time.parse("2021-02-02 00:00:00")) # tuesday morning
    Fugit.do_parse_cron("59 23 * * 2").next_time.to_s.must_equal "2021-02-02 23:59:00 -0000"
  end

since 2021-02-02 is a Tuesday.

jmettraux commented 3 years ago

The spec you gave me, when adapted, goes green.

OK, if I look at the initial problem explanation, 2021-02-09 is the second Tuesday of the month, not the first. So I think that fugit is right.

At 2021-02-09 17:41:10, given "55 18 2#1", the next first Tuesday of the month at 18:55 is "2021-03-02 18:55".

Did I misunderstand you?

grosser commented 3 years ago

my initial case was second tuesday, I tried to dumb it down to make the spec simpler 🤦

2021-02-09 17:41:10 + "55 18 2#1", should come out as is "2021-02-09 18:55" since it is currently the second Tuesday and not yet 18:55

jmettraux commented 3 years ago

2021-02-09 17:41:10 + "55 18 2#1", should come out as is "2021-02-09 18:55" since it is currently the second Tuesday and not yet 18:55

No, "2#1" means "first Tuesday of the month", not "second Tuesday of the month".

https://github.com/floraison/fugit#the-hash-extension

grosser commented 3 years ago

yeah you are correct ... so something happened on the first tuesday and I though it was supposed to come today ... thx for looking into it, sorry for the noise 😞