Skip to content

Commit

Permalink
Merge branch 'master' into tilde
Browse files Browse the repository at this point in the history
  • Loading branch information
jmettraux committed Mar 13, 2024
2 parents 1a87bda + 753da8e commit 409873f
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ c = Fugit.parse_cron('0 12 * * mon#2')

# `#next` and `#prev` return Enumerable instances
#
# These two methods are available since fugit 1.10.0.
#
c.next(Time.parse('2024-02-16 12:00:00'))
.take(3)
.map(&:to_s)
Expand All @@ -157,6 +159,8 @@ c.prev(Time.parse('2024-02-16 12:00:00'))
# instances that correspond to the occurrences of the cron within
# the time range
#
# This method is available since fugit 1.10.0.
#
c.within(Time.parse('2024-02-16 12:00')..Time.parse('2024-08-01 12:00'))
.map(&:to_s)
# => [ '2024-03-11 12:00:00',
Expand Down
2 changes: 1 addition & 1 deletion fugit.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Time tools for flor and the floraison project. Cron parsing and occurrence compu
# this dependency appears in 'et-orbi'

s.add_runtime_dependency 'raabro', '~> 1.4'
s.add_runtime_dependency 'et-orbi', '~> 1', '>= 1.2.7'
s.add_runtime_dependency 'et-orbi', '~> 1', '>= 1.2.9'

s.add_development_dependency 'rspec', '~> 3.8'
s.add_development_dependency 'chronic', '~> 0.10'
Expand Down
2 changes: 1 addition & 1 deletion lib/fugit/duration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def add(a)
when Numeric then add_numeric(a)
when Fugit::Duration then add_duration(a)
when String then add_duration(self.class.parse(a))
when ::Time, EtOrbi::EoTime then add_to_time(a)
when ::Time, ::EtOrbi::EoTime then add_to_time(a)
else fail ArgumentError.new(
"cannot add #{a.class} instance to a Fugit::Duration")
end
Expand Down
35 changes: 23 additions & 12 deletions spec/cron_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -886,28 +886,39 @@ class Fugit::Cron::TimeCursor
# 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.

[ '21 0 * * 1%2 America/Sao_Paulo',
'2024-03-04 04:21:00', '2024-03-13 12:00', 'Europe/Paris' ],
[ '21 0 * * 1%2 America/Santarem',
'2024-03-04 04:21:00', '2024-03-13 12:00', 'Europe/Paris' ],
]

describe '#previous_time' do

PREVIOUS_TIMES.each do |cron, previous_time, now|
PREVIOUS_TIMES.each do |cron, previous_time, now, zone_name|

now = now ? Time.parse(now) : NOW
d = "succeeds #{cron.inspect} #{now} -> #{previous_time.inspect}"
d = d += " in #{zone_name}" if zone_name

it "succeeds #{cron.inspect} #{now} -> #{previous_time.inspect}" do
it(d) do

c = Fugit::Cron.parse(cron)
ept = Time.parse(previous_time)
in_zone(zone_name) do

pt = c.previous_time(now)
now = now ? Time.parse(now) : NOW

expect(
Fugit.time_to_plain_s(pt, false)
).to eq(
Fugit.time_to_plain_s(ept, false)
)
c = Fugit::Cron.parse(cron)
ept = Time.parse(previous_time)

expect(c.match?(ept)).to eq(true) # quick check
pt = c.previous_time(now)

expect(
Fugit.time_to_plain_s(pt, false)
).to eq(
Fugit.time_to_plain_s(ept, false)
)

expect(c.match?(ept)).to eq(true) # quick check
end
end
end

Expand Down
52 changes: 52 additions & 0 deletions tst/ranges.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

#
# exploring ranges...

# 0-59
# 10~30

class R
include Comparable

MAX = 59
attr_reader :current, :type, :slash

def initialize(current, type, slash)
@current = current
@type = type
@slash = slash
end

def to_i
@current
end

def <=>(b)
current <=> b.to_i
end
def succ
c = current
loop do
c = c.succ
return R.new(c, type, slash) if (c % slash) == 0
fail 'overflow' if c > MAX
end
end
end

p (R.new(0, :hyphen, 1)..R.new(59, :hyphen, 1)).to_a.size

range = R.new(0, :hyphen, 2)..R.new(59, :hyphen, 2)
p range.to_a.size

p range.include?(0)
p range.include?(1)
p range.include?(2)
p range.include?(100)

p range.cover?(R.new(10, :hyphen, 2)..R.new(20, :hyphen, 2))
p range.cover?(R.new(10, :hyphen, 2)..R.new(70, :hyphen, 2))
p range.cover?(R.new(10, :hyphen, 1)..R.new(20, :hyphen, 1))

p range.to_a.collect(&:to_i)

65 changes: 65 additions & 0 deletions tst/sao_paulo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

require 'fugit'

def test(x)
puts
puts " --- #{x} ---"
c = Fugit.parse_cron(x)
begin
puts c.previous_time.strftime('%F %T %:z %A')
rescue => err
p err
end
#begin
# puts c.next_time.strftime('%F %T %:z %A')
#rescue => err
# p err
#end
end

##Fugit.parse_cron('21 0 * * 1%2')
#test('21 0 * * 1%2')
#test('21 0 * * 1%1')
#
## minute hour day-of-month month day-of-week [flags] command
#
#test('21 0 * * 1%2 UTC')
#test('21 0 * * 1%2 America/Chicago')
#test('21 0 * * 1%2 America/New_York')
#
##test('21 0 * * 1%2 America/Rio_Branco')
##test('21 0 * * 1%2 America/Manaus')
##test('21 0 * * 1%2 America/Belem')
##test('21 0 * * 1%2 America/Fortaleza')
##test('21 0 * * 1%2 America/Recife')
##test('21 0 * * 1%2 America/Araguaina')
##test('21 0 * * 1%2 America/Maceio')
#test('21 0 * * 1%2 America/Bahia')
#test('21 0 * * 1%2 America/Sao_Paulo')
#test('21 0 * * 1%2 America/Campo_Grande')
#test('21 0 * * 1%2 America/Cuiaba')
#test('21 0 * * 1%2 America/Santarem')
##test('21 0 * * 1%2 America/Porto_Velho')
##test('21 0 * * 1%2 America/Boa_Vista')
##test('21 0 * * 1%2 America/Manaus')
##test('21 0 * * 1%2 America/Eirunepe')
##test('21 0 * * 1%2 America/Rio_Branco')

if (ARGV[0] || '').match?(/p/)
test('21 0 * * 1%2 America/Sao_Paulo')
else
test('21 0 * * 1%2 America/Santarem')
end

puts

# paulo
#["2024-03-04 23:59:59 -0300", 271, "--", 1, "==", 0, "-->", false]
# santarem
#["2024-03-04 23:59:59 -0300", 270, "--", 0, "==", 0, "-->", true]

#p EtOrbi.make_time('2019-01-01 00:00:00', 'America/Sao_Paulo')
#p EtOrbi.make_time('2019-01-01 00:00:00', 'America/Santarem')
#p EtOrbi.make_time('2019-01-01 00:00:00', 'America/Sao_Paulo').to_s
#p EtOrbi.make_time('2019-01-01 00:00:00', 'America/Santarem').to_s

0 comments on commit 409873f

Please sign in to comment.