parse-cron 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NDU3MTY0MGQ1YWE1YTlmNjVhODMzZDk2MjgxMzFiMDNhOGE2OWNmNA==
5
+ data.tar.gz: !binary |-
6
+ OGQ0ZmJlMTZlNTU0MDk1YmY4YjFhMzBhNWE4YTM2N2VjNDg2Yjc3MA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ Mzg4N2NiZTNhNWI2NDcwYTEwNTJkMGU5NDI3ZGRmMmVmOTkwY2E4ZGZmOGY5
10
+ MjQ0NDI2YTA5N2Y2YWUzNmRlZTNjOGM3NmUyMmIzY2YxOWRlZWJiMGIwYmQ1
11
+ YzE5YmIzMDc0NGM4ZTYzZWJiYWQ3MWM1NTA4NWIzN2U4MGQ3NDM=
12
+ data.tar.gz: !binary |-
13
+ ODRhNDM0ZTUyOTBlN2ZhNzkzNjc0NzdhYTA2NTMzY2ZjZTQ0MTVjZTE4ZDM4
14
+ YTBjNWQ2MzFkMmFmYjk0YjdmZjA3NzlmZWUxYzIyNGQyNWRhNDUyMDk4NGY4
15
+ NDQzNTMxMThlMDllYzgzODJjMmNkYTkxZDc1MTFiMDk3OTg0OTc=
data/.gitignore CHANGED
@@ -3,3 +3,4 @@
3
3
  .bundle
4
4
  Gemfile.lock
