parse_cron 0.1.6

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.
@@ -0,0 +1,2 @@
1
+ # More logical way to require 'cron_parser'
2
+ require File.join(File.dirname(__FILE__), 'cron_parser')
@@ -0,0 +1,5 @@
1
+ module Parse
2
+ module Cron
3
+ VERSION = "0.1.6"
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ # More logical way to require 'cron_parser'
2
+ require File.join(File.dirname(__FILE__), 'cron_parser')
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "parse_cron/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "parse_cron"
7
+ s.version = Parse::Cron::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Oscar Mendoza"]
10
+ s.email = ["chi23bearts@gmail.com"]
11
+ s.homepage = "https://github.com/siebertm/parse_cron"
12
+ s.summary = %q{Parses cron expressions and calculates the next occurence}
13
+ s.description = %q{Parses cron expressions and calculates the next occurence}
14
+
15
+ s.rubyforge_project = "parse_cron"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_development_dependency 'rspec', '~>2.6.0'
23
+ end
@@ -0,0 +1,5 @@
1
+ module Parse
2
+ module Cron
3
+ VERSION = "0.1.6"
4
+ end
5
+ end
@@ -0,0 +1,251 @@
1
+ require "time"
2
+ require "./spec/spec_helper"
3
+ require "cron_parser"
4
+ require "date"
5
+
6
+ def parse_date(str)
7
+ dt = DateTime.strptime(str, "%Y-%m-%d %H:%M:%S")
8
+ Time.local(dt.year, dt.month, dt.day, dt.hour, dt.min, dt.sec)
9
+ end
10
+
11
+ describe "CronParser#parse_element" do
12
+ [
13
+ ["*", 0..59, (0..59).to_a],
14
+ ["*/10", 0..59, [0, 10, 20, 30, 40, 50]],
15
+ ["10", 0..59, [10]],
16
+ ["10,30", 0..59, [10, 30]],
17
+ ["10-15", 0..59, [10, 11, 12, 13, 14, 15]],
18
+ ["10-40/10", 0..59, [10, 20, 30, 40]],
19
+ ].each do |element, range, expected|
20
+ it "should return #{expected} for '#{element}' when range is #{range}" do
21
+ parser = CronParser.new('* * * * *')
22
+ expect(parser.parse_element(element, range).first.to_a.sort).to eq expected.sort
23
+ end
24
+ end
25
+ end
26
+
27
+ describe "CronParser#next" do
28
+ [
29
+ ["* * * * *", "2011-08-15 12:00:00", "2011-08-15 12:01:00",1],
30
+ ["* * * * *", "2011-08-15 02:25:00", "2011-08-15 02:26:00",1],
31
+ ["* * * * *", "2011-08-15 02:59:00", "2011-08-15 03:00:00",1],
32
+ ["*/15 * * * *", "2011-08-15 02:02:00", "2011-08-15 02:15:00",1],
33
+ ["*/15,25 * * * *", "2011-08-15 02:15:00", "2011-08-15 02:25:00",1],
34
+ ["30 3,6,9 * * *", "2011-08-15 02:15:00", "2011-08-15 03:30:00",1],
35
+ ["30 9 * * *", "2011-08-15 10:15:00", "2011-08-16 09:30:00",1],
36
+ ["30 9 * * *", "2011-08-31 10:15:00", "2011-09-01 09:30:00",1],
37
+ ["30 9 * * *", "2011-09-30 10:15:00", "2011-10-01 09:30:00",1],
38
+ ["0 9 * * *", "2011-12-31 10:15:00", "2012-01-01 09:00:00",1],
39
+ ["* * 12 * *", "2010-04-15 10:15:00", "2010-05-12 00:00:00",1],
40
+ ["* * * * 1,3", "2010-04-15 10:15:00", "2010-04-19 00:00:00",1],
41
+ ["* * * * MON,WED", "2010-04-15 10:15:00", "2010-04-19 00:00:00",1],
42
+ ["0 0 1 1 *", "2010-04-15 10:15:00", "2011-01-01 00:00:00",1],
43
+ ["0 0 * * 1", "2011-08-01 00:00:00", "2011-08-08 00:00:00",1],
44
+ ["0 0 * * 1", "2011-07-25 00:00:00", "2011-08-01 00:00:00",1],
45
+ ["45 23 7 3 *", "2011-01-01 00:00:00", "2011-03-07 23:45:00",1],
46
+ ["0 0 1 jun *", "2013-05-14 11:20:00", "2013-06-01 00:00:00",1],
47
+ ["0 0 1 may,jul *", "2013-05-14 15:00:00", "2013-07-01 00:00:00",1],
48
+ ["0 0 1 MAY,JUL *", "2013-05-14 15:00:00", "2013-07-01 00:00:00",1],
49
+ ["40 5 * * *", "2014-02-01 15:56:00", "2014-02-02 05:40:00",1],
50
+ ["0 5 * * 1", "2014-02-01 15:56:00", "2014-02-03 05:00:00",1],
51
+ ["10 8 15 * *", "2014-02-01 15:56:00", "2014-02-15 08:10:00",1],
52
+ ["50 6 * * 1", "2014-02-01 15:56:00", "2014-02-03 06:50:00",1],
53
+ ["1 2 * apr mOn", "2014-02-01 15:56:00", "2014-04-07 02:01:00",1],
54
+ ["1 2 3 4 7", "2014-02-01 15:56:00", "2014-04-03 02:01:00",1],
55
+ ["1 2 3 4 7", "2014-04-04 15:56:00", "2014-04-06 02:01:00",1],
56
+ ["1-20/3 * * * *", "2014-02-01 15:56:00", "2014-02-01 16:01:00",1],
57
+ ["1,2,3 * * * *", "2014-02-01 15:56:00", "2014-02-01 16:01:00",1],
58
+ ["1-9,15-30 * * * *", "2014-02-01 15:56:00", "2014-02-01 16:01:00",1],
59
+ ["1-9/3,15-30/4 * * * *", "2014-02-01 15:56:00", "2014-02-01 16:01:00",1],
60
+ ["1 2 3 jan mon", "2014-02-01 15:56:00", "2015-01-03 02:01:00",1],
61
+ ["1 2 3 4 mON", "2014-02-01 15:56:00", "2014-04-03 02:01:00",1],
62
+ ["1 2 3 jan 5", "2014-02-01 15:56:00", "2015-01-02 02:01:00",1],
63
+ ["@yearly", "2014-02-01 15:56:00", "2015-01-01 00:00:00",1],
64
+ ["@annually", "2014-02-01 15:56:00", "2015-01-01 00:00:00",1],
65
+ ["@monthly", "2014-02-01 15:56:00", "2014-03-01 00:00:00",1],
66
+ ["@weekly", "2014-02-01 15:56:00", "2014-02-02 00:00:00",1],
67
+ ["@daily", "2014-02-01 15:56:00", "2014-02-02 00:00:00",1],
68
+ ["@midnight", "2014-02-01 15:56:00", "2014-02-02 00:00:00",1],
69
+ ["@hourly", "2014-02-01 15:56:00", "2014-02-01 16:00:00",1],
70
+ ["@minutely", "2014-02-01 15:56:00", "2014-02-01 15:57:00",1],
71
+ ["*/3 * * * *", "2014-02-01 15:56:00", "2014-02-01 15:57:00",1],
72
+ ["0 5 * 2,3 *", "2014-02-01 15:56:00", "2014-02-02 05:00:00",1],
73
+ ["15-59/15 * * * *", "2014-02-01 15:56:00", "2014-02-01 16:15:00",1],
74
+ ["15-59/15 * * * *", "2014-02-01 15:00:00", "2014-02-01 15:15:00",1],
75
+ ["15-59/15 * * * *", "2014-02-01 15:01:00", "2014-02-01 15:15:00",1],
76
+ ["15-59/15 * * * *", "2014-02-01 15:16:00", "2014-02-01 15:30:00",1],
77
+ ["15-59/15 * * * *", "2014-02-01 15:26:00", "2014-02-01 15:30:00",1],
78
+ ["15-59/15 * * * *", "2014-02-01 15:36:00", "2014-02-01 15:45:00",1],
79
+ ["15-59/15 * * * *", "2014-02-01 15:45:00", "2014-02-01 16:15:00",4],
80
+ ["15-59/15 * * * *", "2014-02-01 15:46:00", "2014-02-01 16:15:00",3],
81
+ ["15-59/15 * * * *", "2014-02-01 15:46:00", "2014-02-01 16:15:00",2],
82
+ ["30 * * * * * *", "2011-08-15 12:00:00", "2011-08-15 12:00:30",1],
83
+ ["*/15 * * * * * *", "2011-08-15 12:00:00", "2011-08-15 12:00:15",1],
84
+ ["20-40 * * * * * *", "2011-08-15 12:00:00", "2011-08-15 12:00:20",1],
85
+ ["12 15-59/15 * * * * *", "2014-02-01 15:46:00", "2014-02-01 16:15:12",2],
86
+ ["* * * * * * 2018", "2014-02-01 15:46:00", "2018-01-01 00:00:00",1],
87
+ ["1 1 1 1 1 * 2018", "2014-02-01 15:46:00", "2018-01-01 01:01:01",1],
88
+ ["* * * * * * 2016-2018", "2014-02-01 15:46:00", "2016-01-01 00:00:00",1],
89
+ ["* * * * * * */20", "2014-02-01 15:46:00", "2020-01-01 00:00:00",1],
90
+ # tw testing
91
+ # 6 digit dkron
92
+ ["0 * * * * *", "2019-06-01 08:00:00", "2019-06-01 08:01:00",1],
93
+ ["0 0 * * * *", "2019-06-01 08:00:00", "2019-06-01 09:00:00",1],
94
+ ["0 0 0 * * *", "2019-06-01 08:00:00", "2019-06-02 00:00:00",1],
95
+ ["0 0 0 1 * *", "2019-06-01 08:00:00", "2019-07-01 00:00:00",1],
96
+ ["0 0 0 5 7 *", "2019-06-01 08:00:00", "2019-07-05 00:00:00",1],
97
+ ["0 0 * * 8 SUN", "2019-06-01 08:00:00", "2019-08-04 00:00:00",1],
98
+
99
+ ["15 0,15,30,45 * * * *", "2019-06-01 08:16:16", "2019-06-01 08:30:15",1],
100
+ ["0 */15 */3 * * *", "2019-06-01 08:00:00", "2019-06-01 09:00:00",1],
101
+ ["0 0 * ? * *", "2019-06-01 08:00:00", "2019-06-01 09:00:00",1],
102
+ ["0 0 * * * ?", "2019-06-01 08:00:00", "2019-06-01 09:00:00",1],
103
+ ["0 0 * ? * ?", "2019-06-01 08:00:00", "2019-06-01 09:00:00",1],
104
+ ].each do |line, now, expected_next,num|
105
+ it "returns #{expected_next} for '#{line}' when now is #{now}" do
106
+ parsed_now = parse_date(now)
107
+ expected = parse_date(expected_next)
108
+ parser = CronParser.new(line)
109
+ expect(parser.next(parsed_now).xmlschema).to eq expected.xmlschema
110
+ end
111
+ it "returns the expected class" do
112
+ parsed_now = parse_date(now)
113
+ expected = parse_date(expected_next)
114
+ parser = CronParser.new(line)
115
+ result = parser.next(parsed_now,num)
116
+ expect(result.class.to_s).to eq (num > 1 ? 'Array' : 'Time')
117
+ end
118
+ it "returns the expected count" do
119
+ parsed_now = parse_date(now)
120
+ expected = parse_date(expected_next)
121
+ parser = CronParser.new(line)
122
+ result = parser.next(parsed_now,num)
123
+ if result.class.to_s == 'Array'
124
+ expect(result.size).to eq num
125
+ else
126
+ expect(result.class.to_s).to eq 'Time'
127
+ end
128
+ end
129
+ end
130
+
131
+ [
132
+ ["* * * * * * 2010", "2014-02-01 15:46:00"]
133
+ ].each do |line, now|
134
+ it "should raise an error for '#{line}' when now is #{now}" do
135
+ now = parse_date(now)
136
+
137
+ parser = CronParser.new(line)
138
+
139
+ expect{parser.next(now)}.to raise_error "No matching dates exist"
140
+ end
141
+ end
142
+ end
143
+
144
+ describe "CronParser#last" do
145
+ [
146
+ ["* * * * *", "2011-08-15 12:00:00", "2011-08-15 11:59:00"],
147
+ ["* * * * *", "2011-08-15 02:25:00", "2011-08-15 02:24:00"],
148
+ ["* * * * *", "2011-08-15 03:00:00", "2011-08-15 02:59:00"],
149
+ ["*/15 * * * *", "2011-08-15 02:02:00", "2011-08-15 02:00:00"],
150
+ ["*/15,45 * * * *", "2011-08-15 02:55:00", "2011-08-15 02:45:00"],
151
+ ["*/15,25 * * * *", "2011-08-15 02:35:00", "2011-08-15 02:30:00"],
152
+ ["30 3,6,9 * * *", "2011-08-15 02:15:00", "2011-08-14 09:30:00"],
153
+ ["30 9 * * *", "2011-08-15 10:15:00", "2011-08-15 09:30:00"],
154
+ ["30 9 * * *", "2011-09-01 08:15:00", "2011-08-31 09:30:00"],
155
+ ["30 9 * * *", "2011-10-01 08:15:00", "2011-09-30 09:30:00"],
156
+ ["0 9 * * *", "2012-01-01 00:15:00", "2011-12-31 09:00:00"],
157
+ ["* * 12 * *", "2010-04-15 10:15:00", "2010-04-12 23:59:00"],
158
+ ["* * * * 1,3", "2010-04-15 10:15:00", "2010-04-14 23:59:00"],
159
+ ["* * * * MON,WED", "2010-04-15 10:15:00", "2010-04-14 23:59:00"],
160
+ ["0 0 1 1 *", "2010-04-15 10:15:00", "2010-01-01 00:00:00"],
161
+ ["0 0 1 jun *", "2013-05-14 11:20:00", "2012-06-01 00:00:00"],
162
+ ["0 0 1 may,jul *", "2013-05-14 15:00:00", "2013-05-01 00:00:00"],
163
+ ["0 0 1 MAY,JUL *", "2013-05-14 15:00:00", "2013-05-01 00:00:00"],
164
+ ["40 5 * * *", "2014-02-01 15:56:00", "2014-02-01 05:40:00"],
165
+ ["0 5 * * 1", "2014-02-01 15:56:00", "2014-01-27 05:00:00"],
166
+ ["10 8 15 * *", "2014-02-01 15:56:00", "2014-01-15 08:10:00"],
167
+ ["50 6 * * 1", "2014-02-01 15:56:00", "2014-01-27 06:50:00"],
168
+ ["1 2 * apr mOn", "2014-02-01 15:56:00", "2013-04-29 02:01:00"],
169
+ ["1 2 3 4 7", "2014-02-01 15:56:00", "2013-04-28 02:01:00"],
170
+ ["1 2 3 4 7", "2014-04-04 15:56:00", "2014-04-03 02:01:00"],
171
+ ["1-20/3 * * * *", "2014-02-01 15:56:00", "2014-02-01 15:19:00"],
172
+ ["1,2,3 * * * *", "2014-02-01 15:56:00", "2014-02-01 15:03:00"],
173
+ ["1-9,15-30 * * * *", "2014-02-01 15:56:00", "2014-02-01 15:30:00"],
174
+ ["1-9/3,15-30/4 * * * *", "2014-02-01 15:56:00", "2014-02-01 15:27:00"],
175
+ ["1 2 3 jan mon", "2014-02-01 15:56:00", "2014-01-27 02:01:00"],
176
+ ["1 2 3 4 mON", "2014-02-01 15:56:00", "2013-04-29 02:01:00"],
177
+ ["1 2 3 jan 5", "2014-02-01 15:56:00", "2014-01-31 02:01:00"],
178
+ ["@yearly", "2014-02-01 15:56:00", "2014-01-01 00:00:00"],
179
+ ["@annually", "2014-02-01 15:56:00", "2014-01-01 00:00:00"],
180
+ ["@monthly", "2014-02-01 15:56:00", "2014-02-01 00:00:00"],
181
+ ["@weekly", "2014-02-01 15:56:00", "2014-01-26 00:00:00"],
182
+ ["@daily", "2014-02-01 15:56:00", "2014-02-01 00:00:00"],
183
+ ["@midnight", "2014-02-01 15:56:00", "2014-02-01 00:00:00"],
184
+ ["@hourly", "2014-02-01 15:56:00", "2014-02-01 15:00:00"],
185
+ ["*/3 * * * *", "2014-02-01 15:56:00", "2014-02-01 15:54:00"],
186
+ ["0 5 * 2,3 *", "2014-02-01 15:56:00", "2014-02-01 05:00:00"],
187
+ ["15-59/15 * * * *", "2014-02-01 15:56:00", "2014-02-01 15:45:00"],
188
+ ["15-59/15 * * * *", "2014-02-01 15:00:00", "2014-02-01 14:45:00"],
189
+ ["15-59/15 * * * *", "2014-02-01 15:01:00", "2014-02-01 14:45:00"],
190
+ ["15-59/15 * * * *", "2014-02-01 15:16:00", "2014-02-01 15:15:00"],
191
+ ["15-59/15 * * * *", "2014-02-01 15:26:00", "2014-02-01 15:15:00"],
192
+ ["15-59/15 * * * *", "2014-02-01 15:36:00", "2014-02-01 15:30:00"],
193
+ ["15-59/15 * * * *", "2014-02-01 15:45:00", "2014-02-01 15:30:00"],
194
+ ["15-59/15 * * * *", "2014-02-01 15:46:00", "2014-02-01 15:45:00"],
195
+ ["30 * * * * * *", "2011-08-15 12:00:00", "2011-08-15 11:59:30"],
196
+ ["*/15 * * * * * *", "2011-08-15 12:00:00", "2011-08-15 11:59:45"],
197
+ ["20-40 * * * * * *", "2011-08-15 12:00:00", "2011-08-15 11:59:40"],
198
+ ["12 15-59/15 * * * * *", "2014-02-01 15:46:00", "2014-02-01 15:45:12"],
199
+ ["* * * * * * 1970", "2014-02-01 15:46:00", "1970-12-31 23:59:59"],
200
+ ["1 1 1 1 1 * 1970", "2014-02-01 15:46:00", "1970-01-01 01:01:01"],
201
+ ["* * * * * * 1970-1978", "2014-02-01 15:46:00", "1978-12-31 23:59:59"],
202
+ ["* * * * * * */20", "2014-02-01 15:46:00", "2000-12-31 23:59:59"],
203
+ ].each do |line, now, expected_next|
204
+ it "should return #{expected_next} for '#{line}' when now is #{now}" do
205
+ now = parse_date(now)
206
+ expected_next = parse_date(expected_next)
207
+
208
+ parser = CronParser.new(line)
209
+
210
+ expect(parser.last(now)).to eq expected_next
211
+ end
212
+ end
213
+
214
+ [
215
+ ["* * * * * * 2018", "2014-02-01 15:46:00"]
216
+ ].each do |line, now|
217
+ it "should raise an error for '#{line}' when now is #{now}" do
218
+ now = parse_date(now)
219
+
220
+ parser = CronParser.new(line)
221
+
222
+ expect{parser.last(now)}.to raise_error "No matching dates exist"
223
+ end
224
+ end
225
+ end
226
+
227
+ describe "CronParser#new" do
228
+ it 'should not raise error when given a valid cronline' do
229
+ expect { CronParser.new('30 * * * *') }.not_to raise_error
230
+ end
231
+
232
+
233
+ [
234
+ ["* * * *"],
235
+ ["? ? ? ? ? ?"],
236
+ ["? * * * * *"],
237
+ ["* * * * * * * *"],
238
+ ].each do |line|
239
+ it 'should raise error when given an invalid cronline' do
240
+ expect { CronParser.new(line) }.to raise_error('not a valid cronline')
241
+ end
242
+ end
243
+ end
244
+
245
+ describe "time source" do
246
+ it "should use an alternate specified time source" do
247
+ ExtendedTime = Class.new(Time)
248
+ expect(ExtendedTime).to receive(:local).once
249
+ CronParser.new("* * * * *",ExtendedTime).next
250
+ end
251
+ end
@@ -0,0 +1,9 @@
1
+ spec_dir = File.dirname(__FILE__)
2
+ lib_dir = File.expand_path(File.join(spec_dir, '..', 'lib'))
3
+ $:.unshift(lib_dir)
4
+ $:.uniq!
5
+
6
+ RSpec.configure do |config|
7
+ end
8
+
9
+ require 'cron_parser'
@@ -0,0 +1,9 @@
1
+ spec_dir = File.dirname(__FILE__)
2
+ lib_dir = File.expand_path(File.join(spec_dir, '..', 'lib'))
3
+ $:.unshift(lib_dir)
4
+ $:.uniq!
5
+
6
+ RSpec.configure do |config|
7
+ end
8
+
9
+ require 'cron_parser'
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parse_cron
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.6
5
+ platform: ruby
6
+ authors:
7
+ - Oscar Mendoza
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-06-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.6.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.6.0
27
+ description: Parses cron expressions and calculates the next occurence
28
+ email:
29
+ - chi23bearts@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rspec"
36
+ - ".travis.yml"
37
+ - Gemfile
38
+ - License
39
+ - README.md
40
+ - Rakefile
41
+ - cron_parser.rb
42
+ - cron_parser_spec.rb
43
+ - lib/cron_parser.rb
44
+ - lib/parse-cron.rb
45
+ - lib/parse_cron/version.rb
46
+ - parse-cron.rb
47
+ - parse_cron.gemspec
48
+ - parse_cron/version.rb
49
+ - spec/cron_parser_spec.rb
50
+ - spec/spec_helper.rb
51
+ - spec_helper.rb
52
+ homepage: https://github.com/siebertm/parse_cron
53
+ licenses: []
54
+ metadata: {}
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.0.3
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Parses cron expressions and calculates the next occurence
74
+ test_files:
75
+ - spec/cron_parser_spec.rb
76
+ - spec/spec_helper.rb