ice_cube 0.11.0 → 0.12.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.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/lib/ice_cube/builders/string_builder.rb +15 -23
  3. data/lib/ice_cube/deprecated.rb +33 -23
  4. data/lib/ice_cube/enumerator.rb +1 -3
  5. data/lib/ice_cube/errors/count_exceeded.rb +1 -1
  6. data/lib/ice_cube/errors/until_exceeded.rb +1 -1
  7. data/lib/ice_cube/flexible_hash.rb +23 -7
  8. data/lib/ice_cube/formatters/string.rb +155 -0
  9. data/lib/ice_cube/hash_input.rb +71 -0
  10. data/lib/ice_cube/occurrence.rb +2 -1
  11. data/lib/ice_cube/parsers/hash_parser.rb +87 -0
  12. data/lib/ice_cube/parsers/yaml_parser.rb +21 -0
  13. data/lib/ice_cube/rule.rb +5 -7
  14. data/lib/ice_cube/rules/daily_rule.rb +1 -1
  15. data/lib/ice_cube/rules/hourly_rule.rb +1 -1
  16. data/lib/ice_cube/rules/minutely_rule.rb +1 -1
  17. data/lib/ice_cube/rules/monthly_rule.rb +1 -1
  18. data/lib/ice_cube/rules/secondly_rule.rb +1 -1
  19. data/lib/ice_cube/rules/yearly_rule.rb +1 -1
  20. data/lib/ice_cube/schedule.rb +76 -64
  21. data/lib/ice_cube/string_helpers.rb +10 -0
  22. data/lib/ice_cube/time_util.rb +24 -8
  23. data/lib/ice_cube/validated_rule.rb +13 -6
  24. data/lib/ice_cube/validations/count.rb +8 -6
  25. data/lib/ice_cube/validations/daily_interval.rb +17 -18
  26. data/lib/ice_cube/validations/day.rb +8 -4
  27. data/lib/ice_cube/validations/day_of_month.rb +12 -10
  28. data/lib/ice_cube/validations/day_of_week.rb +28 -26
  29. data/lib/ice_cube/validations/day_of_year.rb +15 -12
  30. data/lib/ice_cube/validations/hour_of_day.rb +13 -11
  31. data/lib/ice_cube/validations/hourly_interval.rb +18 -21
  32. data/lib/ice_cube/validations/lock.rb +35 -14
  33. data/lib/ice_cube/validations/minute_of_hour.rb +13 -11
  34. data/lib/ice_cube/validations/minutely_interval.rb +16 -19
  35. data/lib/ice_cube/validations/month_of_year.rb +8 -4
  36. data/lib/ice_cube/validations/monthly_interval.rb +19 -17
  37. data/lib/ice_cube/validations/schedule_lock.rb +12 -11
  38. data/lib/ice_cube/validations/second_of_minute.rb +9 -5
  39. data/lib/ice_cube/validations/secondly_interval.rb +13 -16
  40. data/lib/ice_cube/validations/until.rb +16 -12
  41. data/lib/ice_cube/validations/weekly_interval.rb +24 -23
  42. data/lib/ice_cube/validations/yearly_interval.rb +14 -11
  43. data/lib/ice_cube/version.rb +1 -1
  44. data/lib/ice_cube.rb +13 -11
  45. data/spec/spec_helper.rb +2 -0
  46. metadata +24 -34
@@ -4,7 +4,7 @@ module IceCube
4
4
 
5
5
  class Schedule
6
6
 
7
- extend ::Deprecated
7
+ extend Deprecated
8
8
 
9
9
  # Get the start time
10
10
  attr_reader :start_time
@@ -145,60 +145,81 @@ module IceCube
145
145
  # Get all of the occurrences from the start_time up until a
146
146
  # given Time
147
147
  def occurrences(closing_time)
148
- find_occurrences(start_time, closing_time)
148
+ enumerate_occurrences(start_time, closing_time).to_a
149
149
  end
150
150
 
151
151
  # All of the occurrences
152
152
  def all_occurrences
153
153
  require_terminating_rules
154
- find_occurrences(start_time)
154
+ enumerate_occurrences(start_time).to_a
155
+ end
156
+
157
+ # Emit an enumerator based on the start time
158
+ def all_occurrences_enumerator
159
+ enumerate_occurrences(start_time)
155
160
  end
156
161
 
157
162
  # Iterate forever
