diff --git a/README.md b/README.md index 28685a7..fb4de04 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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', diff --git a/fugit.gemspec b/fugit.gemspec index 8f8cb6b..e2f3659 100644 --- a/fugit.gemspec +++ b/fugit.gemspec @@ -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' diff --git a/lib/fugit/duration.rb b/lib/fugit/duration.rb index bdc3420..17cbb32 100644 --- a/lib/fugit/duration.rb +++ b/lib/fugit/duration.rb @@ -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 diff --git a/spec/cron_spec.rb b/spec/cron_spec.rb index 0025b1b..e2471dd 100644 --- a/spec/cron_spec.rb +++ b/spec/cron_spec.rb @@ -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 diff --git a/tst/ranges.rb b/tst/ranges.rb new file mode 100644 index 0000000..82894c3 --- /dev/null +++ b/tst/ranges.rb @@ -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) + diff --git a/tst/sao_paulo.rb b/tst/sao_paulo.rb new file mode 100644 index 0000000..1757545 --- /dev/null +++ b/tst/sao_paulo.rb @@ -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 +