cron_calc 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: df5dd3a2f576d78996d783668af59a058a4fd9256072dfa25b84760d64e09797
4
- data.tar.gz: 922e53ae27d369035d3b7fff81e1ba624756fa365c30694e6871abc54421473e
3
+ metadata.gz: 29ac47b06a1dc188f3fb9ed5c10f9fa1a6ef748e8d15a6783eb7b8f99de85b38
4
+ data.tar.gz: 8f136ff6c1010357acd75942e80bc7d4a2f4b0d1b8555041486f1cc9bdd97f00
5
5
  SHA512:
6
- metadata.gz: 85a0d0ea11962ee78495e6c6c8ee40ea9695d20df2635efbb67cdc32502086032b3aa85aaa419b39cc1a736a31c6d1ecc577afd60b571d4e8d120c5adcd922fc
7
- data.tar.gz: aab193b3c8afe9551dd46fe1b9e0061702cc69e3fa739c08c774d5f77ef40dabbd5c90c816b93a1e864d2e05c6c5278eda102c07649c83afd3a43553c5cc5ef8
6
+ metadata.gz: 38f8b7a5a0f80d10f6c4867a8a2e687b9ebb508c5e79031856cbb9bc81a9a97b6f399e0b37f332f5ec5aeb7d2839e29d11b6e76b0fdc5601b675a6a5ed82e806
7
+ data.tar.gz: d1545c32cd0559370fc1778c3517567ee0be5f4fecf41ad32af0a2a3fd3202d52f0b9d5014f202ab736c8174675d2c7692610fa72968f247022082aa7a2b5567
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## [0.4.0] - 2023-12-22
2
+
3
+ - Added support for named months and wdays
4
+ - Keyword arguments: before:, after:
5
+
6
+ ## [0.3.0] - 2023-12-22
7
+
8
+ - Added support for DOWs
9
+
1
10
  ## [0.2.0] - 2023-12-19
2
11
 
3
12
  - Added #next method
data/README.md CHANGED
@@ -21,9 +21,10 @@ Now, you can use one of three methods `#in`, `#next`, `#last` to determine cron
21
21
 
22
22
  ### Using `#in`
23
23
 
24
- Calculates cron job occurrences within a given time period.
25
- @param period [Range] The time period for which to calculate cron job occurrences.
26
- @return [Array<Time>] An array of Time instances representing each occurrence.
24
+ Calculates cron job occurrences within a given time period.\
25
+ **Parameters:**
26
+ - `period` - a Range object defining the start and end times for the calculation.\
27
+
27
28
 
28
29
  ```ruby
29
30
  period = Time.new(2024, 1, 1, 0, 0)..Time.new(2024, 1, 4, 0, 0)
@@ -34,11 +35,11 @@ Calculates cron job occurrences within a given time period.
34
35
 
35
36
  ### Using `#next`
36
37
 
37
- Calculates the next 'n' occurrences of the cron job from a given start time.
38
- @param count [Integer] The number of occurrences to calculate. Defaults to `1`.
39
- @param period_start [Time] The start time from which to calculate occurrences. Defaults to `Time.now`.
40
- @param max_years [Integer] The maximum number of years to consider for the period. Defaults to `5`.
41
- @return [Array<Time>] An array of the next 'n' occurrences.
38
+ Calculates the next 'n' occurrences of the cron job from a given start time.\
39
+ **Parameters:**
40
+ - `count` - (optional, Integer) The number of occurrences to calculate. Defaults to 1.
41
+ - `after:` - (optional, Time, keyword argument) The start time from which to calculate occurrences. If not provided, defaults to the current time (Time.now).
42
+ - `max_years` - (optional, Integer, keyword argument) The maximum number of years to search for future occurrences. Defaults to 5.
42
43
 
43
44
  ```ruby
44
45
  cron_calc.next
@@ -47,30 +48,28 @@ Calculates the next 'n' occurrences of the cron job from a given start time.
47
48
  cron_calc.next(3)
48
49
  # => [2023-12-20 05:05:00 +0100, 2023-12-21 05:05:00 +0100, 2023-12-22 05:05:00 +0100]
49
50
 
50
- cron_calc.next(2, Time.new(2024, 1, 1, 0, 0))
51
+ cron_calc.next(2, after: Time.new(2024, 1, 1, 0, 0))
51
52
  # => [2024-01-01 05:05:00 +0100, 2024-01-02 05:05:00 +0100]
52
53
  ```
