Skip to content

Commit

Permalink
Solve 'every day at 12:15 am' issue, gh-81
Browse files Browse the repository at this point in the history
  • Loading branch information
jmettraux committed Nov 3, 2022
1 parent d2b5d23 commit bbc4f8b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
39 changes: 26 additions & 13 deletions lib/fugit/nat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def on_objects(i)
#'every week on monday 18:23' => '23 18 * * 1',
#
# every month on the 1st
#
def on(i)
seq(:on, i, :_on, :on_objects)
end
Expand Down Expand Up @@ -452,20 +453,24 @@ def rewrite_every_named(t)
end

def rewrite_tz(t)

slot(:tz, t.string)
end

def rewrite_weekday(t)

Fugit::Cron::Parser::WEEKDS.index(t.string[0, 3].downcase)
end

def rewrite_weekdays(t)

#Raabro.pp(t, colours: true)
slot(:weekday, _rewrite_subs(t, :weekday))
end
alias rewrite_on_weekdays rewrite_weekdays

def rewrite_to_weekday(t)

wd0, wd1 = _rewrite_subs(t, :weekday)
#wd1 = 7 if wd1 == 0
slot(:weekday, "#{wd0}-#{wd1}")
Expand All @@ -476,25 +481,35 @@ def rewrite_to_omonthday(t)
slot(:monthday, "#{md0}-#{md1}")
end

def adjust_h(h, ap)
h = h.to_i
case ap
when 'pm' then h < 12 ? h + 12 : h
when 'midnight' then h + 12
else h
# Try to follow https://en.wikipedia.org/wiki/12-hour_clock#Confusion_at_noon_and_midnight
#
def adjust_h(h, m, ap)

if ap == 'midnight' && h == 12
24
elsif ap == 'pm' && h < 12 # post meridian
h + 12
elsif ap == 'am' && h == 12 # ante meridian
0
else
h
end
end

def rewrite_digital_hour(t)
h, m = t.sublookup(:digital_h).strinpd.split(':')
ap = t.sublookup(:ampm); ap = ap && ap.strinpd
h, m = adjust_h(h, ap), m.to_i

h, m = t.sublookup(:digital_h).strinpd.split(':').collect(&:to_i)
ap = t.sublookup(:ampm)
h, m = adjust_h(h, m, ap && ap.strinpd), m

slot(:hm, h, m)
end

def rewrite_simple_hour(t)

h, ap = t.subgather(nil).collect(&:strinpd)
h = adjust_h(h, ap)
h = adjust_h(h.to_i, 0, ap)

slot(:hm, h, 0)
end

Expand All @@ -506,12 +521,10 @@ def rewrite_named_hour(t)

h = ht.strinp
m = mt ? mt.strinp : 0
#p [ 0, '-->', h, m ]
h = NHOURS[h]
m = NMINUTES[m] || m
#p [ 1, '-->', h, m ]

h = adjust_h(h, apt && apt.strinpd)
h = adjust_h(h, m, apt && apt.strinpd)

slot(:hm, h, m)
end
Expand Down
24 changes: 21 additions & 3 deletions spec/nat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,27 @@
#
# gh-42

'every day at 5pm' => '0 17 * * *',
'every day at 5:00pm' => '0 17 * * *',
'every day at 5:00 pm' => '0 17 * * *',
'every day at 5pm' => '0 17 * * *',
'every day at 5:00pm' => '0 17 * * *',
'every day at 5:00 pm' => '0 17 * * *',
#
'every day at 12am' => '0 0 * * *',
'every day at 12pm' => '0 12 * * *',
'every day at 12:00am' => '0 0 * * *',
'every day at 12:00pm' => '0 12 * * *',
'every day at 12:00 am' => '0 0 * * *',
'every day at 12:00 pm' => '0 12 * * *',
'every day at 12:15am' => '15 0 * * *',
'every day at 12:15pm' => '15 12 * * *',
'every day at 12:15 am' => '15 0 * * *',
'every day at 12:15 pm' => '15 12 * * *',
#
'every day at 12 noon' => '0 12 * * *',
'every day at 12 midnight' => '0 24 * * *',
'every day at 12:00 noon' => '0 12 * * *',
'every day at 12:00 midnight' => '0 24 * * *',
'every day at 12:15 noon' => '15 12 * * *',
'every day at 12:15 midnight' => '15 24 * * *',
#
# gh-81

Expand Down

0 comments on commit bbc4f8b

Please sign in to comment.