cron_calc 0.3.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 +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +13 -13
- data/lib/cron_calc/version.rb +1 -1
- data/lib/cron_calc.rb +26 -11
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 29ac47b06a1dc188f3fb9ed5c10f9fa1a6ef748e8d15a6783eb7b8f99de85b38
|
4
|
+
data.tar.gz: 8f136ff6c1010357acd75942e80bc7d4a2f4b0d1b8555041486f1cc9bdd97f00
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 38f8b7a5a0f80d10f6c4867a8a2e687b9ebb508c5e79031856cbb9bc81a9a97b6f399e0b37f332f5ec5aeb7d2839e29d11b6e76b0fdc5601b675a6a5ed82e806
|
7
|
+
data.tar.gz: d1545c32cd0559370fc1778c3517567ee0be5f4fecf41ad32af0a2a3fd3202d52f0b9d5014f202ab736c8174675d2c7692610fa72968f247022082aa7a2b5567
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -22,8 +22,9 @@ Now, you can use one of three methods `#in`, `#next`, `#last` to determine cron
|
|
22
22
|
### Using `#in`
|
23
23
|
|
24
24
|
Calculates cron job occurrences within a given time period.\
|
25
|
-
|
26
|
-
|
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)
|
@@ -35,10 +36,10 @@ Calculates cron job occurrences within a given time period.\
|
|
35
36
|
### Using `#next`
|
36
37
|
|
37
38
|
Calculates the next 'n' occurrences of the cron job from a given start time.\
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
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,29 +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
57
|
Calculates the last 'n' occurrences of the cron job until a given end time.\
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
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
|
-
- Named DOW and months (SUN-SAT, JAN-DEC)
|
73
73
|
- Joining characters , - /
|
74
74
|
- Predefined definitions (@yearly, @monthly, @weekly, @daily, @midnight, @hourly)
|
75
75
|
|
data/lib/cron_calc/version.rb
CHANGED
data/lib/cron_calc.rb
CHANGED
@@ -22,7 +22,18 @@ module CronCalc
|
|
22
22
|
hours: 0..23,
|
23
23
|
days: 1..31,
|
24
24
|
months: 1..12,
|
25
|
-
|
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'
|
26
37
|
}.freeze
|
27
38
|
|
28
39
|
def initialize(cron_string)
|
@@ -42,24 +53,24 @@ module CronCalc
|
|
42
53
|
|
43
54
|
# Calculates the next 'n' occurrences of the cron job from a given start time.
|
44
55
|
# @param count [Integer] The number of occurrences to calculate.
|
45
|
-
# @param
|
56
|
+
# @param after [Time] The start time from which to calculate occurrences.
|
46
57
|
# @param max_years [Integer] The maximum number of years to consider for the period.
|
47
58
|
# @return [Array<Time>] An array of the next 'n' occurrences.
|
48
|
-
def next(count = 1,
|
59
|
+
def next(count = 1, after: Time.now, max_years: 5)
|
49
60
|
occurrences(
|
50
|
-
|
61
|
+
after..(after + (60 * 60 * 24 * 365 * max_years)),
|
51
62
|
count
|
52
63
|
)
|
53
64
|
end
|
54
65
|
|
55
66
|
# Calculates the last 'n' occurrences of the cron job until a given end time.
|
56
67
|
# @param count [Integer] The number of past occurrences to calculate.
|
57
|
-
# @param
|
68
|
+
# @param before [Time] The end time until which to calculate occurrences.
|
58
69
|
# @param max_years [Integer] The maximum number of years to consider for the period.
|
59
70
|
# @return [Array<Time>] An array of the last 'n' occurrences.
|
60
|
-
def last(count = 1,
|
71
|
+
def last(count = 1, before: Time.now, max_years: 5)
|
61
72
|
occurrences(
|
62
|
-
(
|
73
|
+
(before - (60 * 60 * 24 * 365 * max_years))..before,
|
63
74
|
count,
|
64
75
|
reverse: true
|
65
76
|
)
|
@@ -69,7 +80,7 @@ module CronCalc
|
|
69
80
|
|
70
81
|
def occurrences(period, count = nil, reverse: false)
|
71
82
|
time_combinations = generate_time_combinations(period, reverse).lazy
|
72
|
-
wdays = parse_cron_part(:
|
83
|
+
wdays = parse_cron_part(:wdays)
|
73
84
|
|
74
85
|
time_combinations.each_with_object([]) do |(year, month, day, hour, minute), occ|
|
75
86
|
break occ if count && occ.length == count
|
@@ -94,8 +105,8 @@ module CronCalc
|
|
94
105
|
minutes: splitted[0],
|
95
106
|
hours: splitted[1],
|
96
107
|
days: splitted[2],
|
97
|
-
months: splitted[3],
|
98
|
-
|
108
|
+
months: normalize_with(splitted[3], MONTHS),
|
109
|
+
wdays: normalize_with(splitted[4], WDAYS)
|
99
110
|
}
|
100
111
|
end
|
101
112
|
|
@@ -123,9 +134,13 @@ module CronCalc
|
|
123
134
|
end
|
124
135
|
# rubocop:enable Metrics
|
125
136
|
|
137
|
+
def normalize_with(string, mapping)
|
138
|
+
mapping.inject(string) { |str, (k, v)| str.gsub(k, v) }
|
139
|
+
end
|
140
|
+
|
126
141
|
def cron_string_valid?
|
127
142
|
# rubocop:disable Layout/LineLength
|
128
|
-
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+)) (\*|([0-6])(,([0-6]))*|(\*/[0-6]+)|([0-6]-[0-6]))\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}
|
129
144
|
# rubocop:enable Layout/LineLength
|
130
145
|
cron_string.match?(regex)
|
131
146
|
end
|