53
54
 
54
55
  ### Using `#last`
55
56
 
56
- Calculates the last 'n' occurrences of the cron job until a given end time.
57
- @param count [Integer] The number of past occurrences to calculate.
58
- @param period_end [Time] The end time until which to calculate occurrences.
59
- @param max_years [Integer] The maximum number of years to consider for the period.
60
- @return [Array<Time>] An array of the last 'n' occurrences.
57
+ Calculates the last 'n' occurrences of the cron job until a given end time.\
58
+ **Parameters:**
59
+ - `count` - (optional, Integer) The number of occurrences to calculate. Defaults to 1.
60
+ - `before:` - (optional, Time, keyword argument) The end time from which to calculate past occurrences. If not provided, defaults to the current time (Time.now).
61
+ - `max_years` - (optional, Integer, keyword argument) The maximum number of years to search backward for past occurrences. Defaults to 5.
61
62
 
62
63
  ```ruby
63
64
  cron_calc.last
64
65
  # => [2023-12-19 05:05:00 +0100]
65
66
 
66
- cron_calc.last(4, Time.new(2024, 1, 1, 0, 0))
67
+ cron_calc.last(4, before: Time.new(2024, 1, 1, 0, 0))
67
68
  # => [2023-12-31 05:05:00 +0100, 2023-12-30 05:05:00 +0100, 2023-12-29 05:05:00 +0100, 2023-12-28 05:05:00 +0100]
68
69
  ```
69
70
 
70
71
  ## Unsupported features
71
72
 
72
- - DOW (Day of Week) - always require *
73
- - Named DOW and months (SUN-SAT, JAN-DEC)
74
73
  - Joining characters , - /
75
74
  - Predefined definitions (@yearly, @monthly, @weekly, @daily, @midnight, @hourly)
76
75
 
data/cron_calc.gemspec CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.email = ['jakubmizinski@gmail.com']
10
10
 
11
11
  spec.summary = 'Calculates cron job occurrences'
12
- spec.description = 'Calculates cron job occurrences within a specified period \n
12
+ spec.description = 'Calculates cron job occurrences within a specified period
13
13
  or determines next/last "n" occurrences based on a specified time.'
14
14
  spec.homepage = 'https://github.com/mizinsky/cron_calc'
15
15
  spec.license = 'MIT'
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CronCalc
4
- VERSION = '0.2.0'
4
+ VERSION = '0.4.0'
5
5
  end
data/lib/cron_calc.rb CHANGED
@@ -21,7 +21,19 @@ module CronCalc
21
21
  minutes: 0..59,
22
22
  hours: 0..23,
23
23
  days: 1..31,
24
- months: 1..12
24
+ months: 1..12,
25
+ wdays: 0..6
26
+ }.freeze
27
+
28
+ WDAYS = {
29
+ 'SUN' => '0', 'MON' => '1', 'TUE' => '2', 'WED' => '3',
30
+ 'THU' => '4', 'FRI' => '5', 'SAT' => '6'
31
+ }.freeze
32
+
33
+ MONTHS = {
34
+ 'JAN' => '1', 'FEB' => '2', 'MAR' => '3', 'APR' => '4',
35
+ 'MAY' => '5', 'JUN' => '6', 'JUL' => '7', 'AUG' => '8',
36
+ 'SEP' => '9', 'OCT' => '10', 'NOV' => '11', 'DEC' => '12'
25
37
  }.freeze
26
38
 
27
39
  def initialize(cron_string)
@@ -41,24 +53,24 @@ module CronCalc
41
53
 
42
54
  # Calculates the next 'n' occurrences of the cron job from a given start time.
43
55
  # @param count [Integer] The number of occurrences to calculate.
44
- # @param period_start [Time] The start time from which to calculate occurrences.
56
+ # @param after [Time] The start time from which to calculate occurrences.
45
57
  # @param max_years [Integer] The maximum number of years to consider for the period.
46
58
  # @return [Array<Time>] An array of the next 'n' occurrences.
47
- def next(count = 1, period_start = Time.now, max_years = 5)
59
+ def next(count = 1, after: Time.now, max_years: 5)
48
60
  occurrences(
49
- period_start..(period_start + (60 * 60 * 24 * 365 * max_years)),
61
+ after..(after + (60 * 60 * 24 * 365 * max_years)),
50
62
  count
51
63
  )