158
163
  def each_occurrence(&block)
159
- find_occurrences(start_time, &block)
164
+ enumerate_occurrences(start_time, &block).to_a
160
165
  self
161
166
  end
162
167
 
163
168
  # The next n occurrences after now
164
169
  def next_occurrences(num, from = nil)
165
170
  from ||= TimeUtil.now(@start_time)
166
- find_occurrences(from + 1, nil, num)
171
+ enumerate_occurrences(from + 1, nil).take(num)
167
172
  end
168
173
 
169
174
  # The next occurrence after now (overridable)
170
175
  def next_occurrence(from = nil)
171
176
  from ||= TimeUtil.now(@start_time)
172
- find_occurrences(from + 1, nil, 1).first
177
+ begin
178
+ enumerate_occurrences(from + 1, nil).next()
179
+ rescue StopIteration
180
+ nil
181
+ end
173
182
  end
174
183
 
175
184
  # The previous occurrence from a given time
176
185
  def previous_occurrence(from)
177
186
  return nil if from <= start_time
178
- find_occurrences(start_time, from - 1, nil, 1).last
187
+ enumerate_occurrences(start_time, from - 1).to_a.last
179
188
  end
180
189
 
181
190
  # The previous n occurrences before a given time
182
191
  def previous_occurrences(num, from)
183
192
  return [] if from <= start_time
184
- find_occurrences(start_time, from - 1, nil, num)
193
+ a = enumerate_occurrences(start_time, from - 1).to_a
194
+ a.size > num ? a[-1*num,a.size] : a
185
195
  end
186
196
 
187
197
  # The remaining occurrences (same requirements as all_occurrences)
188
198
  def remaining_occurrences(from = nil)
189
199
  require_terminating_rules
190
200
  from ||= TimeUtil.now(@start_time)
191
- find_occurrences(from)
201
+ enumerate_occurrences(from).to_a
202
+ end
203
+
204
+ # Returns an enumerator for all remaining occurrences
205
+ def remaining_occurrences_enumerator(from = nil)
206
+ from ||= TimeUtil.now(@start_time)
207
+ enumerate_occurrences(from)
192
208
  end
193
209
 
194
210
  # Occurrences between two times
195
211
  def occurrences_between(begin_time, closing_time)
196
- find_occurrences(begin_time, closing_time)
212
+ enumerate_occurrences(begin_time, closing_time).to_a
197
213
  end
198
214
 
199
215
  # Return a boolean indicating if an occurrence falls between two times
200
216
  def occurs_between?(begin_time, closing_time)
201
- !find_occurrences(begin_time, closing_time, 1).empty?
217
+ begin
218
+ enumerate_occurrences(begin_time, closing_time).next()
219
+ true
220
+ rescue StopIteration
221
+ false
222
+ end
202
223
  end
203
224
 
204
225
  # Return a boolean indicating if an occurrence is occurring between two
@@ -237,7 +258,7 @@ module IceCube
237
258
  def conflicts_with?(other_schedule, closing_time = nil)
238
259
  closing_time = TimeUtil.ensure_time closing_time
239
260
  unless terminating? || other_schedule.terminating? || closing_time
240
- raise ArgumentError.new 'At least one schedule must be terminating to use #conflicts_with?'
261
+ raise ArgumentError, "One or both schedules must be terminating to use #conflicts_with?"
241
262
  end
242
263
  # Pick the terminating schedule, and other schedule
243
264
  # No need to reverse if terminating? or there is a closing time
@@ -247,6 +268,7 @@ module IceCube
247
268
  end
248
269
  # Go through each occurrence of the terminating schedule and determine
249
270
  # if the other occurs at that time
271
+ #
250
272
  last_time = nil
251
273
  terminating_schedule.each_occurrence do |time|
252
274
  if closing_time && time > closing_time
@@ -276,7 +298,7 @@ module IceCube
276
298
 
277
299
  # Get the first n occurrences, or the first occurrence if n is skipped
278
300
  def first(n = nil)
279
- occurrences = find_occurrences start_time, nil, n || 1
301
+ occurrences = enumerate_occurrences(start_time).take(n || 1)
280
302
  n.nil? ? occurrences.first : occurrences
281
303
  end
282
304
 
@@ -284,7 +306,7 @@ module IceCube
284
306
  # or the final one if no n is given
285
307
  def last(n = nil)
