fugit 1.8.1 → 1.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b1839fa07acc6287be95f8076c19afa2c9d6c9b5500ac1b03febab61959b5057
4
- data.tar.gz: 839ff5c0f85ccd51625febfdbfedd017e46962d288cf313307b706830dc838c9
3
+ metadata.gz: d32bc8cbff08c0ca8fe2c2037454ca76c208878f26d71398cd46a93729958248
4
+ data.tar.gz: ce7d21ce4265c59def186f0e28271def0b74724c99c46814443182039f26cba1
5
5
  SHA512:
6
- metadata.gz: 5336bf251441fa3f7372d48aec2196cf86831b24c64110955f2ccf2df2e6e1ec84a68b318b53be8282f87794f40c09778bddd47abec3f65fe1469544a758fabb
7
- data.tar.gz: 0fdd70455097838c21c3f03c9dca20889b4aba07f67d790136e40be2d8686b0a6ad8a594fff1fe28e7cd4d322ad66a8a31aa71750cd435c7601345487ae89bdb
6
+ metadata.gz: fe8facb5f36dd89ec4e03994c6474f3a6e8de371f05322b8c7983d2d9d8769889b475e35a2d0bcc74fb04b59f96ca857a16ffb6a3db50db41a36beff48c57c39
7
+ data.tar.gz: d928deee88172c20c15e6960bddfd30b77f3eb7fb0e0edd322baa9ce54932fe476afc7a21985fe3cc65dff3507acd29540f88b51ae27f3e458d060f083a11628
data/CHANGELOG.md CHANGED
@@ -2,6 +2,19 @@
2
2
  # CHANGELOG.md
3
3
 
4
4
 
5
+ ## fugit 1.10.0 released 2024-02-22
6
+
7
+ * Implement `Fugit::Cron#within(time_start, time_end)`
8
+ * Implement `Fugit::Cron#within(time_range)`
9
+ * Implement iterator-returning `Fugit::Cron#next` and `#prev`
10
+
11
+
12
+ ## fugit 1.9.0 released 2023-10-24
13
+
14
+ * Let nat parse "last", gh-88
15
+ * Change that I am not sure about, gh-86
16
+
17
+
5
18
  ## fugit 1.8.1 released 2023-01-20
6
19
 
7
20
  * Fix for month subtraction, gh-84, @mreinsch
data/CREDITS.md CHANGED
@@ -1,6 +1,7 @@
1
1
 
2
2
  # fugit credits
3
3
 
4
+ * Marcos Belluci, https://github.com/delbetu, gh-88 , 1st and last nat
4
5
  * Michael Reinsch, https://github.com/mreinsch, gh-84 and gh-85
5
6
  * Marc Anguera, https://github.com/markets, gh-70 and Sidekiq-Cron
6
7
  * ski-nine, https://github.com/ski-nine, gh-81
data/LICENSE.txt CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
- Copyright (c) 2017-2023, John Mettraux, jmettraux+flor@gmail.com
2
+ Copyright (c) 2017-2024, John Mettraux, jmettraux+flor@gmail.com
3
3
 
4
4
  Permission is hereby granted, free of charge, to any person obtaining a copy
5
5
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -3,7 +3,6 @@
3
3
 
