floraison / fugit

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

Cron for first Tuesday of the month does not work #35

Closed grosser closed 4 years ago

grosser commented 4 years ago
require 'fugit'
c = Fugit.do_cron('59 6 1-7 * 2')
p c.next_time

the 1-7 constraint is ignored I got "2020-03-17 06:59:00 -0700" today

jmettraux commented 4 years ago

"2020-03-17 06:59:00 -0700" is the right answer.

man 5 crontab says:

Note: The day of a command's execution can be specified by two fields -- day of month, and day of week. If both fields are restricted (ie, are not ), the command will be run when either field matches the current time. For example, ``30 4 1,15 5'' would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.

See also https://superuser.com/questions/428807/run-a-cron-job-on-the-first-monday-of-every-month

I think fugit can help in this way:

require 'fugit'                                             
c = Fugit.do_parse_cron('59 6 * * 2#1')    
c = Fugit.do_parse_cron('59 6 * * tue#1')              
p c.next_time.to_s  
  # ==> "2020-04-07 06:59:00 +0900" for me

2#1 and tue#1 mean first Tuesday (of the month).

Unfortunately this was only documented in the mother project: https://github.com/jmettraux/rufus-scheduler#cronline-notations-specific-to-rufus-scheduler

I will close this issue when I have documented this here in fugit.

Thanks!

grosser commented 4 years ago

thx, this is awesome :)

... I was sure this is a valid syntax since I got it from a "how to do foo in cron" stackoverflow ... maybe there are different flavors of cron, or it was just wrong though :)

On Sat, Mar 14, 2020 at 4:35 PM John Mettraux notifications@github.com wrote:

"2020-03-17 06:59:00 -0700" is the right answer.

man 5 crontab says:

Note: The day of a command's execution can be specified by two fields -- day of month, and day of week. If both fields are restricted (ie, are not ), the command will be run when either field matches the current time. For example, ``30 4 1,15 5'' would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.

See also https://superuser.com/questions/428807/run-a-cron-job-on-the-first-monday-of-every-month

I think fugit can help in this way:

require 'fugit' c = Fugit.do_parse_cron('59 6 2#1') c = Fugit.do_parse_cron('59 6 tue#1') p c.next_time.to_s

==> "2020-04-07 06:59:00 +0900" for me

2#1 and tue#1 mean first Tuesday (of the month).

Unfortunately this was only documented in the mother project: https://github.com/jmettraux/rufus-scheduler#cronline-notations-specific-to-rufus-scheduler

I will close this issue when I have documented this here in fugit.

Thanks!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/floraison/fugit/issues/35#issuecomment-599147841, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAACYZ7G3YF7TJEA3QFSQODRHQIEDANCNFSM4LJ3G3EQ .

jmettraux commented 4 years ago

Documentation done.

If there is anything unclear, please tell me or patch.

Closing, thanks!