286
308
  require_terminating_rules
287
- occurrences = find_occurrences(start_time, nil, nil, n || 1)
309
+ occurrences = enumerate_occurrences(start_time).to_a
288
310
  n.nil? ? occurrences.last : occurrences[-n..-1]
289
311
  end
290
312
 
@@ -313,25 +335,27 @@ module IceCube
313
335
 
314
336
  # Convert the schedule to yaml
315
337
  def to_yaml(*args)
316
- IceCube::use_psych? ? Psych::dump(to_hash, *args) : YAML::dump(to_hash, *args)
338
+ YAML::dump(to_hash, *args)
317
339
  end
318
340
 
319
341
  # Load the schedule from yaml
320
342
  def self.from_yaml(yaml, options = {})
321
- hash = IceCube::use_psych? ? Psych::load(yaml) : YAML::load(yaml)
322
- if match = yaml.match(/start_date: .+((?:-|\+)\d{2}:\d{2})$/)
323
- TimeUtil.restore_deserialized_offset(hash[:start_date], match[1])
343
+ YamlParser.new(yaml).to_schedule do |schedule|
344
+ Deprecated.schedule_options(schedule, options)
345
+ yield schedule if block_given?
324
346
  end
325
- from_hash hash, options
326
347
  end
327
348
 
328
349
  # Convert the schedule to a hash
329
350
  def to_hash
330
351
  data = {}
331
- data[:start_date] = TimeUtil.serialize_time(start_time)
352
+ data[:start_time] = TimeUtil.serialize_time(start_time)
353
+ data[:start_date] = data[:start_time] if IceCube.compatibility <= 11
332
354
  data[:end_time] = TimeUtil.serialize_time(end_time) if end_time
333
355
  data[:rrules] = recurrence_rules.map(&:to_hash)
334
- data[:exrules] = exception_rules.map(&:to_hash)
356
+ if IceCube.compatibility <= 11 && exception_rules.any?
357
+ data[:exrules] = exception_rules.map(&:to_hash)
358
+ end
335
359
  data[:rtimes] = recurrence_times.map do |rt|
336
360
  TimeUtil.serialize_time(rt)
337
361
  end
@@ -343,28 +367,10 @@ module IceCube
343
367
 
344
368
  # Load the schedule from a hash
345
369
  def self.from_hash(original_hash, options = {})
346
- original_hash[:start_date] = options[:start_date_override] if options[:start_date_override]
347
- # And then deserialize
348
- data = IceCube::FlexibleHash.new(original_hash)
349
- schedule = IceCube::Schedule.new TimeUtil.deserialize_time(data[:start_date])
350
- schedule.end_time = schedule.start_time + data[:duration] if data[:duration]
351
- schedule.end_time = TimeUtil.deserialize_time(data[:end_time]) if data[:end_time]
352
- data[:rrules] && data[:rrules].each { |h| schedule.rrule(IceCube::Rule.from_hash(h)) }
353
- data[:exrules] && data[:exrules].each { |h| schedule.exrule(IceCube::Rule.from_hash(h)) }
354
- data[:rtimes] && data[:rtimes].each do |t|
355
- schedule.add_recurrence_time TimeUtil.deserialize_time(t)
356
- end
357
- data[:extimes] && data[:extimes].each do |t|
358
- schedule.add_exception_time TimeUtil.deserialize_time(t)
370
+ HashParser.new(original_hash).to_schedule do |schedule|
371
+ Deprecated.schedule_options(schedule, options)
372
+ yield schedule if block_given?
359
373
  end
360
- # Also serialize old format for backward compat
361
- data[:rdates] && data[:rdates].each do |t|
362
- schedule.add_recurrence_time TimeUtil.deserialize_time(t)
363
- end
364
- data[:exdates] && data[:exdates].each do |t|
365
- schedule.add_exception_time TimeUtil.deserialize_time(t)
366
- end
367
- schedule
368
374
  end
369
375
 
370
376
  # Determine if the schedule will end
@@ -391,30 +397,29 @@ module IceCube
391
397
 
392
398
  # Find all of the occurrences for the schedule between opening_time
393
399
  # and closing_time
