Skip to content

Commit ba34f0d

Browse files
authored
Support "until" Date with local Time conversion
Fixes ice-cube-ruby#327
2 parents aed3233 + f88d3fb commit ba34f0d

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

lib/ice_cube/time_util.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def self.build_in_zone(args, reference)
4242
end
4343

4444
def self.match_zone(input_time, reference)
45-
return unless time = ensure_time(input_time)
45+
return unless time = ensure_time(input_time, reference)
4646
time = if reference.respond_to? :time_zone
4747
time.in_time_zone(reference.time_zone)
4848
else
@@ -58,13 +58,21 @@ def self.match_zone(input_time, reference)
5858
end
5959

6060
# Ensure that this is either nil, or a time
61-
def self.ensure_time(time, date_eod = false)
61+
def self.ensure_time(time, reference = nil, date_eod = false)
6262
case time
6363
when DateTime
6464
warn "IceCube: DateTime support is deprecated (please use Time) at: #{ caller[2] }"
6565
Time.local(time.year, time.month, time.day, time.hour, time.min, time.sec)
6666
when Date
67-
date_eod ? end_of_date(time) : time.to_time
67+
if date_eod
68+
end_of_date(time, reference)
69+
else
70+
if reference
71+
build_in_zone([time.year, time.month, time.day], reference)
72+
else
73+
time.to_time
74+
end
75+
end
6876
else
6977
time
7078
end

lib/ice_cube/validations/until.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ def until_time
1111
deprecated_alias :until_date, :until_time
1212

1313
def until(time)
14-
time = TimeUtil.ensure_time(time, true)
1514
@until = time
1615
replace_validations_for(:until, time.nil? ? nil : [Validation.new(time)])
1716
self
@@ -34,7 +33,8 @@ def dst_adjust?
3433
end
3534

3635
def validate(step_time, schedule)
37-
raise UntilExceeded if step_time > time
36+
end_time = TimeUtil.ensure_time(time, schedule.start_time, true)
37+
raise UntilExceeded if step_time > end_time
3838
end
3939

4040
def build_s(builder)

spec/examples/ice_cube_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,21 @@
232232
Time.utc(2010, 11, 9, 5, 0, 0)]
233233
end
234234

235+
WORLD_TIME_ZONES.each do |zone|
236+
context "in #{zone}", :system_time_zone => zone do
237+
it 'works with a until date that is a Date, but the start date is UTC' do
238+
start_time = Time.utc(2016, 1, 1, 0, 0, 0)
239+
schedule = IceCube::Schedule.new(start_time)
240+
schedule.add_recurrence_rule IceCube::Rule.daily.until(Date.new(2016, 1, 2))
241+
times = schedule.all_occurrences
242+
expect(times).to eq [
243+
Time.utc(2016, 1, 1, 0, 0, 0),
244+
Time.utc(2016, 1, 2, 0, 0, 0)
245+
]
246+
end
247+
end
248+
end
249+
235250
it 'works with a monthly rule iterating on UTC' do
236251
start_time = Time.utc(2010, 4, 24, 15, 45, 0)
237252
schedule = IceCube::Schedule.new(start_time)

spec/examples/time_util_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,16 @@ module IceCube
102102

103103
WORLD_TIME_ZONES.each do |zone|
104104
context "in #{zone}", :system_time_zone => zone do
105+
let(:utc_time) { Time.utc(2014, 1, 1, 0, 0, 1) }
105106
let(:local_time) { Time.local(2014, 1, 1, 0, 0, 1) }
106107

107-
it 'converts Date to beginning of date of reference time' do
108+
it 'converts Date to beginning of date of local reference time' do
108109
expect(TimeUtil.match_zone(date, local_time)).to eq local_time - 1
109110
end
111+
112+
it 'converts Date to beginning of date of UTC reference time' do
113+
expect(TimeUtil.match_zone(date, utc_time)).to eq utc_time - 1
114+
end
110115
end
111116
end
112117

0 commit comments

Comments
 (0)