timecop 0.5.6 → 0.5.7

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.
@@ -1,91 +1,98 @@
1
-
2
1
  class Timecop
3
- # A data class for carrying around "time movement" objects. Makes it easy to keep track of the time
4
- # movements on a simple stack.
5
- class TimeStackItem #:nodoc:
6
- attr_reader :mock_type
7
-
8
- def initialize(mock_type, *args)
9
- raise "Unknown mock_type #{mock_type}" unless [:freeze, :travel, :scale].include?(mock_type)
10
- @scaling_factor = args.shift if mock_type == :scale
11
- @mock_type = mock_type
12
- @time = parse_time(*args)
13
- @time_was = Time.now_without_mock_time
14
- @travel_offset = compute_travel_offset
15
- @dst_adjustment = compute_dst_adjustment(@time)
16
- end
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
- def month
23
- time.month
24
- end
17
+ def year
18
+ time.year
19
+ end
25
20
 
26
- def day
27
- time.day
28
- end
21
+ def month
22
+ time.month
23
+ end
29
24
 
30
- def hour
31
- time.hour
32
- end
25
+ def day
26
+ time.day
27
+ end
33
28
 
34
- def min
35
- time.min
36
- end
29
+ def hour
30
+ time.hour
31
+ end
37
32
 
38
- def sec
39
- time.sec
40
- end
33
+ def min
34
+ time.min
35
+ end
41
36
 
42
- def utc_offset
43
- time.utc_offset
44
- end
37
+ def sec
38
+ time.sec
39
+ end
45
40
 
46
- def travel_offset
47
- @travel_offset
48
- end
41
+ def utc_offset
42
+ time.utc_offset
43
+ end
49
44
 
50
- def scaling_factor
51
- @scaling_factor
52
- end
45
+ def travel_offset
46
+ @travel_offset
47
+ end
53
48
 
54
- def time(time_klass = Time) #:nodoc:
55
- if travel_offset.nil?
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
- def scaled_time
65
- (@time + (Time.now_without_mock_time - @time_was) * scaling_factor).to_f
66
- end
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
- def date(date_klass = Date)
69
- date_klass.jd(time.__send__(:to_date).jd)
70
- end
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
- def datetime(datetime_klass = DateTime)
73
- our_offset = utc_offset + dst_adjustment
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
- if Float.method_defined?(:to_r)
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
- datetime_klass.new(year, month, day, hour, min, sec, utc_offset_to_rational(our_offset))
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
- def dst_adjustment
85
- @dst_adjustment
86
- end
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
- end
143
- end
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
@@ -1,3 +1,3 @@
1
1
  class Timecop
2
- VERSION = "0.5.6"
2
+ VERSION = "0.5.7"
3
3
  end
@@ -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.6
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-26 00:00:00.000000000 Z
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