394
- def find_occurrences(opening_time, closing_time = nil, limit = nil, tail_limit = nil, &block)
395
- opening_time = TimeUtil.ensure_time opening_time
396
- closing_time = TimeUtil.ensure_time closing_time
400
+ # Iteration is unrolled in pairs to skip duplicate times in end of DST
401
+ def enumerate_occurrences(opening_time, closing_time = nil, &block)
402
+ opening_time = TimeUtil.match_zone(opening_time, start_time)
403
+ closing_time = TimeUtil.match_zone(closing_time, start_time)
397
404
  opening_time += start_time.subsec - opening_time.subsec rescue 0
398
405
  reset
399
- answers = []
400
406
  opening_time = start_time if opening_time < start_time
401
- # walk up to the opening time - and off we go
402
- # If we have rules with counts, we need to walk from the beginning of time,
403
- # otherwise opening_time
404
- time = full_required? ? start_time : opening_time
405
- loop do
406
- res = next_time(time, closing_time)
407
- break unless res
408
- break if closing_time && res > closing_time
409
- if res >= opening_time
410
- block_given? ? block.call(res) : (answers << res)
411
- answers.shift if tail_limit && answers.length > tail_limit
412
- break if limit && answers.length == limit
407
+ t1 = full_required? ? start_time : opening_time
408
+ e = Enumerator.new do |yielder|
409
+ loop do
410
+ break unless (t0 = next_time(t1, closing_time))
411
+ break if closing_time && t0 > closing_time
412
+ yielder << (block_given? ? block.call(t0) : t0) if t0 >= opening_time
413
+ break unless (t1 = next_time(t0 + 1, closing_time))
414
+ break if closing_time && t1 > closing_time
415
+ if TimeUtil.same_clock?(t0, t1) && recurrence_rules.any?(&:dst_adjust?)
416
+ wind_back_dst
417
+ next (t1 += 1)
418
+ end
419
+ yielder << (block_given? ? block.call(t1) : t1) if t1 >= opening_time
420
+ next (t1 += 1)
413
421
  end
414
- time = res + 1
415
422
  end
416
- # and return our answers
417
- answers
418
423
  end
419
424
 
420
425
  # Get the next time after (or including) a specific time
@@ -424,17 +429,18 @@ module IceCube
424
429
  begin
425
430
  new_time = rule.next_time(time, self, min_time || closing_time)
426
431
  [min_time, new_time].compact.min
427
- rescue CountExceeded, UntilExceeded
432
+ rescue StopIteration
428
433
  min_time
429
434
  end
430
435
  end
431
436
  break nil unless min_time
432
- next(time = min_time + 1) if exception_time?(min_time)
437
+ next (time = min_time + 1) if exception_time?(min_time)
433
438
  break Occurrence.new(min_time, min_time + duration)
434
439
  end
435
440
  end
436
441
 
437
- # Return a boolean indicating if any rule needs to be run from the start of time
442
+ # Indicate if any rule needs to be run from the start of time
443
+ # If we have rules with counts, we need to walk from the beginning of time
438
444
  def full_required?
439
445
  @all_recurrence_rules.any?(&:full_required?) ||
440
446
  @all_exception_rules.any?(&:full_required?)
@@ -478,6 +484,12 @@ module IceCube
478
484
  end
479
485
  end
480
486
 
487
+ def wind_back_dst
488
+ recurrence_rules.each do |rule|
489
+ rule.skipped_for_dst
490
+ end
491
+ end
492
+
481
493
  end
482
494
 
483
495
  end
@@ -0,0 +1,10 @@
1
+ module IceCube
2
+ module StringHelpers
3
+
4
+ module_function
5
+
6
+ def nice_number(number)
7
+ end
8
+
9
+ end
10
+ end
@@ -3,7 +3,7 @@ require 'date'
3
3
  module IceCube
4
4
  module TimeUtil
5
5
 
6
- extend ::Deprecated
6
+ extend Deprecated
7
7
 
8
8
  DAYS = {
9
9
  :sunday => 0, :monday => 1, :tuesday => 2, :wednesday => 3,
@@ -16,6 +16,8 @@ module IceCube
16
16
  :november => 11, :december => 12
17
17
  }
18
18
 
19
+ CLOCK_VALUES = [:year, :month, :day, :hour, :min, :sec]
20
+
19
21
  # Provides a Time.now without the usec, in the reference zone or utc offset
20
22
  def self.now(reference=Time.now)
21
23
  match_zone(Time.at(Time.now.to_i), reference)
@@ -72,7 +74,8 @@ module IceCube
72
74
  if time_or_hash.is_a?(Time)