5
5
  pkg/*
6
+ .rvmrc
@@ -52,15 +52,34 @@ class CronParser
52
52
  }
53
53
 
54
54
  def initialize(source,time_source = Time)
55
- @source = source
55
+ @source = interpret_vixieisms(source)
56
56
  @time_source = time_source
57
57
  validate_source
58
58
  end
59
+
60
+ def interpret_vixieisms(spec)
61
+ case spec
62
+ when '@reboot'
63
+ raise ArgumentError, "Can't predict last/next run of @reboot"
64
+ when '@yearly', '@annually'
65
+ '0 0 1 1 *'
66
+ when '@monthly'
67
+ '0 0 1 * *'
68
+ when '@weekly'
69
+ '0 0 * * 0'
70
+ when '@daily', '@midnight'
71
+ '0 0 * * *'
72
+ when '@hourly'
73
+ '0 * * * *'
74
+ else
75
+ spec
76
+ end
77
+ end
59
78
 
60
79
 
61
80
  # returns the next occurence after the given date
62
81
  def next(now = @time_source.now)
63
- t = InternalTime.new(now,@time_source)
82
+ t = InternalTime.new(now, @time_source)
64
83
 
65
84
  unless time_specs[:month][0].include?(t.month)
66
85
  nudge_month(t)
@@ -123,18 +142,18 @@ class CronParser
123
142
  [$1.to_i]
124
143
  end
125
144
  else
126
- raise "Bad Vixie-style specification #{subel}"
145
+ raise ArgumentError, "Bad Vixie-style specification #{subel}"
127
146
  end
128
147
  end
129
148
  end.flatten.sort
130
149
 
131
- [Set.new(values), values]
150
+ [Set.new(values), values, elem]
132
151
  end
133
152
 
134
153
 
135
154
  protected
136
155
 
137
- # returns a list of days which do both match time_spec[:dom] and time_spec[:dow]
156
+ # returns a list of days which do both match time_spec[:dom] or time_spec[:dow]
138
157
  def interpolate_weekdays(year, month)
139
158
  @_interpolate_weekdays_cache ||= {}
140
159
  @_interpolate_weekdays_cache["#{year}-#{month}"] ||= interpolate_weekdays_without_cache(year, month)
@@ -142,12 +161,21 @@ class CronParser
142
161
 
143
162
  def interpolate_weekdays_without_cache(year, month)
144
163
  t = Date.new(year, month, 1)
145
- valid_mday = time_specs[:dom][0]
146
- valid_wday = time_specs[:dow][0]
164
+ valid_mday, _, mday_field = time_specs[:dom]
165
+ valid_wday, _, wday_field = time_specs[:dow]
166
+
167
+ # Careful, if both DOW and DOM fields are non-wildcard,
168
+ # then we only need to match *one* for cron to run the job:
169
+ if not (mday_field == '*' and wday_field == '*')
170
+ valid_mday = [] if mday_field == '*'
171
+ valid_wday = [] if wday_field == '*'
172
+ end
173
+ # Careful: crontabs may use either 0 or 7 for Sunday:
174
+ valid_wday << 0 if valid_wday.include?(7)
147
175
 
148
176
  result = []
149
177
  while t.month == month
150
- result << t.mday if valid_mday.include?(t.mday) && valid_wday.include?(t.wday)
178
+ result << t.mday if valid_mday.include?(t.mday) || valid_wday.include?(t.wday)
151
179
  t = t.succ
152
180
  end
153
181
 
@@ -1,5 +1,5 @@
1
1
  module Parse
2
2
  module Cron
3
- VERSION = "0.1.3"
3
+ VERSION = "0.1.4"
4
4
  end
5
5
  end
@@ -26,26 +26,57 @@ end
26
26
 
27
27
  describe "CronParser#next" do
28
28
  [
29
- ["* * * * *", "2011-08-15 12:00", "2011-08-15 12:01"],
30
- ["* * * * *", "2011-08-15 02:25", "2011-08-15 02:26"],
31
- ["* * * * *", "2011-08-15 02:59", "2011-08-15 03:00"],
32
- ["*/15 * * * *", "2011-08-15 02:02", "2011-08-15 02:15"],
33
- ["*/15,25 * * * *", "2011-08-15 02:15", "2011-08-15 02:25"],
34
- ["30 3,6,9 * * *", "2011-08-15 02:15", "2011-08-15 03:30"],
35
- ["30 9 * * *", "2011-08-15 10:15", "2011-08-16 09:30"],
36
- ["30 9 * * *", "2011-08-31 10:15", "2011-09-01 09:30"],
37
- ["30 9 * * *", "2011-09-30 10:15", "2011-10-01 09:30"],
38
- ["0 9 * * *", "2011-12-31 10:15", "2012-01-01 09:00"],
39
- ["* * 12 * *", "2010-04-15 10:15", "2010-05-12 00:00"],
40
- ["* * * * 1,3", "2010-04-15 10:15", "2010-04-19 00:00"],
41
- ["* * * * MON,WED", "2010-04-15 10:15", "2010-04-19 00:00"],
42
- ["0 0 1 1 *", "2010-04-15 10:15", "2011-01-01 00:00"],
43
- ["0 0 * * 1", "2011-08-01 00:00", "2011-08-08 00:00"],
44
- ["0 0 * * 1", "2011-07-25 00:00", "2011-08-01 00:00"],
45
- ["45 23 7 3 *", "2011-01-01 00:00", "2011-03-07 23:45"],
46
- ["0 0 1 jun *", "2013-05-14 11:20", "2013-06-01 00:00"],
47
- ["0 0 1 may,jul *", "2013-05-14 15:00", "2013-07-01 00:00"],
48
- ["0 0 1 MAY,JUL *", "2013-05-14 15:00", "2013-07-01 00:00"],
29
+ ["* * * * *", "2011-08-15 12:00", "2011-08-15 12:01"],
30
+ ["* * * * *", "2011-08-15 02:25", "2011-08-15 02:26"],
31
+ ["* * * * *", "2011-08-15 02:59", "2011-08-15 03:00"],
32
+ ["*/15 * * * *", "2011-08-15 02:02", "2011-08-15 02:15"],
33
+ ["*/15,25 * * * *", "2011-08-15 02:15", "2011-08-15 02:25"],
34
+ ["30 3,6,9 * * *", "2011-08-15 02:15", "2011-08-15 03:30"],
35
+ ["30 9 * * *", "2011-08-15 10:15", "2011-08-16 09:30"],
36
+ ["30 9 * * *", "2011-08-31 10:15", "2011-09-01 09:30"],
37
+ ["30 9 * * *", "2011-09-30 10:15", "2011-10-01 09:30"],
38
+ ["0 9 * * *", "2011-12-31 10:15", "2012-01-01 09:00"],
39
+ ["* * 12 * *", "2010-04-15 10:15", "2010-05-12 00:00"],
40
+ ["* * * * 1,3", "2010-04-15 10:15", "2010-04-19 00:00"],
41
+ ["* * * * MON,WED", "2010-04-15 10:15", "2010-04-19 00:00"],
42
+ ["0 0 1 1 *", "2010-04-15 10:15", "2011-01-01 00:00"],
43
+ ["0 0 * * 1", "2011-08-01 00:00", "2011-08-08 00:00"],
44
+ ["0 0 * * 1", "2011-07-25 00:00", "2011-08-01 00:00"],
45
+ ["45 23 7 3 *", "2011-01-01 00:00", "2011-03-07 23:45"],
46
+ ["0 0 1 jun *", "2013-05-14 11:20", "2013-06-01 00:00"],
47
+ ["0 0 1 may,jul *", "2013-05-14 15:00", "2013-07-01 00:00"],
48
+ ["0 0 1 MAY,JUL *", "2013-05-14 15:00", "2013-07-01 00:00"],
49
+ ["40 5 * * *", "2014-02-01 15:56", "2014-02-02 05:40"],
50
+ ["0 5 * * 1", "2014-02-01 15:56", "2014-02-03 05:00"],
51
+ ["10 8 15 * *", "2014-02-01 15:56", "2014-02-15 08:10"],
52
+ ["50 6 * * 1", "2014-02-01 15:56", "2014-02-03 06:50"],
53
+ ["1 2 * apr mOn", "2014-02-01 15:56", "2014-04-07 02:01"],
54
+ ["1 2 3 4 7", "2014-02-01 15:56", "2014-04-03 02:01"],
55
+ ["1 2 3 4 7", "2014-04-04 15:56", "2014-04-06 02:01"],
56
+ ["1-20/3 * * * *", "2014-02-01 15:56", "2014-02-01 16:01"],
57
+ ["1,2,3 * * * *", "2014-02-01 15:56", "2014-02-01 16:01"],
58
+ ["1-9,15-30 * * * *", "2014-02-01 15:56", "2014-02-01 16:01"],
59
+ ["1-9/3,15-30/4 * * * *", "2014-02-01 15:56", "2014-02-01 16:01"],
60
+ ["1 2 3 jan mon", "2014-02-01 15:56", "2015-01-03 02:01"],
61
+ ["1 2 3 4 mON", "2014-02-01 15:56", "2014-04-03 02:01"],
62
+ ["1 2 3 jan 5", "2014-02-01 15:56", "2015-01-02 02:01"],
63
+ ["@yearly", "2014-02-01 15:56", "2015-01-01 00:00"],
64
+ ["@annually", "2014-02-01 15:56", "2015-01-01 00:00"],
65
+ ["@monthly", "2014-02-01 15:56", "2014-03-01 00:00"],
66
+ ["@weekly", "2014-02-01 15:56", "2014-02-02 00:00"],
67
+ ["@daily", "2014-02-01 15:56", "2014-02-02 00:00"],
68
+ ["@midnight", "2014-02-01 15:56", "2014-02-02 00:00"],
69
+ ["@hourly", "2014-02-01 15:56", "2014-02-01 16:00"],
70
+ ["*/3 * * * *", "2014-02-01 15:56", "2014-02-01 15:57"],
71
+ ["0 5 * 2,3 *", "2014-02-01 15:56", "2014-02-02 05:00"],
72
+ ["15-59/15 * * * *", "2014-02-01 15:56", "2014-02-01 16:15"],
73
+ ["15-59/15 * * * *", "2014-02-01 15:00", "2014-02-01 15:15"],
74
+ ["15-59/15 * * * *", "2014-02-01 15:01", "2014-02-01 15:15"],
75
+ ["15-59/15 * * * *", "2014-02-01 15:16", "2014-02-01 15:30"],
76
+ ["15-59/15 * * * *", "2014-02-01 15:26", "2014-02-01 15:30"],
77
+ ["15-59/15 * * * *", "2014-02-01 15:36", "2014-02-01 15:45"],
78
+ ["15-59/15 * * * *", "2014-02-01 15:45", "2014-02-01 16:15"],
79
+ ["15-59/15 * * * *", "2014-02-01 15:46", "2014-02-01 16:15"],
49
80
  ].each do |line, now, expected_next|