52
64
  end
53
65
 
54
66
  # Calculates the last 'n' occurrences of the cron job until a given end time.
55
67
  # @param count [Integer] The number of past occurrences to calculate.
56
- # @param period_end [Time] The end time until which to calculate occurrences.
68
+ # @param before [Time] The end time until which to calculate occurrences.
57
69
  # @param max_years [Integer] The maximum number of years to consider for the period.
58
70
  # @return [Array<Time>] An array of the last 'n' occurrences.
59
- def last(count = 1, period_end = Time.now, max_years = 5)
71
+ def last(count = 1, before: Time.now, max_years: 5)
60
72
  occurrences(
61
- (period_end - (60 * 60 * 24 * 365 * max_years))..period_end,
73
+ (before - (60 * 60 * 24 * 365 * max_years))..before,
62
74
  count,
63
75
  reverse: true
64
76
  )
@@ -68,12 +80,13 @@ module CronCalc
68
80
 
69
81
  def occurrences(period, count = nil, reverse: false)
70
82
  time_combinations = generate_time_combinations(period, reverse).lazy
83
+ wdays = parse_cron_part(:wdays)
71
84
 
72
85
  time_combinations.each_with_object([]) do |(year, month, day, hour, minute), occ|
73
86
  break occ if count && occ.length == count
74
87
 
75
88
  time = Time.new(year, month, day, hour, minute)
76
- occ << time if Date.valid_date?(year, month, day) && period.include?(time)
89
+ occ << time if Date.valid_date?(year, month, day) && period.include?(time) && wdays.include?(time.wday)
77
90
  end
78
91
  end
79
92
 
@@ -92,7 +105,8 @@ module CronCalc
92
105
  minutes: splitted[0],
93
106
  hours: splitted[1],
94
107
  days: splitted[2],
95
- months: splitted[3]
108
+ months: normalize_with(splitted[3], MONTHS),
109
+ wdays: normalize_with(splitted[4], WDAYS)
96
110
  }
97
111
  end
98
112
 
@@ -120,9 +134,13 @@ module CronCalc
120
134
  end
121
135
  # rubocop:enable Metrics
122
136
 
137
+ def normalize_with(string, mapping)
138
+ mapping.inject(string) { |str, (k, v)| str.gsub(k, v) }
139
+ end
140
+
123
141
  def cron_string_valid?
124
142
  # rubocop:disable Layout/LineLength
125
- regex = %r{\A(\*|([0-5]?\d)(,([0-5]?\d))*|(\*/\d+)|(\d+-\d+)) (\*|([01]?\d|2[0-3])(,([01]?\d|2[0-3]))*|(\*/\d+)|(\d+-\d+)) (\*|([12]?\d|3[01])(,([12]?\d|3[01]))*|(\*/\d+)|(\d+-\d+)) (\*|([1-9]|1[0-2])(,([1-9]|1[0-2]))*|(\*/\d+)|(\d+-\d+)) \*\z}
143
+ regex = %r{\A(\*|([0-5]?\d)(,([0-5]?\d))*|(\*/\d+)|(\d+-\d+)) (\*|([01]?\d|2[0-3])(,([01]?\d|2[0-3]))*|(\*/\d+)|(\d+-\d+)) (\*|([12]?\d|3[01])(,([12]?\d|3[01]))*|(\*/\d+)|(\d+-\d+)) (\*|(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC|[1-9]|1[0-2])(,(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC|[1-9]|1[0-2])|-(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))*|(\*/\d+)|(\d+-\d+)) (\*|(SUN|MON|TUE|WED|THU|FRI|SAT|[0-6])(,(SUN|MON|TUE|WED|THU|FRI|SAT|[0-6])|-(SUN|MON|TUE|WED|THU|FRI|SAT))*|(\*/[0-6]+)|([0-6]-[0-6]))\z}
126
144
  # rubocop:enable Layout/LineLength
127
145
  cron_string.match?(regex)
128
146
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cron_calc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakub Miziński
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-12-19 00:00:00.000000000 Z
11
+ date: 2023-12-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |-
14
- Calculates cron job occurrences within a specified period \n
14
+ Calculates cron job occurrences within a specified period
15
15
  or determines next/last "n" occurrences based on a specified time.
16
16
  email:
17
17
  - jakubmizinski@gmail.com