73
75
  time_or_hash
74
76
  elsif time_or_hash.is_a?(Hash)
75
- time_or_hash[:time].in_time_zone(time_or_hash[:zone])
77
+ hash = FlexibleHash.new(time_or_hash)
78
+ hash[:time].in_time_zone(hash[:zone])
76
79
  end
77
80
  end
78
81
 
@@ -82,7 +85,7 @@ module IceCube
82
85
  # keep it. If it was serialized with a different offset than local TZ it
83
86
  # will lose the zone and not support DST.
84
87
  def self.restore_deserialized_offset(time, orig_offset_str)
85
- return time if time.respond_to?(:time_zone) or
88
+ return time if time.respond_to?(:time_zone) ||
86
89
  time.getlocal(orig_offset_str).utc_offset == time.utc_offset
87
90
  warn 'IceCube: parsed Time from nonlocal TZ. Use ActiveSupport to fix DST'
88
91
  time.localtime(orig_offset_str)
@@ -112,22 +115,31 @@ module IceCube
112
115
 
113
116
  # Convert a symbol to a numeric month
114
117
  def self.sym_to_month(sym)
115
- return wday = sym if (1..12).include? sym
116
- MONTHS.fetch(sym) { |k| raise KeyError, "No such month: #{k}" }
118
+ MONTHS.fetch(sym) do |k|
119
+ return wday = sym.to_i if MONTHS.values.any? { |i| i.to_s == sym.to_s }
120
+ raise ArgumentError, "Expecting Fixnum or Symbol value for month. " \
121
+ "No such month: #{k.inspect}"
122
+ end
117
123
  end
118
124
  deprecated_alias :symbol_to_month, :sym_to_month
119
125
 
120
126
  # Convert a symbol to a wday number
121
127
  def self.sym_to_wday(sym)
122
- return sym if (0..6).include? sym
123
- DAYS.fetch(sym) { |k| raise KeyError, "No such weekday: #{k}" }
128
+ DAYS.fetch(sym) do |k|
129
+ return sym.to_i if DAYS.values.any? { |i| i.to_s == sym.to_s }
130
+ raise ArgumentError, "Expecting Fixnum or Symbol value for weekday. " \
131
+ "No such weekday: #{k.inspect}"
132
+ end
124
133
  end
125
134
  deprecated_alias :symbol_to_day, :sym_to_wday
126
135
 
127
136
  # Convert wday number to day symbol
128
137
  def self.wday_to_sym(wday)
129
138
  return sym = wday if DAYS.keys.include? wday
130
- DAYS.invert.fetch(wday) { |i| raise KeyError, "No such wday number: #{i}" }
139
+ DAYS.invert.fetch(wday) do |i|
140
+ raise ArgumentError, "Expecting Fixnum value for weekday. " \
141
+ "No such wday number: #{i.inspect}"
142
+ end
131
143
  end
132
144
 
133
145
  # Convert weekday from base sunday to the schedule's week start.
@@ -200,6 +212,10 @@ module IceCube
200
212
  end
201
213
  end
202
214
 
215
+ def self.same_clock?(t1, t2)
216
+ CLOCK_VALUES.all? { |i| t1.send(i) == t2.send(i) }
217
+ end
218
+
203
219
  # A utility class for safely moving time around
204
220
  class TimeWrapper
205
221
 
@@ -46,6 +46,14 @@ module IceCube
46
46
  @time
47
47
  end
48
48
 
49
+ def skipped_for_dst
50
+ @uses -= 1 if @uses > 0
51
+ end
52
+
53
+ def dst_adjust?
54
+ @validations[:interval].any? &:dst_adjust?
55
+ end
56
+
49
57
  def to_s
50
58
  builder = StringBuilder.new
51
59
  @validations.each do |name, validations|
@@ -132,12 +140,11 @@ module IceCube
132
140
  end
133
141
 
134
142
  def shift_time_by_validation(res, vals)
135
- return unless res.min
136
- type = vals.first.type # get the jump type
137
- dst_adjust = !vals.first.respond_to?(:dst_adjust?) || vals.first.dst_adjust?
138
- wrapper = TimeUtil::TimeWrapper.new(@time, dst_adjust)
139
- wrapper.add(type, res.min)
140
- wrapper.clear_below(type)
143
+ return unless (interval = res.min)
144
+ validation = vals.first
145
+ wrapper = TimeUtil::TimeWrapper.new(@time, validation.dst_adjust?)
146
+ wrapper.add(validation.type, interval)
147
+ wrapper.clear_below(validation.type)
141
148
 
