timecop 0.5.6 → 0.5.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/timecop/time_stack_item.rb +82 -71
- data/lib/timecop/version.rb +1 -1
- data/test/time_stack_item_test.rb +9 -0
- metadata +2 -2
@@ -1,91 +1,98 @@
|
|
1
|
-
|
2
1
|
class Timecop
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
def year
|
19
|
-
time.year
|
20
|
-
end
|
2
|
+
# A data class for carrying around "time movement" objects. Makes it easy to keep track of the time
|
3
|
+
# movements on a simple stack.
|
4
|
+
class TimeStackItem #:nodoc:
|
5
|
+
attr_reader :mock_type
|
6
|
+
|
7
|
+
def initialize(mock_type, *args)
|
8
|
+
raise "Unknown mock_type #{mock_type}" unless [:freeze, :travel, :scale].include?(mock_type)
|
9
|
+
@scaling_factor = args.shift if mock_type == :scale
|
10
|
+
@mock_type = mock_type
|
11
|
+
@time = parse_time(*args)
|
12
|
+
@time_was = Time.now_without_mock_time
|
13
|
+
@travel_offset = compute_travel_offset
|
14
|
+
@dst_adjustment = compute_dst_adjustment(@time)
|
15
|
+
end
|
21
16
|
|
22
|
-
|
23
|
-
|
24
|
-
|
17
|
+
def year
|
18
|
+
time.year
|
19
|
+
end
|
25
20
|
|
26
|
-
|
27
|
-
|
28
|
-
|
21
|
+
def month
|
22
|
+
time.month
|
23
|
+
end
|
29
24
|
|
30
|
-
|
31
|
-
|
32
|
-
|
25
|
+
def day
|
26
|
+
time.day
|
27
|
+
end
|
33
28
|
|
34
|
-
|
35
|
-
|
36
|
-
|
29
|
+
def hour
|
30
|
+
time.hour
|
31
|
+
end
|
37
32
|
|
38
|
-
|
39
|
-
|
40
|
-
|
33
|
+
def min
|
34
|
+
time.min
|
35
|
+
end
|
41
36
|
|
42
|
-
|
43
|
-
|
44
|
-
|
37
|
+
def sec
|
38
|
+
time.sec
|
39
|
+
end
|
45
40
|
|
46
|
-
|
47
|
-
|
48
|
-
|
41
|
+
def utc_offset
|
42
|
+
time.utc_offset
|
43
|
+
end
|
49
44
|
|
50
|
-
|
51
|
-
|
52
|
-
|
45
|
+
def travel_offset
|
46
|
+
@travel_offset
|
47
|
+
end
|
53
48
|
|
54
|
-
|
55
|
-
|
56
|
-
time_klass.at(@time)
|
57
|
-
elsif scaling_factor.nil?
|
58
|
-
time_klass.at(Time.now_without_mock_time + travel_offset)
|
59
|
-
else
|
60
|
-
time_klass.at(scaled_time)
|
49
|
+
def scaling_factor
|
50
|
+
@scaling_factor
|
61
51
|
end
|
62
|
-
end
|
63
52
|
|
64
|
-
|
65
|
-
|
66
|
-
|
53
|
+
def time(time_klass = Time) #:nodoc:
|
54
|
+
actual_time = time_klass.at(@time)
|
55
|
+
calculated_time = time_klass.at(@time.to_f)
|
67
56
|
|
68
|
-
|
69
|
-
|
70
|
-
|
57
|
+
if travel_offset.nil?
|
58
|
+
if times_are_equal_within_epsilon(actual_time, calculated_time, 1)
|
59
|
+
actual_time
|
60
|
+
else
|
61
|
+
calculated_time
|
62
|
+
end
|
63
|
+
elsif scaling_factor.nil?
|
64
|
+
time_klass.at(Time.now_without_mock_time + travel_offset)
|
65
|
+
else
|
66
|
+
time_klass.at(scaled_time)
|
67
|
+
end
|
68
|
+
end
|
71
69
|
|
72
|
-
|
73
|
-
|
70
|
+
def scaled_time
|
71
|
+
(@time + (Time.now_without_mock_time - @time_was) * scaling_factor).to_f
|
72
|
+
end
|
73
|
+
|
74
|
+
def date(date_klass = Date)
|
75
|
+
date_klass.jd(time.__send__(:to_date).jd)
|
76
|
+
end
|
74
77
|
|
75
|
-
|
76
|
-
fractions_of_a_second = time.to_f % 1
|
77
|
-
datetime_klass.new(year, month, day, hour, min, sec + fractions_of_a_second, utc_offset_to_rational(our_offset))
|
78
|
-
else
|
78
|
+
def datetime(datetime_klass = DateTime)
|
79
79
|
our_offset = utc_offset + dst_adjustment
|
80
|
-
|
80
|
+
|
81
|
+
if Float.method_defined?(:to_r)
|
82
|
+
fractions_of_a_second = time.to_f % 1
|
83
|
+
datetime_klass.new(year, month, day, hour, min, sec + fractions_of_a_second, utc_offset_to_rational(our_offset))
|
84
|
+
else
|
85
|
+
our_offset = utc_offset + dst_adjustment
|
86
|
+
datetime_klass.new(year, month, day, hour, min, sec, utc_offset_to_rational(our_offset))
|
87
|
+
end
|
81
88
|
end
|
82
|
-
end
|
83
89
|
|
84
|
-
|
85
|
-
|
86
|
-
|
90
|
+
def dst_adjustment
|
91
|
+
@dst_adjustment
|
92
|
+
end
|
93
|
+
|
94
|
+
private
|
87
95
|
|
88
|
-
private
|
89
96
|
def rational_to_utc_offset(rational)
|
90
97
|
((24.0 / rational.denominator) * rational.numerator) * (60 * 60)
|
91
98
|
end
|
@@ -139,5 +146,9 @@ class Timecop
|
|
139
146
|
return nil if mock_type == :freeze
|
140
147
|
time - Time.now_without_mock_time
|
141
148
|
end
|
142
|
-
|
143
|
-
|
149
|
+
|
150
|
+
def times_are_equal_within_epsilon t1, t2, epsilon_in_seconds
|
151
|
+
(t1 - t2).abs < epsilon_in_seconds
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
data/lib/timecop/version.rb
CHANGED
@@ -183,6 +183,15 @@ class TestTimeStackItem < Test::Unit::TestCase
|
|
183
183
|
assert_equal nil, tsi.send(:travel_offset)
|
184
184
|
end
|
185
185
|
|
186
|
+
def test_timezones
|
187
|
+
require 'active_support/all'
|
188
|
+
Time.zone = "Europe/Zurich"
|
189
|
+
time = Time.zone.parse("2012-12-27T12:12:12+08:00")
|
190
|
+
Timecop.freeze(time) do |frozen_time|
|
191
|
+
assert_equal time, frozen_time
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
186
195
|
def test_set_scaling_factor_for_scale
|
187
196
|
t_now = Time.now
|
188
197
|
t = Time.local(2009, 10, 1, 0, 0, 30)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: timecop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-12-
|
13
|
+
date: 2012-12-27 00:00:00.000000000 Z
|
14
14
|
dependencies: []
|
15
15
|
description: A gem providing "time travel" and "time freezing" capabilities, making
|
16
16
|
it dead simple to test time-dependent code. It provides a unified method to mock
|