Skip to content

Commit

Permalink
Merge pull request travisjeffery#73 from tommeier/master
Browse files Browse the repository at this point in the history
[Bug fix] Timezones travel/freeze losing zone awareness on to_date or Date.today
  • Loading branch information
Travis Jeffery committed Mar 8, 2013
2 parents 0d6c58f + 0dc15bc commit 8c4df10
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
19 changes: 11 additions & 8 deletions lib/timecop/time_stack_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,21 @@ def scaling_factor
@scaling_factor
end

def time(time_klass = Time) #:nodoc:
def time(klass = time_klass) #:nodoc:
begin
actual_time = time_klass.at(@time)
calculated_time = time_klass.at(@time.to_f)
actual_time = klass.at(@time)
calculated_time = klass.at(@time.to_f)
time = times_are_equal_within_epsilon(actual_time, calculated_time, 1) ? actual_time : calculated_time
rescue
time = time_klass.at(@time.to_f)
time = klass.at(@time.to_f)
end

if travel_offset.nil?
time
elsif scaling_factor.nil?
time_klass.at(Time.now_without_mock_time + travel_offset)
klass.at(Time.now_without_mock_time + travel_offset)
else
time_klass.at(scaled_time)
klass.at(scaled_time)
end
end

Expand Down Expand Up @@ -103,7 +103,6 @@ def utc_offset_to_rational(utc_offset)
end

def parse_time(*args)
time_klass = Time.respond_to?(:zone) && Time.zone ? Time.zone : Time
arg = args.shift
if arg.is_a?(Time)
if Timecop.active_support != false && arg.respond_to?(:in_time_zone)
Expand Down Expand Up @@ -151,5 +150,9 @@ def compute_travel_offset
def times_are_equal_within_epsilon t1, t2, epsilon_in_seconds
(t1 - t2).abs < epsilon_in_seconds
end

def time_klass
Time.respond_to?(:zone) ? Time.zone : Time
end
end
end
end
2 changes: 1 addition & 1 deletion lib/timecop/timecop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def top_stack_item #:nodoc:
private
def send_travel(mock_type, *args, &block)
val = instance.send(:travel, mock_type, *args, &block)
block_given? ? val : Time.now
block_given? ? val : Time.now
end
end

Expand Down
12 changes: 11 additions & 1 deletion test/time_stack_item_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@ def test_timezones
end
end

def test_timezones_apply_dates
require 'active_support/all'
Time.zone = "Marshall Is."
time = Time.zone.local(2013,1,3)

Timecop.freeze(time) do
assert_equal time.to_date, Date.today
end
end

def test_set_scaling_factor_for_scale
t_now = Time.now
t = Time.local(2009, 10, 1, 0, 0, 30)
Expand Down Expand Up @@ -247,7 +257,7 @@ def test_nsecs_are_set

def test_time_with_different_timezone
require 'active_support/all'

Time.zone = "Tokyo"
t = Time.now
Timecop.freeze(t) do
Expand Down

0 comments on commit 8c4df10

Please sign in to comment.