50
81
  it "should return #{expected_next} for '#{line}' when now is #{now}" do
51
82
  now = parse_date(now)
@@ -60,24 +91,55 @@ end
60
91
 
61
92
  describe "CronParser#last" do
62
93
  [
63
- ["* * * * *", "2011-08-15 12:00", "2011-08-15 11:59"],
64
- ["* * * * *", "2011-08-15 02:25", "2011-08-15 02:24"],
65
- ["* * * * *", "2011-08-15 03:00", "2011-08-15 02:59"],
66
- ["*/15 * * * *", "2011-08-15 02:02", "2011-08-15 02:00"],
67
- ["*/15,45 * * * *", "2011-08-15 02:55", "2011-08-15 02:45"],
68
- ["*/15,25 * * * *", "2011-08-15 02:35", "2011-08-15 02:30"],
69
- ["30 3,6,9 * * *", "2011-08-15 02:15", "2011-08-14 09:30"],
70
- ["30 9 * * *", "2011-08-15 10:15", "2011-08-15 09:30"],
71
- ["30 9 * * *", "2011-09-01 08:15", "2011-08-31 09:30"],
72
- ["30 9 * * *", "2011-10-01 08:15", "2011-09-30 09:30"],
73
- ["0 9 * * *", "2012-01-01 00:15", "2011-12-31 09:00"],
74
- ["* * 12 * *", "2010-04-15 10:15", "2010-04-12 23:59"],
75
- ["* * * * 1,3", "2010-04-15 10:15", "2010-04-14 23:59"],
76
- ["* * * * MON,WED", "2010-04-15 10:15", "2010-04-14 23:59"],
77
- ["0 0 1 1 *", "2010-04-15 10:15", "2010-01-01 00:00"],
78
- ["0 0 1 jun *", "2013-05-14 11:20", "2012-06-01 00:00"],
79
- ["0 0 1 may,jul *", "2013-05-14 15:00", "2013-05-01 00:00"],
80
- ["0 0 1 MAY,JUL *", "2013-05-14 15:00", "2013-05-01 00:00"],
94
+ ["* * * * *", "2011-08-15 12:00", "2011-08-15 11:59"],
95
+ ["* * * * *", "2011-08-15 02:25", "2011-08-15 02:24"],
96
+ ["* * * * *", "2011-08-15 03:00", "2011-08-15 02:59"],
97
+ ["*/15 * * * *", "2011-08-15 02:02", "2011-08-15 02:00"],
98
+ ["*/15,45 * * * *", "2011-08-15 02:55", "2011-08-15 02:45"],
99
+ ["*/15,25 * * * *", "2011-08-15 02:35", "2011-08-15 02:30"],
100
+ ["30 3,6,9 * * *", "2011-08-15 02:15", "2011-08-14 09:30"],
101
+ ["30 9 * * *", "2011-08-15 10:15", "2011-08-15 09:30"],
102
+ ["30 9 * * *", "2011-09-01 08:15", "2011-08-31 09:30"],
103
+ ["30 9 * * *", "2011-10-01 08:15", "2011-09-30 09:30"],
104
+ ["0 9 * * *", "2012-01-01 00:15", "2011-12-31 09:00"],
105
+ ["* * 12 * *", "2010-04-15 10:15", "2010-04-12 23:59"],
106
+ ["* * * * 1,3", "2010-04-15 10:15", "2010-04-14 23:59"],
107
+ ["* * * * MON,WED", "2010-04-15 10:15", "2010-04-14 23:59"],
108
+ ["0 0 1 1 *", "2010-04-15 10:15", "2010-01-01 00:00"],
109
+ ["0 0 1 jun *", "2013-05-14 11:20", "2012-06-01 00:00"],
110
+ ["0 0 1 may,jul *", "2013-05-14 15:00", "2013-05-01 00:00"],
111
+ ["0 0 1 MAY,JUL *", "2013-05-14 15:00", "2013-05-01 00:00"],
112
+ ["40 5 * * *", "2014-02-01 15:56", "2014-02-01 05:40"],
113
+ ["0 5 * * 1", "2014-02-01 15:56", "2014-01-27 05:00"],
114
+ ["10 8 15 * *", "2014-02-01 15:56", "2014-01-15 08:10"],
115
+ ["50 6 * * 1", "2014-02-01 15:56", "2014-01-27 06:50"],
116
+ ["1 2 * apr mOn", "2014-02-01 15:56", "2013-04-29 02:01"],
117
+ ["1 2 3 4 7", "2014-02-01 15:56", "2013-04-28 02:01"],
118
+ ["1 2 3 4 7", "2014-04-04 15:56", "2014-04-03 02:01"],
119
+ ["1-20/3 * * * *", "2014-02-01 15:56", "2014-02-01 15:19"],
120
+ ["1,2,3 * * * *", "2014-02-01 15:56", "2014-02-01 15:03"],
121
+ ["1-9,15-30 * * * *", "2014-02-01 15:56", "2014-02-01 15:30"],
122
+ ["1-9/3,15-30/4 * * * *", "2014-02-01 15:56", "2014-02-01 15:27"],
123
+ ["1 2 3 jan mon", "2014-02-01 15:56", "2014-01-27 02:01"],
124
+ ["1 2 3 4 mON", "2014-02-01 15:56", "2013-04-29 02:01"],
125
+ ["1 2 3 jan 5", "2014-02-01 15:56", "2014-01-31 02:01"],
126
+ ["@yearly", "2014-02-01 15:56", "2014-01-01 00:00"],
127
+ ["@annually", "2014-02-01 15:56", "2014-01-01 00:00"],
128
+ ["@monthly", "2014-02-01 15:56", "2014-02-01 00:00"],
129
+ ["@weekly", "2014-02-01 15:56", "2014-01-26 00:00"],
130
+ ["@daily", "2014-02-01 15:56", "2014-02-01 00:00"],
131
+ ["@midnight", "2014-02-01 15:56", "2014-02-01 00:00"],
132
+ ["@hourly", "2014-02-01 15:56", "2014-02-01 15:00"],
133
+ ["*/3 * * * *", "2014-02-01 15:56", "2014-02-01 15:54"],
134
+ ["0 5 * 2,3 *", "2014-02-01 15:56", "2014-02-01 05:00"],
135
+ ["15-59/15 * * * *", "2014-02-01 15:56", "2014-02-01 15:45"],
136
+ ["15-59/15 * * * *", "2014-02-01 15:00", "2014-02-01 14:45"],
137
+ ["15-59/15 * * * *", "2014-02-01 15:01", "2014-02-01 14:45"],
138
+ ["15-59/15 * * * *", "2014-02-01 15:16", "2014-02-01 15:15"],
139
+ ["15-59/15 * * * *", "2014-02-01 15:26", "2014-02-01 15:15"],
140
+ ["15-59/15 * * * *", "2014-02-01 15:36", "2014-02-01 15:30"],
141
+ ["15-59/15 * * * *", "2014-02-01 15:45", "2014-02-01 15:30"],
142
+ ["15-59/15 * * * *", "2014-02-01 15:46", "2014-02-01 15:45"],
81
143
  ].each do |line, now, expected_next|
