Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

using a time's float value to re-calcuate itself is clearly not going…

… to be as accurate as just using a time object itself (fix #56)
  • Loading branch information...
commit 9e11ccfe0b3f0e8e568e1a138c15abc184a0d93f 1 parent 26f7e26
Travis Jeffery authored
42 lib/timecop/time_stack_item.rb
View
@@ -4,7 +4,7 @@ class Timecop
# movements on a simple stack.
class TimeStackItem #:nodoc:
attr_reader :mock_type
-
+
def initialize(mock_type, *args)
raise "Unknown mock_type #{mock_type}" unless [:freeze, :travel, :scale].include?(mock_type)
@scaling_factor = args.shift if mock_type == :scale
@@ -14,35 +14,35 @@ def initialize(mock_type, *args)
@travel_offset = compute_travel_offset
@dst_adjustment = compute_dst_adjustment(@time)
end
-
+
def year
time.year
end
-
+
def month
time.month
end
-
+
def day
time.day
end
-
+
def hour
time.hour
end
-
+
def min
time.min
end
-
+
def sec
time.sec
end
-
+
def utc_offset
time.utc_offset
end
-
+
def travel_offset
@travel_offset
end
@@ -50,12 +50,12 @@ def travel_offset
def scaling_factor
@scaling_factor
end
-
+
def time(time_klass = Time) #:nodoc:
if travel_offset.nil?
- time_klass.at(@time.to_f)
+ time_klass.at(@time)
elsif scaling_factor.nil?
- time_klass.at((Time.now_without_mock_time + travel_offset).to_f)
+ time_klass.at(Time.now_without_mock_time + travel_offset)
else
time_klass.at(scaled_time)
end
@@ -64,11 +64,11 @@ def time(time_klass = Time) #:nodoc:
def scaled_time
(@time + (Time.now_without_mock_time - @time_was) * scaling_factor).to_f
end
-
+
def date(date_klass = Date)
date_klass.jd(time.__send__(:to_date).jd)
end
-
+
def datetime(datetime_klass = DateTime)
our_offset = utc_offset + dst_adjustment
@@ -80,24 +80,24 @@ def datetime(datetime_klass = DateTime)
datetime_klass.new(year, month, day, hour, min, sec, utc_offset_to_rational(our_offset))
end
end
-
+
def dst_adjustment
@dst_adjustment
end
-
+
private
def rational_to_utc_offset(rational)
((24.0 / rational.denominator) * rational.numerator) * (60 * 60)
end
-
+
def utc_offset_to_rational(utc_offset)
Rational(utc_offset, 24 * 60 * 60)
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 arg.is_a?(Time)
if Timecop.active_support != false && arg.respond_to?(:in_time_zone)
arg.in_time_zone
else
@@ -128,13 +128,13 @@ def parse_time(*args)
end
end
end
-
+
def compute_dst_adjustment(time)
return 0 if !(time.dst? ^ Time.now.dst?)
return -1 * 60 * 60 if time.dst?
return 60 * 60
end
-
+
def compute_travel_offset
return nil if mock_type == :freeze
time - Time.now_without_mock_time
2  lib/timecop/version.rb
View
@@ -1,3 +1,3 @@
class Timecop
- VERSION = "0.5.5"
+ VERSION = "0.5.6"
end
37 test/time_stack_item_test.rb
View
@@ -7,7 +7,7 @@ def teardown
Timecop.active_support = nil
Timecop.return
end
-
+
def test_new_with_time
t = Time.now
y, m, d, h, min, s = t.year, t.month, t.day, t.hour, t.min, t.sec
@@ -33,7 +33,7 @@ def test_new_with_time_and_arguments
assert_equal min, stack_item.min
assert_equal s, stack_item.sec
end
-
+
def test_new_with_datetime_now
t = DateTime.now
y, m, d, h, min, s = t.year, t.month, t.day, t.hour, t.min, t.sec
@@ -46,7 +46,7 @@ def test_new_with_datetime_now
assert_equal min, stack_item.min
assert_equal s, stack_item.sec
end
-
+
def test_new_with_datetime_in_different_timezone
each_timezone do
t = DateTime.parse("2009-10-11 00:38:00 +0200")
@@ -55,7 +55,7 @@ def test_new_with_datetime_in_different_timezone
assert_date_times_equal(t, stack_item.datetime)
end
end
-
+
def test_new_with_date
date = Date.today
y, m, d, h, min, s = date.year, date.month, date.day, 0, 0, 0
@@ -68,7 +68,7 @@ def test_new_with_date
assert_equal min, stack_item.min
assert_equal s, stack_item.sec
end
-
+
# Due to the nature of this test (calling Time.now once in this test and
# once in #new), this test may fail when two subsequent calls
# to Time.now return a different second.
@@ -84,7 +84,7 @@ def test_new_with_integer
assert_equal min, stack_item.min
assert_equal s, stack_item.sec
end
-
+
def test_new_with_individual_arguments
y, m, d, h, min, s = 2008, 10, 10, 10, 10, 10
stack_item = Timecop::TimeStackItem.new(:freeze, y, m, d, h, min, s)
@@ -96,14 +96,14 @@ def test_new_with_individual_arguments
assert_equal min, stack_item.min
assert_equal s, stack_item.sec
end
-
+
def test_rational_to_utc_offset
assert_equal -14400, a_time_stack_item.send(:rational_to_utc_offset, Rational(-1, 6))
assert_equal -18000, a_time_stack_item.send(:rational_to_utc_offset, Rational(-5, 24))
assert_equal 0, a_time_stack_item.send(:rational_to_utc_offset, Rational(0, 1))
assert_equal 3600, a_time_stack_item.send(:rational_to_utc_offset, Rational(1, 24))
end
-
+
def test_utc_offset_to_rational
assert_equal Rational(-1, 6), a_time_stack_item.send(:utc_offset_to_rational, -14400)
assert_equal Rational(-5, 24), a_time_stack_item.send(:utc_offset_to_rational, -18000)
@@ -119,16 +119,16 @@ def test_compute_dst_adjustment_for_dst_to_dst
assert_equal 0, tsi.send(:dst_adjustment)
end
-
+
def test_compute_dst_adjustment_for_non_dst_to_non_dst
Timecop.freeze(DateTime.parse("2009-12-1 00:38:00 -0400"))
t = DateTime.parse("2009-12-11 00:00:00 -0400")
tsi = Timecop::TimeStackItem.new(:freeze, t)
return if Time.now.dst? || tsi.time.dst?
- assert_equal 0, tsi.send(:dst_adjustment)
+ assert_equal 0, tsi.send(:dst_adjustment)
end
-
+
def test_compute_dst_adjustment_for_dst_to_non_dst
Timecop.freeze(DateTime.parse("2009-10-1 00:38:00 -0400"))
t = DateTime.parse("2009-12-11 00:00:00 -0400")
@@ -137,7 +137,7 @@ def test_compute_dst_adjustment_for_dst_to_non_dst
assert_equal 60 * 60, tsi.send(:dst_adjustment)
end
-
+
def test_compute_dst_adjustment_for_non_dst_to_dst
Timecop.freeze(DateTime.parse("2009-12-1 00:38:00 -0400"))
t = DateTime.parse("2009-10-11 00:00:00 -0400")
@@ -146,7 +146,7 @@ def test_compute_dst_adjustment_for_non_dst_to_dst
assert_equal -1 * 60 * 60, tsi.send(:dst_adjustment)
end
-
+
# Ensure DateTimes handle changing DST properly
def test_datetime_for_dst_to_non_dst
Timecop.freeze(DateTime.parse("2009-12-1 00:38:00 -0500"))
@@ -155,7 +155,7 @@ def test_datetime_for_dst_to_non_dst
assert_date_times_equal t, tsi.datetime
end
-
+
def test_datetime_for_non_dst_to_dst
Timecop.freeze(DateTime.parse("2009-10-11 00:00:00 -0400"))
t = DateTime.parse("2009-11-30 23:38:00 -0500")
@@ -174,7 +174,7 @@ def test_set_travel_offset_for_travel
assert_times_effectively_equal expected_offset, tsi.send(:travel_offset), 1, "Offset not calculated correctly"
end
-
+
def test_set_travel_offset_for_freeze
Timecop.freeze(2009, 10, 1, 0, 0, 0)
t = Time.local(2009, 10, 1, 0, 0, 30)
@@ -228,4 +228,11 @@ def test_parse_date
Timecop.freeze(Date.new(2012, 6, 9))
end
end
+
+ def test_nsecs_are_set
+ time = Time.now
+ Timecop.freeze time
+ assert_equal time, Time.now
+ assert_equal time.nsec, Time.now.nsec
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.