4
4
  [![tests](https://github.com/floraison/fugit/workflows/test/badge.svg)](https://github.com/floraison/fugit/actions)
5
5
  [![Gem Version](https://badge.fury.io/rb/fugit.svg)](http://badge.fury.io/rb/fugit)
6
- [![Join the chat at https://gitter.im/floraison/fugit](https://badges.gitter.im/floraison/fugit.svg)](https://gitter.im/floraison/fugit?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
7
6
 
8
7
  Time tools for [flor](https://github.com/floraison/flor) and the floraison group.
9
8
 
@@ -27,17 +26,21 @@ The intersection of those two projects is where fugit is born:
27
26
  * [parse-cron](https://github.com/siebertm/parse-cron) - parses cron expressions and calculates the next occurrence after a given date
28
27
  * [ice_cube](https://github.com/seejohnrun/ice_cube) - Ruby date recurrence library
29
28
  * [ISO8601](https://github.com/arnau/ISO8601) - Ruby parser to work with ISO8601 dateTimes and durations
29
+ * [chrono](https://github.com/r7kamura/chrono) - a chain of logics about chronology
30
+ * [CronCalc](https://github.com/mizinsky/cron_calc) - calculates cron job occurrences
31
+ * [Recurrence](https://github.com/fnando/recurrence) - a simple library to handle recurring events
30
32
  * ...
31
33
 
32
34
  ### Projects using fugit
33
35
 
34
36
  * [arask](https://github.com/Ebbe/arask) - "Automatic RAils taSKs" uses fugit to parse cron strings
35
37
  * [sidekiq-cron](https://github.com/ondrejbartas/sidekiq-cron) - recent versions of Sidekiq-Cron use fugit to parse cron strings
36
- * [rufus-scheduler](https://github.com/jmettraux/rufus-scheduler) -
38
+ * [rufus-scheduler](https://github.com/jmettraux/rufus-scheduler) - as seen above
37
39
  * [flor](https://github.com/floraison/flor) - used in the [cron](https://github.com/floraison/flor/blob/master/doc/procedures/cron.md) procedure
38
40
  * [que-scheduler](https://github.com/hlascelles/que-scheduler) - a reliable job scheduler for [que](https://github.com/chanks/que)
39
41
  * [serial_scheduler](https://github.com/grosser/serial_scheduler) - ruby task scheduler without threading
40
42
  * [delayed_cron_job](https://github.com/codez/delayed_cron_job) - an extension to Delayed::Job that allows you to set cron expressions for your jobs
43
+ * [GoodJob](https://github.com/bensheldon/good_job) - a multithreaded, Postgres-based, Active Job backend for Ruby on Rails
41
44
  * ...
42
45
 
43
46
  ## `Fugit.parse(s)`
@@ -93,7 +96,7 @@ As `Fugit.parse(s)` returns nil when it doesn't grok its input, and `Fugit.do_pa
93
96
 
94
97
  Sometimes you know a cron expression or an "every" natural expression will come in and you want to discard the rest.
95
98
 
96
- ```
99
+ ```ruby
97
100
  require 'fugit'
98
101
 
99
102
  Fugit.parse_cronish('0 0 1 jan *').class # ==> ::Fugit::Cron
@@ -123,8 +126,44 @@ c = Fugit::Cron.new('0 0 * * sun')
123
126
 
124
127
  p Time.now # => 2017-01-03 09:53:27 +0900
125
128
 
126
- p c.next_time # => 2017-01-08 00:00:00 +0900
127
- p c.previous_time # => 2017-01-01 00:00:00 +0900
129
+ p c.next_time.to_s # => 2017-01-08 00:00:00 +0900
130
+ p c.previous_time.to_s # => 2017-01-01 00:00:00 +0900
131
+
132
+ p c.next_time(Time.parse('2024-06-01')).to_s
133
+ # => "2024-06-02 00:00:00 +0900"
134
+ p c.previous_time(Time.parse('2024-06-01')).to_s
135
+ # => "2024-05-26 00:00:00 +0900"
136
+ #
137
+ # `Fugit::Cron#next_time` and `#previous_time` accept a "start time"
138
+
139
+ c = Fugit.parse_cron('0 12 * * mon#2')
140
+
141
+ # `#next` and `#prev` return Enumerable instances
142
+ #
143
+ c.next(Time.parse('2024-02-16 12:00:00'))
144
+ .take(3)
145
+ .map(&:to_s)
146
+ # => [ '2024-03-11 12:00:00',
147
+ # '2024-04-08 12:00:00',
148
+ # '2024-05-13 12:00:00' ]
149
+ c.prev(Time.parse('2024-02-16 12:00:00'))
150
+ .take(3)
151
+ .map(&:to_s)
152
+ # => [ '2024-02-12 12:00:00',
153
+ # '2024-01-08 12:00:00',
154
+ # '2023-12-11 12:00:00' ]
155
+
156
+ # `#within` accepts a time range and returns an array of Eo::EoTime
157
+ # instances that correspond to the occurrences of the cron within
158
+ # the time range
159
+ #
160
+ c.within(Time.parse('2024-02-16 12:00')..Time.parse('2024-08-01 12:00'))
161
+ .map(&:to_s)
162
+ # => [ '2024-03-11 12:00:00',
163
+ # '2024-04-08 12:00:00',
164
+ # '2024-05-13 12:00:00',
165
+ # '2024-06-10 12:00:00',
166
+ # '2024-07-08 12:00:00' ]
128
167
 
129
168
  p c.brute_frequency # => [ 604800, 604800, 53 ]
130
169
  # [ delta min, delta max, occurrence count ]
@@ -269,6 +308,18 @@ c.match?('2019-01-08') # => false, since (rweek + 1) % 2 == 1
269
308
  `tue%x+y` matches if Tuesday and `current_date.rweek + y % x == 0`
270
309
 
271
310
 
311
+ ### the second extension
312
+
313
+ Fugit accepts cron strings with five elements, `minute hour day-of-month month day-of-week`, the standard cron format or six elements `second minute hour day-of-month month day-of-week`.
314
+
315
+ ```ruby
316
+ c = Fugit.parse('* * * * *') # every minute
317
+ c = Fugit.parse('5 * * * *') # every hour at minute 5
318
+ c = Fugit.parse('* * * * * *') # every second
319
+ c = Fugit.parse('5 * * * * *') # every minute at second 5
320
+ ```
321
+
322
+
272
323
  ## `Fugit::Duration`
273
324
 
274
325
  A class `Fugit::Duration` to parse duration strings (vanilla [rufus-scheduler](https://github.com/jmettraux/rufus-scheduler) ones and [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) ones).
@@ -402,7 +453,8 @@ Fugit::Nat.parse('every day at 16:15 and 18:30', multi: true)
402
453
  # ==> [ '15 16 * * *', '30 18 * * *' ] (two Fugit::Cron instances)
403
454
 
404
455
  Fugit::Nat.parse('every day at 16:15 and 18:30', multi: :fail)
405
- # ==> ArgumentError: multiple crons in "every day at 16:15 and 18:30" (15 16 * * * | 30 18 * * *)
456
+ # ==> ArgumentError: multiple crons in "every day at 16:15 and 18:30"
457
+ # (15 16 * * * | 30 18 * * *)
406
458
  Fugit::Nat.parse('every day at 16:15 nada 18:30', multi: true)
407
459
  # ==> nil
408
460
  ```
data/lib/fugit/cron.rb CHANGED
@@ -40,8 +40,6 @@ module Fugit
40
40
  #p s; Raabro.pp(Parser.parse(s, debug: 3), colors: true)
41
41
  h = Parser.parse(s.strip)
42
42
 
43
- return nil unless h
44
-
45
43
  self.allocate.send(:init, s, h)
46
44
  end
47
45
 
@@ -320,6 +318,53 @@ module Fugit
320
318
  t.time.translate(from.zone)
321
319
  end
322
320
 
321
+ # Used by Fugit::Cron#next and Fugit::Cron#prev
322
+ #
323
+ class CronIterator include ::Enumerable
324
+ attr_reader :cron, :start, :current, :direction
325
+ def initialize(cron, direction, start)
326
+ @cron = cron
327
+ @start = start
328
+ @current = start.dup
329
+ @direction = direction
330
+ end
331
+ def each
332
+ loop do
333
+ yield(@current = @cron.send(@direction, @current))
334
+ end
335
+ end
336
+ end
337
+
338
+ # Returns an ::Enumerable instance that yields each "next time" in
339
+ # succession
340
+ #
341
+ def next(from=::EtOrbi::EoTime.now)
342
+
343
+ CronIterator.new(self, :next_time, from)
344
+ end
345
+
346
+ # Returns an ::Enumerable instance that yields each "previous time" in
347
+ # succession
348
+ #
349
+ def prev(from=::EtOrbi::EoTime.now)
350
+
351
+ CronIterator.new(self, :previous_time, from)
352
+ end
353
+
354
+ # Returns an array of EtOrbi::EoTime instances that correspond to
355
+ # the occurrences of the cron within the given time range
356
+ #
357
+ def within(time_range, time_end=nil)
358
+
359
+ sta, ned =
360
+ time_range.is_a?(::Range) ? [ time_range.begin, time_range.end ] :
361
+ [ ::EtOrbi.make_time(time_range), ::EtOrbi.make_time(time_end) ]
362
+
363
+ CronIterator
364
+ .new(self, :next_time, sta)
365
+ .take_while { |eot| eot.to_t < ned }
366
+ end
367
+
323
368
  # Mostly used as a #next_time sanity check.
324
369
  # Avoid for "business" use, it's slow.
325
370
  #
@@ -488,18 +533,22 @@ module Fugit
488
533
 
489
534
  def init(original, h)
490
535
 
536
+ return nil unless h
537
+
491
538
  @original = original
492
539
  @cron_s = nil # just to be sure
493
540
  @day_and = h[:&]
494
541
 
495
- determine_seconds(h[:sec])
496
- determine_minutes(h[:min])
497
- determine_hours(h[:hou])
498
- determine_monthdays(h[:dom])
499
- determine_months(h[:mon])
500
- determine_weekdays(h[:dow])
501
- determine_timezone(h[:tz])
542
+ valid =
543
+ determine_seconds(h[:sec]) &&
544
+ determine_minutes(h[:min]) &&
545
+ determine_hours(h[:hou]) &&
546
+ determine_monthdays(h[:dom]) &&
547
+ determine_months(h[:mon]) &&
548
+ determine_weekdays(h[:dow]) &&
549
+ determine_timezone(h[:tz])
502
550
 
551
+ return nil unless valid
503
552
  return nil unless compact_month_days
504
553
 
505
554
  self
@@ -509,10 +558,12 @@ module Fugit
509
558
 
510
559
  sta, edn, sla = r
511
560
 
561
+ return false if sla && sla > max
562
+
512
563
  edn = max if sla && edn.nil?
513
564
 
514
- return [ nil ] if sta.nil? && edn.nil? && sla.nil?
515
- return [ sta ] if sta && edn.nil?
565
+ return nil if sta.nil? && edn.nil? && sla.nil?
566
+ return sta if sta && edn.nil?
516
567
 
517
568
  sla = 1 if sla == nil
518
569
  sta = min if sta == nil
@@ -563,42 +614,41 @@ module Fugit
563
614
  .uniq
564
615
  end
565
616
 
566
- def compact(key)
617
+ def do_determine(key, arr, min, max)
567
618
 
568
- arr = instance_variable_get(key)
619
+ null = false
569
620
 
570
- return instance_variable_set(key, nil) if arr.include?(nil)
571
- # reductio ad astrum
621
+ r = arr
622
+ .collect { |v|
623
+ expand(min, max, v) }
624
+ .flatten(1)
625
+ .collect { |e|
626
+ return false if e == false
627
+ null = null || e == nil
628
+ (key == :hours && e == 24) ? 0 : e }
572
629
 
573
- arr.uniq!
574
- arr.sort!
630
+ return nil if null
631
+ r.uniq.sort
575
632
  end
576
633
 
577
634
  def determine_seconds(arr)
578
- @seconds = (arr || [ 0 ]).inject([]) { |a, s| a.concat(expand(0, 59, s)) }
579
- compact(:@seconds)
635
+ (@seconds = do_determine(:seconds, arr || [ 0 ], 0, 59)) != false
580
636
  end
581
637
 
582
638
  def determine_minutes(arr)
583
- @minutes = arr.inject([]) { |a, m| a.concat(expand(0, 59, m)) }
584
- compact(:@minutes)
639
+ (@minutes = do_determine(:minutes, arr, 0, 59)) != false
585
640
  end
586
641
 
587
642
  def determine_hours(arr)
588
- @hours = arr
589
- .inject([]) { |a, h| a.concat(expand(0, 23, h)) }
590
- .collect { |h| h == 24 ? 0 : h }
591
- compact(:@hours)
643
+ (@hours = do_determine(:hours, arr, 0, 23)) != false
592
644
  end
593
645
 
594
646
  def determine_monthdays(arr)
595
- @monthdays = arr.inject([]) { |a, d| a.concat(expand(1, 31, d)) }
596
- compact(:@monthdays)
647
+ (@monthdays = do_determine(:monthdays, arr, 1, 31)) != false
597
648
  end
598
649
 
599
650
  def determine_months(arr)
600
- @months = arr.inject([]) { |a, m| a.concat(expand(1, 12, m)) }
601
- compact(:@months)
651
+ (@months = do_determine(:months, arr, 1, 12)) != false
602
652
  end
603
653
 
604
654
  def determine_weekdays(arr)
@@ -624,11 +674,15 @@ module Fugit
624
674
  @weekdays.uniq!
625
675
  @weekdays.sort!
626
676
  @weekdays = nil if @weekdays.empty?
677
+
678
+ true
627
679
  end
628
680
 
629
681
  def determine_timezone(z)
630
682
 
631
683
  @zone, @timezone = z
684
+
685
+ true
632
686
  end
633
687
 
634
688
  def weekdays_to_cron_s
data/lib/fugit/nat.rb CHANGED
@@ -642,9 +642,9 @@ module Fugit
642
642
  fail(ArgumentError.new(
643
643
  "multiple crons in #{opts[:_s].inspect} - #{@slots.inspect}"))
644
644
  elsif multi == true
645
- hms.collect { |hm| parse_cron(hm) }
645
+ hms.collect { |hm| parse_cron(hm, opts) }
646
646
  else
647
- parse_cron(hms.first)
647
+ parse_cron(hms.first, opts)
648
648
  end
649
649
  end
650
650
 
@@ -678,7 +678,7 @@ module Fugit
678
678
  .to_a
679
679
  end
680
680
 
681
- def parse_cron(hm)
681
+ def parse_cron(hm, opts)
682
682
 
683
683
  a = [
684
684
  slot(:second, '0'),
@@ -691,11 +691,39 @@ module Fugit
691
691
  a << tz.data0 if tz
692
692
  a.shift if a.first == [ '0' ]
693
693
 
694
+ letters_last = lambda { |x| x.is_a?(Numeric) ? x : 999_999 }
695
+
694
696
  s = a
695
- .collect { |e| e.uniq.sort.collect(&:to_s).join(',') }
697
+ .collect { |e|
698
+ e.uniq.sort_by(&letters_last).collect(&:to_s).join(',') }
696
699
  .join(' ')
697
700
 
698
- Fugit::Cron.parse(s)
701
+ c = Fugit::Cron.parse(s)
702
+
703
+ if opts[:strict]
704
+ restrict(a, c)
705
+ else
706
+ c
707
+ end
708
+ end
709
+
710
+ # Return nil if the cron is "not strict"
711
+ #
712
+ # For example, "0 0/17 * * *" (gh-86) is a perfectly valid
713
+ # cron string, but makes not much sense when derived via `.parse_nat`
714
+ # from "every 17 hours".
715
+ #
716
+ # It happens here because it's nat being strict, not cron.
717
+ #
718
+ def restrict(a, cron)
719
+
720
+ if m = ((a[1] && a[1][0]) || '').match(/^(\d+|\*)\/(\d+)$/)
721
+ #p m
722
+ sla = m[1].to_i
723
+ return nil unless [ 1, 2, 3, 4, 5, 6, 8, 12 ].include?(sla)
724
+ end
725
+
726
+ cron
699
727
  end
700
728
 
701
729
  def slot(key, default)
data/lib/fugit.rb CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  module Fugit
5
5
 
6
- VERSION = '1.8.1'
6
+ VERSION = '1.10.0'
7
7
  end
8
8
 
9
9
  require 'time'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fugit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.1
4
+ version: 1.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Mettraux
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-20 00:00:00.000000000 Z
11
+ date: 2024-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: raabro
@@ -116,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
116
  - !ruby/object:Gem::Version
117
117
  version: '0'
118
118
  requirements: []
119
- rubygems_version: 3.1.6
119
+ rubygems_version: 3.2.33
120
120
  signing_key:
121
121
  specification_version: 4
122
122
  summary: time tools for flor