floraison / fugit

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

Natural parsing for ranges of hours in ranges of days #44

Closed gee-forr closed 3 years ago

gee-forr commented 3 years ago

Issue description

Hello there, I'm wondering if fugit supports natural parsing of both days and weeks? We're looking for a way to define blocks of time where people are or aren't available. Below is a simple example.

How to reproduce

require 'fugit'

f = Fugit::Nat.parse('every weekday 8am to 5pm') # => nil

Expected behaviour

I was expecting to get a fugit cron object back that looks a little something like this?

#<Fugit::Cron:0x000055f7859a1ca8 @original="* 8-17 * * 1-5", @cron_s=nil, @seconds=[0], @minutes=[0..59], @hours=[8..17], @monthdays=nil, @months=nil, @weekdays=[[1], [2], [3], [4], [5]], @zone=nil, @timezone=nil>

It might not be feasible to include every minute of every hour in the 8am-5pm range, but any help getting a string like this to parse would be very helpful. Many thanks for this gem, it truly is wonderful.

Context

Please replace the content of this section with the output of the following commands:

Linux a716f734f0ee 4.19.76-linuxkit #1 SMP Tue May 26 11:42:35 UTC 2020 x86_64 GNU/Linux

ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-linux]

[:env_tz, nil]

(secs:1601231894.3672342,utc~:"2020-09-27 18:38:14.3672342300415039",ltz~:"UTC")
(etz:nil,tnz:"UTC",tziv:"1.2.7",tzidv:nil,rv:"2.6.6",rp:"x86_64-linux",win:false,rorv:"6.0.3.3",astz:[ActiveSupport::TimeZone, "Etc/UTC"],eov:"1.2.4",eotnz:#<TZInfo::DataTimezone: Etc/UTC>,eotnfz:"+0000",eotlzn:"Etc/UTC",eotnfZ:"UTC",debian:"Etc/UTC",centos:nil,osx:"Etc/UTC")

Additional context

It turns out that parsing every monday to friday 8am and 5pm will correctly parse the day range supplied, but change the hours from a list; 8am and 5pm to a range; 8am to 5pm returns a nil.

I tried with multi: true and still get a nil returned.

jmettraux commented 3 years ago

Many thanks for this gem, it truly is wonderful.

Thanks, maybe it deserves a star.

jmettraux commented 3 years ago

Hello,

thanks for the feature request.

It turns out that parsing every monday to friday 8am and 5pm will correctly parse the day range supplied

require 'fugit'

p Fugit::Nat.parse('every monday to friday 8am and 5pm')
  # ==>
  #   #<Fugit::Cron:0x00000d59c7723430
  #      @original="0 8,17 * * 1-5", @cron_s=nil,
  #      @seconds=[0], @minutes=[0], @hours=[8, 17], @monthdays=nil,
  #      @months=nil, @weekdays=[[1], [2], [3], [4], [5]],
  #      @zone=nil, @timezone=nil>

This isn't a range, it's a parsed cron expression (potential point in time, not ranges in time).

every monday to friday 8am to 5pm returns a nil

Yes, because it doesn't understand such hour ranges.

I tried with multi: true and still get a nil returned.

multi: true is for cases where outputting a single Fugit::Cron instance isn't sufficient, for example:

Fugit::Nat.parse('every day at 16:15 and 18:30', multi: true)
  # ==> [
  #  #<Fugit::Cron:0x0000024a34af05e8 @original="15 16 * * *", @cron_s=nil,
  #    @seconds=[0], @minutes=[15], @hours=[16], @monthdays=nil, @months=nil,
  #    @weekdays=nil, @zone=nil, @timezone=nil>,
  #  #<Fugit::Cron:0x0000024a05183b38 @original="30 18 * * *", @cron_s=nil,
  #    @seconds=[0], @minutes=[30], @hours=[18], @monthdays=nil, @months=nil,
  #    @weekdays=nil, @zone=nil, @timezone=nil>]
Fugit::Nat.parse('every weekday 8am to 5pm')

I was expecting to get a fugit cron object back that looks a little something like this?

#<Fugit::Cron:0x000055f7859a1ca8 @original="* 8-17 * * 1-5" ...>`

It might not be feasible to include every minute of every hour in the 8am-5pm range, but any help getting a string like this to parse would be very helpful.

That I can do, "* 8-17 * * 1-5", monday to friday, every minute from 08:00 to 17:59. I can modify fugit to understand the to between "8am" and "5pm". But then, maybe, what you really mean is "8am until 5pm"?

There is currently no concept of time ranges in fugit. I am not sure if I have the spare time to add that and then maintain that for the next ten years.

Should "every weekday 8am to 5pm" sounds neat, but for some people (like you) it should parse to a time range, for others it should parse to "every weekday, 08:00, 09:00, 10:00, ..., 17:00", for others it should parse to " 8-17 * 1-5".

Maybe:

I'll think about it.

gee-forr commented 3 years ago

Hi @jmettraux

Sure - totally understand that cron is about points in time, not ranges. Being able to parse every weekday 8am to 5pm would be perfect.

P.S - I always forget about starring projects I use and admire, thanks for the reminder :)

jmettraux commented 3 years ago

P.S - I always forget about starring projects I use and admire, thanks for the reminder :)

Thanks a lot! It helps!

jmettraux commented 3 years ago

@gee-forr

Hello,

what do you think of this piece of spec:

  {
                      'every weekday 8am to 5pm'  =>  '0 8-17 * * 1-5',
          'every weekday 8am to 5pm on the hour'  =>  '0 8-17 * * 1-5',
        'every weekday 8am to 5pm on the minute'  =>  '* 8-16 * * 1-5',
  'every weekday 8am to 5pm on minute 10 and 30'  =>  '10,30 8-16 * * 1-5',
                        'every hour, 8am to 5pm'  =>  '0 8-17 * * *',
                   'every hour, from 8am to 5pm'  =>  '0 8-17 * * *',
                      'every minute, 8am to 5pm'  =>  '* 8-16 * * *',
                  'every minute from 8am to 5pm'  =>  '* 8-16 * * *',
  }

?

jmettraux commented 3 years ago

Closing.