142
149
  # Move over DST if blocked, no adjustments
143
150
  if wrapper.to_time <= @time
@@ -2,7 +2,7 @@ module IceCube
2
2
 
3
3
  module Validations::Count
4
4
 
5
- # accessor
5
+ # Value reader for limit
6
6
  def occurrence_count
7
7
  @count
8
8
  end
@@ -12,7 +12,7 @@ module IceCube
12
12
  raise ArgumentError, "Expecting Fixnum or nil value for count, got #{max.inspect}"
13
13
  end
14
14
  @count = max
15
- replace_validations_for(:count, [Validation.new(max, self)])
15
+ replace_validations_for(:count, max && [Validation.new(max, self)])
16
16
  self
17
17
  end
18
18
 
@@ -26,13 +26,15 @@ module IceCube
26
26
  end
27
27
 
28
28
  def type
29
- :dealbreaker
29
+ :limit
30
+ end
31
+
32
+ def dst_adjust?
33
+ false
30
34
  end
31
35
 
32
36
  def validate(time, schedule)
33
- if rule.uses && rule.uses >= count
34
- raise CountExceeded
35
- end
37
+ raise CountExceeded if rule.uses && rule.uses >= count
36
38
  end
37
39
 
38
40
  def build_s(builder)
@@ -10,8 +10,6 @@ module IceCube
10
10
  self
11
11
  end
12
12
 
13
- # A validation for checking to make sure that a time
14
- # is inside of a certain DailyInterval
15
13
  class Validation
16
14
 
17
15
  attr_reader :interval
@@ -20,6 +18,22 @@ module IceCube
20
18
  @interval = interval
21
19
  end
22
20
 
21
+ def type
22
+ :day
23
+ end
24
+
25
+ def dst_adjust?
26
+ true
27
+ end
28
+
29
+ def validate(step_time, schedule)
30
+ t0, t1 = schedule.start_time, step_time
31
+ days = Date.new(t1.year, t1.month, t1.day) -
32
+ Date.new(t0.year, t0.month, t0.day)
33
+ offset = (days % interval).nonzero?
34
+ interval - offset if offset
35
+ end
36
+
23
37
  def build_s(builder)
24
38
  builder.base = interval == 1 ? 'Daily' : "Every #{interval} days"
25
39
  end
@@ -30,22 +44,7 @@ module IceCube
30
44
 
31
45
  def build_ical(builder)
32
46
  builder['FREQ'] << 'DAILY'
33
- unless interval == 1
34
- builder['INTERVAL'] << interval
35
- end
36
- end
37
-
38
- def type
39
- :day
40
- end
41
-
42
- def validate(time, schedule)
43
- time_date = Date.new(time.year, time.month, time.day)
44
- start_date = Date.new(schedule.start_time.year, schedule.start_time.month, schedule.start_time.day)
45
- days = time_date - start_date
46
- unless days % interval === 0
47
- interval - (days % interval)
48
- end
47
+ builder['INTERVAL'] << interval unless interval == 1
49
48
  end
50
49
 
51
50
  end
@@ -27,6 +27,14 @@ module IceCube
27
27
  @day = day
28
28
  end
29
29
 
30
+ def type
31
+ :wday
32
+ end
33
+
34
+ def dst_adjust?
35
+ true
36
+ end
37
+
30
38
  def build_s(builder)
31
39
  builder.piece(:day) << day
32
40
  end
@@ -43,10 +51,6 @@ module IceCube
43
51
  end
44
52
  end
45
53
 
46
- def type
47
- :wday
48
- end
49
-
50
54
  StringBuilder.register_formatter(:day) do |validation_days|
51
55
  # sort the days
52
56
  validation_days.sort!
@@ -2,8 +2,6 @@ module IceCube
2
2
 
3
3
  module Validations::DayOfMonth
4
4
 
5
- include Validations::Lock
6
-
7
5
  def day_of_month(*days)
8
6
  days.flatten.each do |day|
9
7
  unless day.is_a?(Fixnum)
@@ -19,12 +17,6 @@ module IceCube
19
17
 
20
18
  include Validations::Lock
21
19
 
