/
zone_spec.rb
299 lines (238 loc) · 8.33 KB
/
zone_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
require_relative '../spec_helper'
module Barometer::Data
RSpec.describe Zone do
describe '#new' do
let(:zone) { double(:zone) }
before do
allow(ZoneFull).to receive(:detect?).and_return(false)
allow(ZoneOffset).to receive(:detect?).and_return(false)
allow(ZoneCode).to receive(:detect?).and_return(false)
end
it 'detects a full timezone input' do
allow(ZoneFull).to receive_messages(detect?: true, new: nil)
Zone.new(zone)
expect(ZoneFull).to have_received(:new).with(zone)
end
it 'detects a timezone code input' do
allow(ZoneOffset).to receive_messages(detect?: true, new: nil)
Zone.new(zone)
expect(ZoneOffset).to have_received(:new).with(zone)
end
it 'detects a timezone offset input' do
allow(ZoneCode).to receive_messages(detect?: true, new: nil)
Zone.new(zone)
expect(ZoneCode).to have_received(:new).with(zone)
end
it 'raises an error when nothing detected' do
expect {
Zone.new(zone)
}.to raise_error(ArgumentError)
end
end
end
RSpec.describe ZoneFull do
def stub_time(utc_now)
now = double(:now, utc: utc_now)
double(:time_class, now: now)
end
describe '.detect?' do
it 'returns true when given a full timezone' do
expect( ZoneFull.detect?('America/Los_Angeles') ).to be true
end
it 'returns false when given a timezone code' do
expect( ZoneFull.detect?('PST') ).to be false
end
it 'returns false when given an offset' do
expect( ZoneFull.detect?(10) ).to be false
end
it 'returns false when given nothing' do
expect( ZoneFull.detect?('') ).to be false
expect( ZoneFull.detect?(nil) ).to be false
end
end
describe '#code' do
it 'returns the correct non-DST zone code' do
time = stub_time(::Time.utc(2013, 1, 1))
zone = ZoneFull.new('America/Los_Angeles', time)
expect( zone.code ).to eq 'PST'
end
it 'returns the correct DST zone code' do
time = stub_time(::Time.utc(2013, 6, 1))
zone = ZoneFull.new('America/Los_Angeles', time)
expect( zone.code ).to eq 'PDT'
end
end
describe '#offset' do
it 'returns the current non-DST offset' do
time = stub_time(::Time.utc(2013, 1, 1, 18, 0, 0))
zone = ZoneFull.new('America/Los_Angeles', time)
expect( zone.offset ).to eq(-8 * 60 * 60)
end
it 'returns the current DST offset' do
time = stub_time(::Time.utc(2013, 6, 1, 18, 0, 0))
zone = ZoneFull.new('America/Los_Angeles', time)
expect( zone.offset ).to eq(-7 * 60 * 60)
end
end
describe '#now' do
it 'returns the current non-DST local time' do
time = stub_time(::Time.utc(2013, 1, 1, 18, 0, 0))
zone = ZoneFull.new('America/Los_Angeles', time)
expect( zone.now ).to eq ::Time.utc(2013, 1, 1, 10, 0, 0)
end
it 'returns the current DST local time' do
time = stub_time(::Time.utc(2013, 6, 1, 18, 0, 0))
zone = ZoneFull.new('America/Los_Angeles', time)
expect( zone.now ).to eq ::Time.utc(2013, 6, 1, 11, 0, 0)
end
end
describe '#to_s' do
it 'returns the input zone' do
expect( ZoneFull.new('Europe/Paris').to_s ).to eq 'Europe/Paris'
end
end
describe '#local_to_utc' do
it 'converts a time in the local time zone to UTC' do
zone = ZoneFull.new('America/Los_Angeles')
local_time = ::Time.now.utc
expect( zone.local_to_utc(local_time).to_i ).to eq((local_time - zone.offset).to_i)
end
end
describe '#utc_to_local' do
it 'converts a time in the local time zone to UTC' do
zone = ZoneFull.new('America/Los_Angeles')
utc_time = ::Time.now.utc
expect( zone.utc_to_local(utc_time).to_i ).to eq((utc_time + zone.offset).to_i)
end
end
end
RSpec.describe ZoneOffset do
def stub_time(utc_now)
now = double(:now, utc: utc_now)
double(:time_class, now: now)
end
describe '.detect?' do
it 'returns false when given a full timezone' do
expect( ZoneOffset.detect?('America/Los_Angeles') ).to be false
end
it 'returns false when given a timezone code' do
expect( ZoneOffset.detect?('PST') ).to be false
end
it 'returns true when given an offset' do
expect( ZoneOffset.detect?(10) ).to be true
end
it 'returns false when given an offset out of range' do
expect( ZoneOffset.detect?(15) ).to be false
end
it 'returns false when given nothing' do
expect( ZoneOffset.detect?('') ).to be false
expect( ZoneOffset.detect?(nil) ).to be false
end
end
describe '#code' do
it 'returns nil' do
expect( ZoneOffset.new(10).code ).to be_nil
end
end
describe '#offset' do
it 'converts the input from hours to seconds' do
expect( ZoneOffset.new(5).offset ).to eq(5 * 60 * 60)
end
end
describe '#now' do
it 'returns the current local time' do
time = stub_time(::Time.utc(2013, 1, 1, 10, 0, 0))
zone = ZoneOffset.new(5, time)
expect( zone.now ).to eq ::Time.utc(2013, 1, 1, 15, 0, 0)
end
end
describe '#to_s' do
it 'returns the input zone' do
expect( ZoneOffset.new(5).to_s ).to eq '5'
end
end
describe '#local_to_utc' do
it 'converts a time in the local time zone to UTC' do
zone = ZoneOffset.new(5)
local_time = ::Time.now.utc
expect( zone.local_to_utc(local_time).to_i ).to eq((local_time - zone.offset).to_i)
end
end
describe '#utc_to_local' do
it 'converts a time in the local time zone to UTC' do
zone = ZoneOffset.new(5)
utc_time = ::Time.now.utc
expect( zone.utc_to_local(utc_time).to_i ).to eq((utc_time + zone.offset).to_i)
end
end
end
RSpec.describe ZoneCode do
def stub_time(utc_now)
now = double(:now, utc: utc_now)
double(:time_class, now: now)
end
describe '.detect?' do
it 'returns false when given a full timezone' do
expect( ZoneCode.detect?('America/Los_Angeles') ).to be false
end
it 'returns true when given a timezone code' do
expect( ZoneCode.detect?('PST') ).to be true
end
it 'returns true when given an obscure timezone code' do
expect( ZoneCode.detect?('CEST') ).to be true
end
it 'returns false when given an invalid timezone code' do
expect( ZoneCode.detect?('ABC') ).to be false
end
it 'returns false when given an offset' do
expect( ZoneCode.detect?(10) ).to be false
end
it 'returns false when given nothing' do
expect( ZoneCode.detect?('') ).to be false
expect( ZoneCode.detect?(nil) ).to be false
end
end
describe '#code' do
it 'returns the input code' do
expect( ZoneCode.new('PST').code ).to eq 'PST'
end
end
describe '#offset' do
it 'returns the offset in seconds' do
expect( ZoneCode.new('PST').offset ).to eq(-8 * 60 * 60)
end
it 'returns the offset in seconds for an obscure input code' do
expect( ZoneCode.new('CEST').offset ).to eq(2 * 60 * 60)
end
it 'returns 0 for unknown codes' do
expect( ZoneCode.new('ABC').offset ).to be_zero
end
end
describe '#now' do
it 'returns the current local time' do
time = stub_time(::Time.utc(2013, 1, 1, 10, 0, 0))
zone = ZoneCode.new('PST', time)
expect( zone.now ).to eq ::Time.utc(2013, 1, 1, 2, 0, 0)
end
end
describe '#to_s' do
it 'returns the input zone' do
expect( ZoneCode.new('PST').to_s ).to eq 'PST'
end
end
describe '#local_to_utc' do
it 'converts a time in the local time zone to UTC' do
zone = ZoneCode.new('PST')
local_time = ::Time.now.utc
expect( zone.local_to_utc(local_time).to_i ).to eq((local_time - zone.offset).to_i)
end
end
describe '#utc_to_local' do
it 'converts a time in the local time zone to UTC' do
zone = ZoneCode.new('PST')
utc_time = ::Time.now.utc
expect( zone.utc_to_local(utc_time).to_i ).to eq((utc_time + zone.offset).to_i)
end
end
end
end