ice_cube 0.9.1 → 0.10.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 +7 -0
- data/lib/ice_cube/builders/hash_builder.rb +1 -1
- data/lib/ice_cube/builders/string_builder.rb +4 -4
- data/lib/ice_cube/occurrence.rb +90 -0
- data/lib/ice_cube/rule.rb +3 -2
- data/lib/ice_cube/rules/daily_rule.rb +1 -0
- data/lib/ice_cube/rules/hourly_rule.rb +1 -0
- data/lib/ice_cube/rules/minutely_rule.rb +1 -0
- data/lib/ice_cube/rules/monthly_rule.rb +1 -0
- data/lib/ice_cube/rules/secondly_rule.rb +2 -1
- data/lib/ice_cube/rules/weekly_rule.rb +1 -0
- data/lib/ice_cube/rules/yearly_rule.rb +1 -0
- data/lib/ice_cube/schedule.rb +95 -56
- data/lib/ice_cube/single_occurrence_rule.rb +2 -2
- data/lib/ice_cube/time_step.rb +30 -0
- data/lib/ice_cube/time_util.rb +123 -79
- data/lib/ice_cube/validated_rule.rb +83 -42
- data/lib/ice_cube/validations/count.rb +1 -1
- data/lib/ice_cube/validations/day.rb +2 -2
- data/lib/ice_cube/validations/day_of_week.rb +2 -2
- data/lib/ice_cube/validations/hourly_interval.rb +1 -1
- data/lib/ice_cube/validations/lock.rb +49 -22
- data/lib/ice_cube/validations/month_of_year.rb +1 -1
- data/lib/ice_cube/validations/schedule_lock.rb +6 -0
- data/lib/ice_cube/validations/until.rb +1 -0
- data/lib/ice_cube/validations/weekly_interval.rb +9 -3
- data/lib/ice_cube/version.rb +1 -1
- data/lib/ice_cube.rb +1 -0
- data/spec/spec_helper.rb +40 -0
- metadata +31 -22
data/lib/ice_cube/time_util.rb
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
require 'date'
|
|
2
2
|
|
|
3
3
|
module IceCube
|
|
4
|
-
|
|
5
4
|
module TimeUtil
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
COMMON_YEAR_MONTH_DAYS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
|
6
|
+
extend ::Deprecated
|
|
9
7
|
|
|
10
8
|
DAYS = {
|
|
11
9
|
:sunday => 0, :monday => 1, :tuesday => 2, :wednesday => 3,
|
|
@@ -18,15 +16,51 @@ module IceCube
|
|
|
18
16
|
:november => 11, :december => 12
|
|
19
17
|
}
|
|
20
18
|
|
|
21
|
-
# Provides a Time.now without the usec
|
|
22
|
-
def self.now
|
|
23
|
-
Time.at
|
|
19
|
+
# Provides a Time.now without the usec, in the reference zone or utc offset
|
|
20
|
+
def self.now(reference=Time.now)
|
|
21
|
+
match_zone(Time.at(Time.now.to_i), reference)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def self.match_zone(time, reference)
|
|
25
|
+
if reference.respond_to? :time_zone
|
|
26
|
+
time.in_time_zone(reference.time_zone)
|
|
27
|
+
else
|
|
28
|
+
if reference.utc?
|
|
29
|
+
time.utc
|
|
30
|
+
elsif reference.zone
|
|
31
|
+
time.getlocal
|
|
32
|
+
else
|
|
33
|
+
time.getlocal(reference.utc_offset)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Ensure that this is either nil, or a time
|
|
39
|
+
def self.ensure_time(time, date_eod = false)
|
|
40
|
+
case time
|
|
41
|
+
when DateTime
|
|
42
|
+
warn "IceCube: DateTime support is deprecated (please use Time)"
|
|
43
|
+
Time.local(time.year, time.month, time.day, time.hour, time.min, time.sec)
|
|
44
|
+
when Date
|
|
45
|
+
date_eod ? end_of_date(time) : time.to_time
|
|
46
|
+
else
|
|
47
|
+
time
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Ensure that this is either nil, or a date
|
|
52
|
+
def self.ensure_date(date)
|
|
53
|
+
case date
|
|
54
|
+
when Date then date
|
|
55
|
+
else
|
|
56
|
+
return Date.new(date.year, date.month, date.day)
|
|
57
|
+
end
|
|
24
58
|
end
|
|
25
59
|
|
|
26
60
|
# Serialize a time appropriate for storing
|
|
27
61
|
def self.serialize_time(time)
|
|
28
|
-
if
|
|
29
|
-
{
|
|
62
|
+
if time.respond_to?(:time_zone)
|
|
63
|
+
{:time => time.utc, :zone => time.time_zone.name}
|
|
30
64
|
elsif time.is_a?(Time)
|
|
31
65
|
time
|
|
32
66
|
end
|
|
@@ -41,45 +75,65 @@ module IceCube
|
|
|
41
75
|
end
|
|
42
76
|
end
|
|
43
77
|
|
|
78
|
+
# Check the deserialized time offset string against actual local time
|
|
79
|
+
# offset to try and preserve the original offset for plain Ruby Time. If
|
|
80
|
+
# the offset is the same as local we can assume the same original zone and
|
|
81
|
+
# keep it. If it was serialized with a different offset than local TZ it
|
|
82
|
+
# will lose the zone and not support DST.
|
|
83
|
+
def self.restore_deserialized_offset(time, orig_offset_str)
|
|
84
|
+
return time if time.respond_to?(:time_zone) or
|
|
85
|
+
time.getlocal(orig_offset_str).utc_offset == time.utc_offset
|
|
86
|
+
warn 'IceCube: parsed Time from nonlocal TZ. Use ActiveSupport to fix DST'
|
|
87
|
+
time.localtime(orig_offset_str)
|
|
88
|
+
end
|
|
89
|
+
|
|
44
90
|
# Get the beginning of a date
|
|
45
|
-
def self.beginning_of_date(date)
|
|
46
|
-
date.
|
|
47
|
-
|
|
48
|
-
|
|
91
|
+
def self.beginning_of_date(date, reference=nil)
|
|
92
|
+
args = [date.year, date.month, date.day, 0, 0, 0]
|
|
93
|
+
reference ||= Time.local(*args)
|
|
94
|
+
if reference.respond_to?(:time_zone) && reference.time_zone
|
|
95
|
+
reference.time_zone.local(*args)
|
|
96
|
+
else
|
|
97
|
+
match_zone(Time.new(*args << reference.utc_offset), reference)
|
|
98
|
+
end
|
|
49
99
|
end
|
|
50
100
|
|
|
51
101
|
# Get the end of a date
|
|
52
|
-
def self.end_of_date(date)
|
|
53
|
-
date.
|
|
54
|
-
|
|
55
|
-
|
|
102
|
+
def self.end_of_date(date, reference=nil)
|
|
103
|
+
args = [date.year, date.month, date.day, 23, 59, 59]
|
|
104
|
+
reference ||= Time.local(*args)
|
|
105
|
+
if reference.respond_to?(:time_zone) && reference.time_zone
|
|
106
|
+
reference.time_zone.local(*args)
|
|
107
|
+
else
|
|
108
|
+
match_zone(Time.new(*args << reference.utc_offset), reference)
|
|
109
|
+
end
|
|
56
110
|
end
|
|
57
111
|
|
|
58
112
|
# Convert a symbol to a numeric month
|
|
59
|
-
def self.
|
|
60
|
-
|
|
61
|
-
raise "No such month: #{
|
|
62
|
-
month
|
|
113
|
+
def self.sym_to_month(sym)
|
|
114
|
+
return wday = sym if (1..12).include? sym
|
|
115
|
+
MONTHS.fetch(sym) { |k| raise KeyError, "No such month: #{k}" }
|
|
63
116
|
end
|
|
117
|
+
deprecated_alias :symbol_to_month, :sym_to_month
|
|
64
118
|
|
|
65
|
-
# Convert a symbol to a
|
|
66
|
-
def self.
|
|
67
|
-
|
|
68
|
-
raise "No such
|
|
69
|
-
day
|
|
119
|
+
# Convert a symbol to a wday number
|
|
120
|
+
def self.sym_to_wday(sym)
|
|
121
|
+
return sym if (0..6).include? sym
|
|
122
|
+
DAYS.fetch(sym) { |k| raise KeyError, "No such weekday: #{k}" }
|
|
70
123
|
end
|
|
124
|
+
deprecated_alias :symbol_to_day, :sym_to_wday
|
|
71
125
|
|
|
72
|
-
# Convert
|
|
73
|
-
def self.
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
day
|
|
126
|
+
# Convert wday number to day symbol
|
|
127
|
+
def self.wday_to_sym(wday)
|
|
128
|
+
return sym = wday if DAYS.keys.include? wday
|
|
129
|
+
DAYS.invert.fetch(wday) { |i| raise KeyError, "No such wday number: #{i}" }
|
|
77
130
|
end
|
|
78
131
|
|
|
79
132
|
# Convert weekday from base sunday to the schedule's week start.
|
|
80
|
-
def self.
|
|
81
|
-
(
|
|
133
|
+
def self.normalize_wday(wday, week_start)
|
|
134
|
+
(wday - sym_to_wday(week_start)) % 7
|
|
82
135
|
end
|
|
136
|
+
deprecated_alias :normalize_weekday, :normalize_wday
|
|
83
137
|
|
|
84
138
|
# Return the count of the number of times wday appears in the month,
|
|
85
139
|
# and which of those time falls on
|
|
@@ -92,67 +146,57 @@ module IceCube
|
|
|
92
146
|
|
|
93
147
|
# Get the days in the month for +time
|
|
94
148
|
def self.days_in_month(time)
|
|
95
|
-
|
|
149
|
+
date = Date.new(time.year, time.month, 1)
|
|
150
|
+
((date >> 1) - date).to_i
|
|
96
151
|
end
|
|
97
152
|
|
|
153
|
+
# Get the days in the following month for +time
|
|
98
154
|
def self.days_in_next_month(time)
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
month = time.month + 1
|
|
102
|
-
if month > 12
|
|
103
|
-
month %= 12
|
|
104
|
-
year += 1
|
|
105
|
-
end
|
|
106
|
-
# And then determine
|
|
107
|
-
days_in_month_year(month, year)
|
|
155
|
+
date = Date.new(time.year, time.month, 1) >> 1
|
|
156
|
+
((date >> 1) - date).to_i
|
|
108
157
|
end
|
|
109
158
|
|
|
110
|
-
|
|
111
|
-
|
|
159
|
+
# Count the number of days to the same day of the next month without
|
|
160
|
+
# overflowing shorter months
|
|
161
|
+
def self.days_to_next_month(time)
|
|
162
|
+
date = Date.new(time.year, time.month, time.day)
|
|
163
|
+
(date >> 1) - date
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# Get a day of the month in the month of a given time without overflowing
|
|
167
|
+
# into the next month. Accepts days from positive (start of month forward) or
|
|
168
|
+
# negative (from end of month)
|
|
169
|
+
def self.day_of_month(value, date)
|
|
170
|
+
if value.to_i > 0
|
|
171
|
+
[value, days_in_month(date)].min
|
|
172
|
+
else
|
|
173
|
+
[1 + days_in_month(date) + value, 1].max
|
|
174
|
+
end
|
|
112
175
|
end
|
|
113
176
|
|
|
114
177
|
# Number of days in a year
|
|
115
178
|
def self.days_in_year(time)
|
|
116
|
-
|
|
179
|
+
date = Date.new(time.year, 1, 1)
|
|
180
|
+
((date >> 12) - date).to_i
|
|
117
181
|
end
|
|
118
182
|
|
|
119
183
|
# Number of days to n years
|
|
120
184
|
def self.days_in_n_years(time, year_distance)
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
year_distance.times do
|
|
124
|
-
diy = days_in_year(wrapper.to_time)
|
|
125
|
-
sum += diy
|
|
126
|
-
wrapper.add(:day, diy)
|
|
127
|
-
end
|
|
128
|
-
sum
|
|
185
|
+
date = Date.new(time.year, time.month, time.day)
|
|
186
|
+
((date >> year_distance * 12) - date).to_i
|
|
129
187
|
end
|
|
130
188
|
|
|
131
189
|
# The number of days in n months
|
|
132
190
|
def self.days_in_n_months(time, month_distance)
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
time -= IceCube::ONE_DAY * (time.day - 27) if time.day >= 28
|
|
136
|
-
# move n months ahead
|
|
137
|
-
sum = 0
|
|
138
|
-
wrapper = TimeWrapper.new(time)
|
|
139
|
-
month_distance.times do
|
|
140
|
-
dim = days_in_month(wrapper.to_time)
|
|
141
|
-
sum += dim
|
|
142
|
-
wrapper.add(:day, dim)
|
|
143
|
-
end
|
|
144
|
-
# now we can move to the desired day
|
|
145
|
-
dim = days_in_month(wrapper.to_time)
|
|
146
|
-
if desired_day > dim
|
|
147
|
-
sum -= desired_day - dim
|
|
148
|
-
end
|
|
149
|
-
sum
|
|
191
|
+
date = Date.new(time.year, time.month, time.day)
|
|
192
|
+
((date >> month_distance) - date).to_i
|
|
150
193
|
end
|
|
151
194
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
195
|
+
def self.dst_change(time)
|
|
196
|
+
one_hour_ago = time - ONE_HOUR
|
|
197
|
+
if time.dst? ^ one_hour_ago.dst?
|
|
198
|
+
(time.utc_offset - one_hour_ago.utc_offset) / ONE_HOUR
|
|
199
|
+
end
|
|
156
200
|
end
|
|
157
201
|
|
|
158
202
|
# A utility class for safely moving time around
|
|
@@ -209,20 +253,20 @@ module IceCube
|
|
|
209
253
|
end
|
|
210
254
|
|
|
211
255
|
def clear_sec
|
|
212
|
-
@time -= @time.sec
|
|
256
|
+
@time.sec > 0 ? @time -= @time.sec : @time
|
|
213
257
|
end
|
|
214
258
|
|
|
215
259
|
def clear_min
|
|
216
|
-
@time -= (@time.min * ONE_MINUTE)
|
|
260
|
+
@time.min > 0 ? @time -= (@time.min * ONE_MINUTE) : @time
|
|
217
261
|
end
|
|
218
262
|
|
|
219
263
|
def clear_hour
|
|
220
|
-
@time -= (@time.hour * ONE_HOUR)
|
|
264
|
+
@time.hour > 0 ? @time -= (@time.hour * ONE_HOUR) : @time
|
|
221
265
|
end
|
|
222
266
|
|
|
223
267
|
# Move to the first of the month, 0 hours
|
|
224
268
|
def clear_day
|
|
225
|
-
@time -= (@time.day - 1) *
|
|
269
|
+
@time.day > 1 ? @time -= (@time.day - 1) * ONE_DAY : @time
|
|
226
270
|
end
|
|
227
271
|
|
|
228
272
|
# Clear to january 1st
|
|
@@ -235,10 +279,10 @@ module IceCube
|
|
|
235
279
|
end
|
|
236
280
|
|
|
237
281
|
def clear_year
|
|
282
|
+
@time
|
|
238
283
|
end
|
|
239
284
|
|
|
240
285
|
end
|
|
241
286
|
|
|
242
287
|
end
|
|
243
|
-
|
|
244
288
|
end
|
|
@@ -16,51 +16,30 @@ module IceCube
|
|
|
16
16
|
include Validations::Count
|
|
17
17
|
include Validations::Until
|
|
18
18
|
|
|
19
|
+
# Validations ordered for efficiency in sequence of:
|
|
20
|
+
# * descending intervals
|
|
21
|
+
# * boundary limits
|
|
22
|
+
# * base values by cardinality (n = 60, 60, 31, 24, 12, 7)
|
|
23
|
+
# * locks by cardinality (n = 365, 60, 60, 31, 24, 12, 7)
|
|
24
|
+
# * interval multiplier
|
|
25
|
+
VALIDATION_ORDER = [
|
|
26
|
+
:year, :month, :day, :wday, :hour, :min, :sec, :count, :until,
|
|
27
|
+
:base_sec, :base_min, :base_day, :base_hour, :base_month, :base_wday,
|
|
28
|
+
:day_of_year, :second_of_minute, :minute_of_hour, :day_of_month,
|
|
29
|
+
:hour_of_day, :month_of_year, :day_of_week,
|
|
30
|
+
:interval
|
|
31
|
+
]
|
|
32
|
+
|
|
19
33
|
# Compute the next time after (or including) the specified time in respect
|
|
20
34
|
# to the given schedule
|
|
21
|
-
# NOTE: optimization target, sort the rules by their type, year first
|
|
22
|
-
# so we can make bigger jumps more often
|
|
23
35
|
def next_time(time, schedule, closing_time)
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if res.any? { |r| r.nil? || r == 0 }
|
|
32
|
-
true
|
|
33
|
-
else
|
|
34
|
-
return nil if res.all? { |r| r === true } # allow quick escaping
|
|
35
|
-
res.reject! { |r| r.nil? || r == 0 || r === true }
|
|
36
|
-
if fwd = res.min
|
|
37
|
-
type = vals.first.type # get the jump type
|
|
38
|
-
dst_adjust = !vals.first.respond_to?(:dst_adjust?) || vals.first.dst_adjust?
|
|
39
|
-
wrapper = TimeUtil::TimeWrapper.new(time, dst_adjust)
|
|
40
|
-
wrapper.add(type, fwd)
|
|
41
|
-
wrapper.clear_below(type)
|
|
42
|
-
# Move over DST if blocked, no adjustments
|
|
43
|
-
if wrapper.to_time <= time
|
|
44
|
-
wrapper = TimeUtil::TimeWrapper.new(wrapper.to_time, false)
|
|
45
|
-
until wrapper.to_time > time
|
|
46
|
-
wrapper.add(:min, 10) # smallest interval
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
# And then get the correct time out
|
|
50
|
-
time = wrapper.to_time
|
|
51
|
-
end
|
|
52
|
-
false
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
# Prevent a non-matching infinite loop
|
|
56
|
-
return nil if closing_time && time > closing_time
|
|
57
|
-
end
|
|
58
|
-
# NOTE Uses may be 1 higher than proper here since end_time isn't
|
|
59
|
-
# validated in this class. This is okay now, since we never expose it -
|
|
60
|
-
# but if we ever do - we should check that above this line, and return
|
|
61
|
-
# nil if end_time is past
|
|
62
|
-
@uses += 1 if time
|
|
63
|
-
time
|
|
36
|
+
@time = time
|
|
37
|
+
@schedule = schedule
|
|
38
|
+
|
|
39
|
+
return nil unless find_acceptable_time_before(closing_time)
|
|
40
|
+
|
|
41
|
+
@uses += 1 if @time
|
|
42
|
+
@time
|
|
64
43
|
end
|
|
65
44
|
|
|
66
45
|
def to_s
|
|
@@ -115,6 +94,68 @@ module IceCube
|
|
|
115
94
|
end
|
|
116
95
|
end
|
|
117
96
|
|
|
97
|
+
private
|
|
98
|
+
|
|
99
|
+
def finds_acceptable_time?
|
|
100
|
+
validation_names.all? do |type|
|
|
101
|
+
validation_accepts_or_updates_time?(@validations[type])
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def find_acceptable_time_before(boundary)
|
|
106
|
+
until finds_acceptable_time?
|
|
107
|
+
return false if past_closing_time?(boundary)
|
|
108
|
+
end
|
|
109
|
+
true
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def validation_accepts_or_updates_time?(validations_for_type)
|
|
113
|
+
res = validated_results(validations_for_type)
|
|
114
|
+
# If there is any nil, then we're set - otherwise choose the lowest
|
|
115
|
+
if res.any? { |r| r.nil? || r == 0 }
|
|
116
|
+
true
|
|
117
|
+
else
|
|
118
|
+
return nil if res.all? { |r| r === true } # allow quick escaping
|
|
119
|
+
res.reject! { |r| r.nil? || r == 0 || r === true }
|
|
120
|
+
shift_time_by_validation(res, validations_for_type)
|
|
121
|
+
false
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def validated_results(validations_for_type)
|
|
126
|
+
validations_for_type.map do |validation|
|
|
127
|
+
validation.validate(@time, @schedule)
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def shift_time_by_validation(res, vals)
|
|
132
|
+
return unless res.min
|
|
133
|
+
type = vals.first.type # get the jump type
|
|
134
|
+
dst_adjust = !vals.first.respond_to?(:dst_adjust?) || vals.first.dst_adjust?
|
|
135
|
+
wrapper = TimeUtil::TimeWrapper.new(@time, dst_adjust)
|
|
136
|
+
wrapper.add(type, res.min)
|
|
137
|
+
wrapper.clear_below(type)
|
|
138
|
+
|
|
139
|
+
# Move over DST if blocked, no adjustments
|
|
140
|
+
if wrapper.to_time <= @time
|
|
141
|
+
wrapper = TimeUtil::TimeWrapper.new(wrapper.to_time, false)
|
|
142
|
+
until wrapper.to_time > @time
|
|
143
|
+
wrapper.add(:min, 10) # smallest interval
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# And then get the correct time out
|
|
148
|
+
@time = wrapper.to_time
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def past_closing_time?(closing_time)
|
|
152
|
+
closing_time && @time > closing_time
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def validation_names
|
|
156
|
+
VALIDATION_ORDER & @validations.keys
|
|
157
|
+
end
|
|
158
|
+
|
|
118
159
|
end
|
|
119
160
|
|
|
120
161
|
end
|
|
@@ -3,10 +3,10 @@ require 'date'
|
|
|
3
3
|
module IceCube
|
|
4
4
|
|
|
5
5
|
module Validations::Day
|
|
6
|
-
|
|
6
|
+
|
|
7
7
|
def day(*days)
|
|
8
8
|
days.each do |day|
|
|
9
|
-
day = TimeUtil.
|
|
9
|
+
day = TimeUtil.sym_to_wday(day)
|
|
10
10
|
validations_for(:day) << Validation.new(day)
|
|
11
11
|
end
|
|
12
12
|
clobber_base_validations(:wday, :day)
|
|
@@ -5,7 +5,7 @@ module IceCube
|
|
|
5
5
|
def day_of_week(dows)
|
|
6
6
|
dows.each do |day, occs|
|
|
7
7
|
occs.each do |occ|
|
|
8
|
-
day = TimeUtil.
|
|
8
|
+
day = TimeUtil.sym_to_wday(day)
|
|
9
9
|
validations_for(:day_of_week) << Validation.new(day, occ)
|
|
10
10
|
end
|
|
11
11
|
end
|
|
@@ -18,7 +18,7 @@ module IceCube
|
|
|
18
18
|
attr_reader :day, :occ
|
|
19
19
|
|
|
20
20
|
StringBuilder.register_formatter(:day_of_week) do |segments|
|
|
21
|
-
'on the ' + segments.join('
|
|
21
|
+
'on the ' + segments.join(' and ')
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
def type
|
|
@@ -42,7 +42,7 @@ module IceCube
|
|
|
42
42
|
|
|
43
43
|
def validate(time, schedule)
|
|
44
44
|
start_time = schedule.start_time
|
|
45
|
-
sec = (time.to_i - time.to_i % ONE_HOUR) -
|
|
45
|
+
sec = (time.to_i - time.to_i % ONE_HOUR) -
|
|
46
46
|
(start_time.to_i - start_time.to_i % ONE_HOUR)
|
|
47
47
|
hours = sec / ONE_HOUR
|
|
48
48
|
unless hours % interval == 0
|
|
@@ -5,7 +5,8 @@ module IceCube
|
|
|
5
5
|
|
|
6
6
|
module Validations::Lock
|
|
7
7
|
|
|
8
|
-
INTERVALS = {
|
|
8
|
+
INTERVALS = {:min => 60, :sec => 60, :month => 12, :wday => 7}
|
|
9
|
+
|
|
9
10
|
def validate(time, schedule)
|
|
10
11
|
return send(:"validate_#{type}_lock", time, schedule) unless INTERVALS[type]
|
|
11
12
|
start = value || schedule.start_time.send(type)
|
|
@@ -15,31 +16,57 @@ module IceCube
|
|
|
15
16
|
|
|
16
17
|
private
|
|
17
18
|
|
|
18
|
-
#
|
|
19
|
-
#
|
|
19
|
+
# Lock the hour if explicitly set by hour_of_day, but allow for the nearest
|
|
20
|
+
# hour during DST start to keep the correct interval.
|
|
21
|
+
#
|
|
22
|
+
def validate_hour_lock(time, schedule)
|
|
23
|
+
hour = value || schedule.start_time.send(type)
|
|
24
|
+
hour = 24 + hour if hour < 0
|
|
25
|
+
if hour >= time.hour
|
|
26
|
+
hour - time.hour
|
|
27
|
+
else
|
|
28
|
+
if dst_offset = TimeUtil.dst_change(time)
|
|
29
|
+
hour - time.hour + dst_offset
|
|
30
|
+
else
|
|
31
|
+
24 - time.hour + hour
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# For monthly rules that have no specified day value, the validation relies
|
|
37
|
+
# on the schedule start time and jumps to include every month even if it
|
|
38
|
+
# has fewer days than the schedule's start day.
|
|
39
|
+
#
|
|
40
|
+
# Negative day values (from month end) also include all months.
|
|
41
|
+
#
|
|
42
|
+
# Positive day values are taken literally so months with fewer days will
|
|
43
|
+
# be skipped.
|
|
44
|
+
#
|
|
20
45
|
def validate_day_lock(time, schedule)
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
if
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
46
|
+
days_in_month = TimeUtil.days_in_month(time)
|
|
47
|
+
date = Date.new(time.year, time.month, time.day)
|
|
48
|
+
|
|
49
|
+
if value && value < 0
|
|
50
|
+
start = TimeUtil.day_of_month(value, date)
|
|
51
|
+
month_overflow = days_in_month - TimeUtil.days_in_next_month(time)
|
|
52
|
+
elsif value && value > 0
|
|
53
|
+
start = value
|
|
54
|
+
month_overflow = 0
|
|
55
|
+
else
|
|
56
|
+
start = TimeUtil.day_of_month(schedule.start_time.day, date)
|
|
57
|
+
month_overflow = 0
|
|
33
58
|
end
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
days_in_this_month + start_two - time.day
|
|
59
|
+
|
|
60
|
+
sleeps = start - date.day
|
|
61
|
+
|
|
62
|
+
if value && value > 0
|
|
63
|
+
until_next_month = days_in_month + sleeps
|
|
40
64
|
else
|
|
41
|
-
|
|
65
|
+
until_next_month = TimeUtil.days_to_next_month(date) + sleeps
|
|
66
|
+
until_next_month -= month_overflow
|
|
42
67
|
end
|
|
68
|
+
|
|
69
|
+
sleeps >= 0 ? sleeps : until_next_month
|
|
43
70
|
end
|
|
44
71
|
|
|
45
72
|
end
|
|
@@ -4,7 +4,7 @@ module IceCube
|
|
|
4
4
|
|
|
5
5
|
def month_of_year(*months)
|
|
6
6
|
months.each do |month|
|
|
7
|
-
month = TimeUtil.
|
|
7
|
+
month = TimeUtil.sym_to_month(month)
|
|
8
8
|
validations_for(:month_of_year) << Validation.new(month)
|
|
9
9
|
end
|
|
10
10
|
clobber_base_validations :month
|
|
@@ -6,11 +6,16 @@ module IceCube
|
|
|
6
6
|
|
|
7
7
|
def interval(interval, week_start = :sunday)
|
|
8
8
|
@interval = interval
|
|
9
|
+
@week_start = TimeUtil.wday_to_sym(week_start)
|
|
9
10
|
validations_for(:interval) << Validation.new(interval, week_start)
|
|
10
11
|
clobber_base_validations(:day)
|
|
11
12
|
self
|
|
12
13
|
end
|
|
13
14
|
|
|
15
|
+
def week_start
|
|
16
|
+
@week_start
|
|
17
|
+
end
|
|
18
|
+
|
|
14
19
|
class Validation
|
|
15
20
|
|
|
16
21
|
attr_reader :interval, :week_start
|
|
@@ -27,12 +32,13 @@ module IceCube
|
|
|
27
32
|
builder['FREQ'] << 'WEEKLY'
|
|
28
33
|
unless interval == 1
|
|
29
34
|
builder['INTERVAL'] << interval
|
|
30
|
-
builder['WKST'] <<
|
|
35
|
+
builder['WKST'] << week_start.to_s.upcase[0..1]
|
|
31
36
|
end
|
|
32
37
|
end
|
|
33
38
|
|
|
34
39
|
def build_hash(builder)
|
|
35
40
|
builder[:interval] = interval
|
|
41
|
+
builder[:week_start] = TimeUtil.sym_to_wday(week_start)
|
|
36
42
|
end
|
|
37
43
|
|
|
38
44
|
def initialize(interval, week_start)
|
|
@@ -45,8 +51,8 @@ module IceCube
|
|
|
45
51
|
st = schedule.start_time
|
|
46
52
|
start_date = Date.new(st.year, st.month, st.day)
|
|
47
53
|
weeks = (
|
|
48
|
-
(date - TimeUtil.
|
|
49
|
-
(start_date - TimeUtil.
|
|
54
|
+
(date - TimeUtil.normalize_wday(date.wday, week_start)) -
|
|
55
|
+
(start_date - TimeUtil.normalize_wday(start_date.wday, week_start))
|
|
50
56
|
) / 7
|
|
51
57
|
unless weeks % interval == 0
|
|
52
58
|
(interval - (weeks % interval)) * 7
|
data/lib/ice_cube/version.rb
CHANGED
data/lib/ice_cube.rb
CHANGED
|
@@ -17,6 +17,7 @@ module IceCube
|
|
|
17
17
|
|
|
18
18
|
autoload :Rule, 'ice_cube/rule'
|
|
19
19
|
autoload :Schedule, 'ice_cube/schedule'
|
|
20
|
+
autoload :Occurrence, 'ice_cube/occurrence'
|
|
20
21
|
|
|
21
22
|
autoload :IcalBuilder, 'ice_cube/builders/ical_builder'
|
|
22
23
|
autoload :HashBuilder, 'ice_cube/builders/hash_builder'
|