22
- StringBuilder.register_formatter(:day_of_month) do |entries|
23
- str = "on the #{StringBuilder.sentence(entries)} "
24
- str << (entries.size == 1 ? 'day of the month' : 'days of the month')
25
- str
26
- end
27
-
28
20
  attr_reader :day
29
21
  alias :value :day
30
22
 
@@ -32,6 +24,14 @@ module IceCube
32
24
  @day = day
33
25
  end
34
26
 
27
+ def type
28
+ :day
29
+ end
30
+
31
+ def dst_adjust?
32
+ true
33
+ end
34
+
35
35
  def build_s(builder)
36
36
  builder.piece(:day_of_month) << StringBuilder.nice_number(day)
37
37
  end
@@ -44,8 +44,10 @@ module IceCube
44
44
  builder['BYMONTHDAY'] << day
45
45
  end
46
46
 
47
- def type
48
- :day
47
+ StringBuilder.register_formatter(:day_of_month) do |entries|
48
+ str = "on the #{StringBuilder.sentence(entries)} "
49
+ str << (entries.size == 1 ? 'day of the month' : 'days of the month')
50
+ str
49
51
  end
50
52
 
51
53
  end
@@ -17,23 +17,35 @@ module IceCube
17
17
 
18
18
  attr_reader :day, :occ
19
19
 
20
- StringBuilder.register_formatter(:day_of_week) do |segments|
21
- 'on the ' + segments.join(' and ')
20
+ def initialize(day, occ)
21
+ @day = day
22
+ @occ = occ
22
23
  end
23
24
 
24
25
  def type
25
26
  :day
26
27
  end
27
28
 
28
- def build_s(builder)
29
- builder.piece(:day_of_week) << "#{StringBuilder.nice_number(occ)} #{Date::DAYNAMES[day]}"
29
+ def dst_adjust?
30
+ true
30
31
  end
31
32
 
32
- def build_ical(builder)
33
- ical_day = IcalBuilder.fixnum_to_ical_day(day)
34
- # Delete any with this day and no occ first
35
- builder['BYDAY'].delete_if { |d| d == ical_day }
36
- builder['BYDAY'] << "#{occ}#{ical_day}"
33
+ def validate(step_time, schedule)
34
+ wday = step_time.wday
35
+ offset = (day < wday) ? (7 - wday + day) : (day - wday)
36
+ wrapper = TimeUtil::TimeWrapper.new(step_time)
37
+ wrapper.add :day, offset
38
+ loop do
39
+ which_occ, num_occ = TimeUtil.which_occurrence_in_month(wrapper.to_time, day)
40
+ this_occ = (occ < 0) ? (num_occ + occ + 1) : (occ)
41
+ break offset if which_occ == this_occ
42
+ wrapper.add :day, 7
43
+ offset += 7
44
+ end
45
+ end
46
+
47
+ def build_s(builder)
48
+ builder.piece(:day_of_week) << "#{StringBuilder.nice_number(occ)} #{Date::DAYNAMES[day]}"
37
49
  end
38
50
 
39
51
  def build_hash(builder)
@@ -42,25 +54,15 @@ module IceCube
42
54
  arr << occ
43
55
  end
44
56
 
45
- def initialize(day, occ)
46
- @day = day
47
- @occ = occ
57
+ def build_ical(builder)
58
+ ical_day = IcalBuilder.fixnum_to_ical_day(day)
59
+ # Delete any with this day and no occ first
60
+ builder['BYDAY'].delete_if { |d| d == ical_day }
61
+ builder['BYDAY'] << "#{occ}#{ical_day}"
48
62
  end
49
63
 
50
- def validate(time, schedule)
51
- # count the days to the weekday
52
- sum = day >= time.wday ? day - time.wday : 7 - time.wday + day
53
- wrapper = TimeUtil::TimeWrapper.new(time)
54
- wrapper.add :day, sum
55
- # and then count the week until a viable occ
56
- loop do
57
- which_occ, num_occ = TimeUtil.which_occurrence_in_month(wrapper.to_time, day)
58
- this_occ = occ < 0 ? num_occ + occ + 1 : occ
59
- break if which_occ == this_occ
60
- sum += 7
61
- wrapper.add :day, 7 # one week
62
- end
63
- sum
64
+ StringBuilder.register_formatter(:day_of_week) do |segments|
65
+ 'on the ' + segments.join(' and ')
64
66
  end
65
67
 
66
68
  end