82
144
  it "should return #{expected_next} for '#{line}' when now is #{now}" do
83
145
  now = parse_date(now)
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parse-cron
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
5
- prerelease:
4
+ version: 0.1.4
6
5
  platform: ruby
7
6
  authors:
8
7
  - Michael Siebert
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-08-30 00:00:00.000000000 Z
11
+ date: 2014-02-06 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rspec
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ~>
28
25
  - !ruby/object:Gem::Version
@@ -49,26 +46,25 @@ files:
49
46
  - spec/spec_helper.rb
50
47
  homepage: https://github.com/siebertm/parse-cron
51
48
  licenses: []
49
+ metadata: {}
52
50
  post_install_message:
53
51
  rdoc_options: []
54
52
  require_paths:
55
53
  - lib
56
54
  required_ruby_version: !ruby/object:Gem::Requirement
57
- none: false
58
55
  requirements:
59
56
  - - ! '>='
60
57
  - !ruby/object:Gem::Version
61
58
  version: '0'
62
59
  required_rubygems_version: !ruby/object:Gem::Requirement
63
- none: false
64
60
  requirements:
65
61
  - - ! '>='
66
62
  - !ruby/object:Gem::Version
67
63
  version: '0'
68
64
  requirements: []
69
65
  rubyforge_project: parse-cron
70
- rubygems_version: 1.8.23
66
+ rubygems_version: 2.1.11
71
67
  signing_key:
72
- specification_version: 3
68
+ specification_version: 4
73
69
  summary: Parses cron expressions and calculates the next occurence
74
70
  test_files: []