moments 0.0.1.alpha → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.editorconfig +12 -0
- data/.gitignore +2 -0
- data/.rspec +1 -1
- data/.rubocop.yml +81 -0
- data/.travis.yml +9 -4
- data/CHANGELOG.md +43 -0
- data/Gemfile +2 -0
- data/{LICENSE.txt → LICENSE.md} +0 -0
- data/README.md +29 -4
- data/Rakefile +2 -1
- data/lib/moments/difference.rb +132 -46
- data/lib/moments/version.rb +13 -4
- data/lib/moments.rb +5 -2
- data/moments.gemspec +18 -17
- data/spec/lib/moments/difference_spec.rb +726 -110
- data/spec/lib/moments_spec.rb +6 -15
- data/spec/spec_helper.rb +2 -6
- metadata +48 -20
@@ -1,194 +1,810 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
describe Moments::Difference do
|
4
|
-
let(:from)
|
5
|
-
|
4
|
+
let(:from) { Time.utc 2007, 1, 1 }
|
5
|
+
let(:to) { Time.utc 2012, 1, 1 }
|
6
|
+
|
7
|
+
describe '#new' do
|
8
|
+
context 'without arguments' do
|
9
|
+
it { expect { Moments::Difference.new }.to raise_error(ArgumentError) }
|
10
|
+
end
|
6
11
|
end
|
7
12
|
|
8
|
-
|
9
|
-
|
13
|
+
context '#to_hash' do
|
14
|
+
subject { Moments::Difference.new(from, to).to_hash }
|
15
|
+
|
16
|
+
describe 'order of keys' do
|
17
|
+
subject { super().keys }
|
18
|
+
|
19
|
+
it { is_expected.to eq %i[years months days hours minutes seconds] }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'with equal dates' do
|
23
|
+
let(:to) { from }
|
24
|
+
|
25
|
+
let(:expected_result) do
|
26
|
+
{
|
27
|
+
years: 0,
|
28
|
+
months: 0,
|
29
|
+
days: 0,
|
30
|
+
hours: 0,
|
31
|
+
minutes: 0,
|
32
|
+
seconds: 0
|
33
|
+
}
|
34
|
+
end
|
35
|
+
|
36
|
+
it { is_expected.to eq expected_result }
|
37
|
+
end
|
38
|
+
|
39
|
+
describe 'seconds' do
|
40
|
+
let(:expected_result) do
|
41
|
+
{
|
42
|
+
years: 0,
|
43
|
+
months: 0,
|
44
|
+
days: 0,
|
45
|
+
hours: 0,
|
46
|
+
minutes: 0,
|
47
|
+
seconds: 1
|
48
|
+
}
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'when future' do
|
52
|
+
context 'when the same minute' do
|
53
|
+
let(:from) { Time.utc 2013, 1, 1, 0, 0, 0 }
|
54
|
+
let(:to) { Time.utc 2013, 1, 1, 0, 0, 1 }
|
55
|
+
|
56
|
+
it { is_expected.to eq expected_result }
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'when different minutes' do
|
60
|
+
let(:from) { Time.utc 2013, 1, 31, 23, 59, 59 }
|
61
|
+
let(:to) { Time.utc 2013, 2, 1, 0, 0, 0 }
|
62
|
+
|
63
|
+
it { is_expected.to eq expected_result }
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'when past' do
|
68
|
+
context 'when the same minute' do
|
69
|
+
let(:from) { Time.utc 2013, 1, 1, 0, 0, 1 }
|
70
|
+
let(:to) { Time.utc 2013, 1, 1, 0, 0, 0 }
|
71
|
+
|
72
|
+
it { is_expected.to eq expected_result }
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'when different minutes' do
|
76
|
+
let(:from) { Time.utc 2013, 2, 1, 0, 0, 0 }
|
77
|
+
let(:to) { Time.utc 2013, 1, 31, 23, 59, 59 }
|
78
|
+
|
79
|
+
it { is_expected.to eq expected_result }
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe 'minutes' do
|
85
|
+
let(:expected_result) do
|
86
|
+
{
|
87
|
+
years: 0,
|
88
|
+
months: 0,
|
89
|
+
days: 0,
|
90
|
+
hours: 0,
|
91
|
+
minutes: 2,
|
92
|
+
seconds: 1
|
93
|
+
}
|
94
|
+
end
|
95
|
+
|
96
|
+
context 'when future' do
|
97
|
+
context 'when the same hour' do
|
98
|
+
let(:from) { Time.utc 2013, 1, 1, 0, 0, 0 }
|
99
|
+
let(:to) { Time.utc 2013, 1, 1, 0, 2, 1 }
|
100
|
+
|
101
|
+
it { is_expected.to eq expected_result }
|
102
|
+
end
|
103
|
+
|
104
|
+
context 'when different hours' do
|
105
|
+
let(:from) { Time.utc 2013, 1, 31, 23, 59, 59 }
|
106
|
+
let(:to) { Time.utc 2013, 2, 1, 0, 2, 0 }
|
107
|
+
|
108
|
+
it { is_expected.to eq expected_result }
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
context 'when past' do
|
113
|
+
context 'when the same hour' do
|
114
|
+
let(:from) { Time.utc 2013, 1, 1, 0, 2, 1 }
|
115
|
+
let(:to) { Time.utc 2013, 1, 1, 0, 0, 0 }
|
116
|
+
|
117
|
+
it { is_expected.to eq expected_result }
|
118
|
+
end
|
119
|
+
|
120
|
+
context 'when different hours' do
|
121
|
+
let(:from) { Time.utc 2013, 2, 1, 0, 2, 0 }
|
122
|
+
let(:to) { Time.utc 2013, 1, 31, 23, 59, 59 }
|
123
|
+
|
124
|
+
it { is_expected.to eq expected_result }
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe 'hours' do
|
130
|
+
let(:expected_result) do
|
131
|
+
{
|
132
|
+
years: 0,
|
133
|
+
months: 0,
|
134
|
+
days: 0,
|
135
|
+
hours: 3,
|
136
|
+
minutes: 2,
|
137
|
+
seconds: 1
|
138
|
+
}
|
139
|
+
end
|
140
|
+
|
141
|
+
context 'when future' do
|
142
|
+
context 'when the same day' do
|
143
|
+
let(:from) { Time.utc 2013, 1, 1, 0, 0, 0 }
|
144
|
+
let(:to) { Time.utc 2013, 1, 1, 3, 2, 1 }
|
145
|
+
|
146
|
+
it { is_expected.to eq expected_result }
|
147
|
+
end
|
148
|
+
|
149
|
+
context 'when different days' do
|
150
|
+
let(:from) { Time.utc 2013, 1, 31, 23, 59, 59 }
|
151
|
+
let(:to) { Time.utc 2013, 2, 1, 3, 2, 0 }
|
152
|
+
|
153
|
+
it { is_expected.to eq expected_result }
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
context 'when past' do
|
158
|
+
context 'when the same day' do
|
159
|
+
let(:from) { Time.utc 2013, 1, 1, 3, 2, 1 }
|
160
|
+
let(:to) { Time.utc 2013, 1, 1, 0, 0, 0 }
|
161
|
+
|
162
|
+
it { is_expected.to eq expected_result }
|
163
|
+
end
|
164
|
+
|
165
|
+
context 'when different days' do
|
166
|
+
let(:from) { Time.utc 2013, 2, 1, 3, 2, 0 }
|
167
|
+
let(:to) { Time.utc 2013, 1, 31, 23, 59, 59 }
|
168
|
+
|
169
|
+
it { is_expected.to eq expected_result }
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
describe 'days' do
|
175
|
+
let(:expected_result) do
|
176
|
+
{
|
177
|
+
years: 0,
|
178
|
+
months: 0,
|
179
|
+
days: 4,
|
180
|
+
hours: 3,
|
181
|
+
minutes: 2,
|
182
|
+
seconds: 1
|
183
|
+
}
|
184
|
+
end
|
185
|
+
|
186
|
+
context 'when future' do
|
187
|
+
context 'when the same month' do
|
188
|
+
let(:from) { Time.utc 2013, 1, 1, 0, 0, 0 }
|
189
|
+
let(:to) { Time.utc 2013, 1, 5, 3, 2, 1 }
|
190
|
+
|
191
|
+
it { is_expected.to eq expected_result }
|
192
|
+
end
|
193
|
+
|
194
|
+
context 'when different months' do
|
195
|
+
let(:from) { Time.utc 2013, 1, 31, 23, 59, 59 }
|
196
|
+
let(:to) { Time.utc 2013, 2, 5, 3, 2, 0 }
|
197
|
+
|
198
|
+
it { is_expected.to eq expected_result }
|
199
|
+
end
|
200
|
+
|
201
|
+
context 'when `to` month has a fewer days than `from`' do
|
202
|
+
let(:from) { Time.utc 2013, 1, 31, 3, 2, 0 }
|
203
|
+
let(:to) { Time.utc 2013, 2, 28, 23, 59, 59 }
|
204
|
+
|
205
|
+
let(:expected_result) do
|
206
|
+
{
|
207
|
+
years: 0,
|
208
|
+
months: 0,
|
209
|
+
days: 28,
|
210
|
+
hours: 20,
|
211
|
+
minutes: 57,
|
212
|
+
seconds: 59
|
213
|
+
}
|
214
|
+
end
|
215
|
+
|
216
|
+
it { is_expected.to eq expected_result }
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
context 'when past' do
|
221
|
+
context 'when the same month' do
|
222
|
+
let(:from) { Time.utc 2013, 1, 5, 3, 2, 1 }
|
223
|
+
let(:to) { Time.utc 2013, 1, 1, 0, 0, 0 }
|
224
|
+
|
225
|
+
it { is_expected.to eq expected_result }
|
226
|
+
end
|
227
|
+
|
228
|
+
context 'when different months' do
|
229
|
+
let(:from) { Time.utc 2013, 2, 5, 3, 2, 0 }
|
230
|
+
let(:to) { Time.utc 2013, 1, 31, 23, 59, 59 }
|
231
|
+
|
232
|
+
it { is_expected.to eq expected_result }
|
233
|
+
end
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
describe 'months' do
|
238
|
+
let(:expected_result) do
|
239
|
+
{
|
240
|
+
years: 0,
|
241
|
+
months: 5,
|
242
|
+
days: 4,
|
243
|
+
hours: 3,
|
244
|
+
minutes: 2,
|
245
|
+
seconds: 1
|
246
|
+
}
|
247
|
+
end
|
248
|
+
|
249
|
+
context 'when future' do
|
250
|
+
context 'when the same year' do
|
251
|
+
let(:from) { Time.utc 2013, 1, 1, 0, 0, 0 }
|
252
|
+
let(:to) { Time.utc 2013, 6, 5, 3, 2, 1 }
|
253
|
+
|
254
|
+
it { is_expected.to eq expected_result }
|
255
|
+
end
|
256
|
+
|
257
|
+
context 'when different years' do
|
258
|
+
let(:from) { Time.utc 2012, 8, 27, 20, 57, 58 }
|
259
|
+
let(:to) { Time.utc 2013, 1, 31, 23, 59, 59 }
|
260
|
+
|
261
|
+
it { is_expected.to eq expected_result }
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
context 'when past' do
|
266
|
+
context 'when the same year' do
|
267
|
+
let(:from) { Time.utc 2013, 6, 5, 3, 2, 1 }
|
268
|
+
let(:to) { Time.utc 2013, 1, 1, 0, 0, 0 }
|
269
|
+
|
270
|
+
it { is_expected.to eq expected_result }
|
271
|
+
end
|
272
|
+
|
273
|
+
context 'when different years' do
|
274
|
+
let(:from) { Time.utc 2013, 1, 31, 23, 59, 59 }
|
275
|
+
let(:to) { Time.utc 2012, 8, 27, 20, 57, 58 }
|
276
|
+
|
277
|
+
it { is_expected.to eq expected_result }
|
278
|
+
end
|
279
|
+
end
|
280
|
+
end
|
281
|
+
|
282
|
+
describe 'years' do
|
283
|
+
let(:expected_result) do
|
284
|
+
{
|
285
|
+
years: 6,
|
286
|
+
months: 5,
|
287
|
+
days: 4,
|
288
|
+
hours: 3,
|
289
|
+
minutes: 2,
|
290
|
+
seconds: 1
|
291
|
+
}
|
292
|
+
end
|
293
|
+
|
294
|
+
context 'when future' do
|
295
|
+
let(:from) { Time.utc 2013, 1, 1, 0, 0, 0 }
|
296
|
+
let(:to) { Time.utc 2019, 6, 5, 3, 2, 1 }
|
297
|
+
|
298
|
+
it { is_expected.to eq expected_result }
|
299
|
+
end
|
300
|
+
|
301
|
+
context 'when past' do
|
302
|
+
let(:from) { Time.utc 2019, 6, 5, 3, 2, 1 }
|
303
|
+
let(:to) { Time.utc 2013, 1, 1, 0, 0, 0 }
|
304
|
+
|
305
|
+
it { is_expected.to eq expected_result }
|
306
|
+
end
|
307
|
+
|
308
|
+
context 'when different time zones' do
|
309
|
+
let(:from) { Time.new 2013, 1, 1, 3, 0, 0, '+03:00' }
|
310
|
+
let(:to) { Time.new 2019, 6, 5, 3, 2, 1, '+00:00' }
|
311
|
+
|
312
|
+
it { is_expected.to eq expected_result }
|
313
|
+
end
|
314
|
+
|
315
|
+
context 'with DateTime class' do
|
316
|
+
context 'when future' do
|
317
|
+
let(:from) { DateTime.new 2013, 1, 1, 0, 0, 0 }
|
318
|
+
let(:to) { DateTime.new 2019, 6, 5, 3, 2, 1 }
|
319
|
+
|
320
|
+
it { is_expected.to eq expected_result }
|
321
|
+
end
|
322
|
+
|
323
|
+
context 'when past' do
|
324
|
+
let(:from) { DateTime.new 2019, 6, 5, 3, 2, 1 }
|
325
|
+
let(:to) { DateTime.new 2013, 1, 1, 0, 0, 0 }
|
326
|
+
|
327
|
+
it { is_expected.to eq expected_result }
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|
331
|
+
context 'with Date class' do
|
332
|
+
let(:expected_result) do
|
333
|
+
{
|
334
|
+
years: 6,
|
335
|
+
months: 5,
|
336
|
+
days: 4,
|
337
|
+
hours: 0,
|
338
|
+
minutes: 0,
|
339
|
+
seconds: 0
|
340
|
+
}
|
341
|
+
end
|
342
|
+
|
343
|
+
context 'when future' do
|
344
|
+
let(:from) { Date.new 2013, 1, 1 }
|
345
|
+
let(:to) { Date.new 2019, 6, 5 }
|
346
|
+
|
347
|
+
it { is_expected.to eq expected_result }
|
348
|
+
end
|
349
|
+
|
350
|
+
context 'when past' do
|
351
|
+
let(:from) { Date.new 2019, 6, 5 }
|
352
|
+
let(:to) { Date.new 2013, 1, 1 }
|
353
|
+
|
354
|
+
it { is_expected.to eq expected_result }
|
355
|
+
end
|
356
|
+
end
|
357
|
+
end
|
358
|
+
|
359
|
+
context 'leap year' do
|
360
|
+
let(:expected_result) do
|
361
|
+
{
|
362
|
+
years: 0,
|
363
|
+
months: 0,
|
364
|
+
days: 2,
|
365
|
+
hours: 0,
|
366
|
+
minutes: 0,
|
367
|
+
seconds: 0
|
368
|
+
}
|
369
|
+
end
|
370
|
+
|
371
|
+
let(:from) { Time.utc 2008, 2, 28 }
|
372
|
+
let(:to) { Time.utc 2008, 3, 1 }
|
373
|
+
|
374
|
+
it { is_expected.to eq expected_result }
|
375
|
+
end
|
10
376
|
end
|
11
377
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
378
|
+
describe '#same?' do
|
379
|
+
subject { Moments::Difference.new(from, to).same? }
|
380
|
+
|
381
|
+
context 'with the same dates' do
|
382
|
+
let(:to) { from }
|
383
|
+
|
384
|
+
it { is_expected.to eq true }
|
385
|
+
end
|
386
|
+
|
387
|
+
context 'when `from` is earlier than `to`' do
|
388
|
+
let(:from) { Time.utc 2020, 1, 1 }
|
389
|
+
let(:to) { Time.utc 2020, 1, 2 }
|
16
390
|
|
17
|
-
|
18
|
-
|
19
|
-
|
391
|
+
it { is_expected.to eq false }
|
392
|
+
end
|
393
|
+
|
394
|
+
context 'when `to` is earlier than `from`' do
|
395
|
+
let(:from) { Time.utc 2020, 1, 2 }
|
396
|
+
let(:to) { Time.utc 2020, 1, 1 }
|
397
|
+
|
398
|
+
it { is_expected.to eq false }
|
399
|
+
end
|
20
400
|
end
|
21
401
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
402
|
+
describe '#future?' do
|
403
|
+
subject { Moments::Difference.new(from, to).future? }
|
404
|
+
|
405
|
+
context 'with the same dates' do
|
406
|
+
let(:to) { from }
|
407
|
+
|
408
|
+
it { is_expected.to eq false }
|
409
|
+
end
|
410
|
+
|
411
|
+
context 'when `from` is earlier than `to`' do
|
412
|
+
let(:from) { Time.utc 2020, 1, 1 }
|
413
|
+
let(:to) { Time.utc 2020, 1, 2 }
|
414
|
+
|
415
|
+
it { is_expected.to eq true }
|
416
|
+
end
|
417
|
+
|
418
|
+
context 'when `to` is earlier than `from`' do
|
419
|
+
let(:from) { Time.utc 2020, 1, 2 }
|
420
|
+
let(:to) { Time.utc 2020, 1, 1 }
|
421
|
+
|
422
|
+
it { is_expected.to eq false }
|
423
|
+
end
|
26
424
|
end
|
27
425
|
|
28
|
-
|
29
|
-
|
30
|
-
it '2013-01-01, 2013-01-01' do
|
31
|
-
from = Time.new 2013, 1, 1
|
32
|
-
to = from
|
426
|
+
describe '#past?' do
|
427
|
+
subject { Moments::Difference.new(from, to).past? }
|
33
428
|
|
34
|
-
|
429
|
+
context 'with the same dates' do
|
430
|
+
let(:to) { from }
|
35
431
|
|
36
|
-
|
37
|
-
|
432
|
+
it { is_expected.to eq false }
|
433
|
+
end
|
434
|
+
|
435
|
+
context 'when `from` is earlier than `to`' do
|
436
|
+
let(:from) { Time.utc 2020, 1, 1 }
|
437
|
+
let(:to) { Time.utc 2020, 1, 2 }
|
438
|
+
|
439
|
+
it { is_expected.to eq false }
|
440
|
+
end
|
441
|
+
|
442
|
+
context 'when `to` is earlier than `from`' do
|
443
|
+
let(:from) { Time.utc 2020, 1, 2 }
|
444
|
+
let(:to) { Time.utc 2020, 1, 1 }
|
445
|
+
|
446
|
+
it { is_expected.to eq true }
|
447
|
+
end
|
448
|
+
end
|
449
|
+
|
450
|
+
shared_examples 'in a component' do |when_seconds:, when_minutes:, when_hours:, when_days:, when_months:, when_years:|
|
451
|
+
context 'with equal dates' do
|
452
|
+
let(:to) { from }
|
453
|
+
|
454
|
+
it { is_expected.to eq 0 }
|
38
455
|
end
|
39
456
|
|
40
|
-
context 'seconds' do
|
41
|
-
|
42
|
-
from = Time.new 2013, 1, 1, 0, 0, 0
|
43
|
-
to = Time.new 2013, 1, 1, 0, 0, 1
|
457
|
+
context 'when seconds' do
|
458
|
+
let(:expected_result) { when_seconds }
|
44
459
|
|
45
|
-
|
460
|
+
context 'when future' do
|
461
|
+
context 'when the same minute' do
|
462
|
+
let(:from) { Time.utc 2013, 1, 1, 0, 0, 0 }
|
463
|
+
let(:to) { Time.utc 2013, 1, 1, 0, 0, 15 }
|
46
464
|
|
47
|
-
|
465
|
+
it { is_expected.to eq expected_result }
|
466
|
+
end
|
467
|
+
|
468
|
+
context 'when different minutes' do
|
469
|
+
let(:from) { Time.utc 2013, 1, 31, 23, 59, 59 }
|
470
|
+
let(:to) { Time.utc 2013, 2, 1, 0, 0, 14 }
|
471
|
+
|
472
|
+
it { is_expected.to eq expected_result }
|
473
|
+
end
|
48
474
|
end
|
49
475
|
|
50
|
-
|
51
|
-
|
52
|
-
|
476
|
+
context 'when past' do
|
477
|
+
context 'when the same minute' do
|
478
|
+
let(:from) { Time.utc 2013, 1, 1, 0, 0, 15 }
|
479
|
+
let(:to) { Time.utc 2013, 1, 1, 0, 0, 0 }
|
480
|
+
|
481
|
+
it { is_expected.to eq expected_result }
|
482
|
+
end
|
53
483
|
|
54
|
-
|
484
|
+
context 'when different minutes' do
|
485
|
+
let(:from) { Time.utc 2013, 2, 1, 0, 0, 14 }
|
486
|
+
let(:to) { Time.utc 2013, 1, 31, 23, 59, 59 }
|
55
487
|
|
56
|
-
|
488
|
+
it { is_expected.to eq expected_result }
|
489
|
+
end
|
57
490
|
end
|
58
491
|
end
|
59
492
|
|
60
|
-
context 'minutes' do
|
61
|
-
|
62
|
-
|
63
|
-
|
493
|
+
context 'when minutes' do
|
494
|
+
let(:expected_result) { when_minutes }
|
495
|
+
|
496
|
+
context 'when future' do
|
497
|
+
context 'when the same hour' do
|
498
|
+
let(:from) { Time.utc 2013, 1, 1, 0, 0, 0 }
|
499
|
+
let(:to) { Time.utc 2013, 1, 1, 0, 12, 15 }
|
500
|
+
|
501
|
+
it { is_expected.to eq expected_result }
|
502
|
+
end
|
64
503
|
|
65
|
-
|
504
|
+
context 'when different hours' do
|
505
|
+
let(:from) { Time.utc 2013, 1, 31, 23, 59, 59 }
|
506
|
+
let(:to) { Time.utc 2013, 2, 1, 0, 12, 14 }
|
66
507
|
|
67
|
-
|
508
|
+
it { is_expected.to eq expected_result }
|
509
|
+
end
|
68
510
|
end
|
69
511
|
|
70
|
-
|
71
|
-
|
72
|
-
|
512
|
+
context 'when past' do
|
513
|
+
context 'when the same hour' do
|
514
|
+
let(:from) { Time.utc 2013, 1, 1, 0, 12, 15 }
|
515
|
+
let(:to) { Time.utc 2013, 1, 1, 0, 0, 0 }
|
73
516
|
|
74
|
-
|
517
|
+
it { is_expected.to eq expected_result }
|
518
|
+
end
|
75
519
|
|
76
|
-
|
520
|
+
context 'when different hours' do
|
521
|
+
let(:from) { Time.utc 2013, 2, 1, 0, 12, 14 }
|
522
|
+
let(:to) { Time.utc 2013, 1, 31, 23, 59, 59 }
|
523
|
+
|
524
|
+
it { is_expected.to eq expected_result }
|
525
|
+
end
|
77
526
|
end
|
78
527
|
end
|
79
528
|
|
80
|
-
context 'hours' do
|
81
|
-
|
82
|
-
|
83
|
-
|
529
|
+
context 'when hours' do
|
530
|
+
let(:expected_result) { when_hours }
|
531
|
+
|
532
|
+
context 'when future' do
|
533
|
+
context 'when the same day' do
|
534
|
+
let(:from) { Time.utc 2013, 1, 1, 0, 0, 0 }
|
535
|
+
let(:to) { Time.utc 2013, 1, 1, 8, 12, 15 }
|
536
|
+
|
537
|
+
it { is_expected.to eq expected_result }
|
538
|
+
end
|
84
539
|
|
85
|
-
|
540
|
+
context 'when different days' do
|
541
|
+
let(:from) { Time.utc 2013, 1, 31, 23, 59, 59 }
|
542
|
+
let(:to) { Time.utc 2013, 2, 1, 8, 12, 14 }
|
86
543
|
|
87
|
-
|
544
|
+
it { is_expected.to eq expected_result }
|
545
|
+
end
|
88
546
|
end
|
89
547
|
|
90
|
-
|
91
|
-
|
92
|
-
|
548
|
+
context 'when past' do
|
549
|
+
context 'when the same day' do
|
550
|
+
let(:from) { Time.utc 2013, 1, 1, 8, 12, 15 }
|
551
|
+
let(:to) { Time.utc 2013, 1, 1, 0, 0, 0 }
|
93
552
|
|
94
|
-
|
553
|
+
it { is_expected.to eq expected_result }
|
554
|
+
end
|
95
555
|
|
96
|
-
|
556
|
+
context 'when different days' do
|
557
|
+
let(:from) { Time.utc 2013, 2, 1, 8, 12, 14 }
|
558
|
+
let(:to) { Time.utc 2013, 1, 31, 23, 59, 59 }
|
559
|
+
|
560
|
+
it { is_expected.to eq expected_result }
|
561
|
+
end
|
97
562
|
end
|
98
563
|
end
|
99
564
|
|
100
|
-
context 'days' do
|
101
|
-
|
102
|
-
|
103
|
-
|
565
|
+
context 'when days' do
|
566
|
+
let(:expected_result) { when_days }
|
567
|
+
|
568
|
+
context 'when future' do
|
569
|
+
context 'when the same month' do
|
570
|
+
let(:from) { Time.utc 2013, 1, 1, 0, 0, 0 }
|
571
|
+
let(:to) { Time.utc 2013, 1, 7, 8, 12, 15 }
|
104
572
|
|
105
|
-
|
573
|
+
it { is_expected.to eq expected_result }
|
574
|
+
end
|
106
575
|
|
107
|
-
|
576
|
+
context 'when different months' do
|
577
|
+
let(:from) { Time.utc 2013, 1, 31, 23, 59, 59 }
|
578
|
+
let(:to) { Time.utc 2013, 2, 7, 8, 12, 14 }
|
579
|
+
|
580
|
+
it { is_expected.to eq expected_result }
|
581
|
+
end
|
108
582
|
end
|
109
583
|
|
110
|
-
|
111
|
-
|
112
|
-
|
584
|
+
context 'when past' do
|
585
|
+
context 'when the same month' do
|
586
|
+
let(:from) { Time.utc 2013, 1, 7, 8, 12, 15 }
|
587
|
+
let(:to) { Time.utc 2013, 1, 1, 0, 0, 0 }
|
588
|
+
|
589
|
+
it { is_expected.to eq expected_result }
|
590
|
+
end
|
113
591
|
|
114
|
-
|
592
|
+
context 'when different months' do
|
593
|
+
let(:from) { Time.utc 2013, 2, 7, 8, 12, 14 }
|
594
|
+
let(:to) { Time.utc 2013, 1, 31, 23, 59, 59 }
|
115
595
|
|
116
|
-
|
596
|
+
it { is_expected.to eq expected_result }
|
597
|
+
end
|
117
598
|
end
|
118
599
|
end
|
119
600
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
601
|
+
describe 'when months' do
|
602
|
+
let(:expected_result) { when_months }
|
603
|
+
|
604
|
+
context 'when future' do
|
605
|
+
context 'when the same year' do
|
606
|
+
let(:from) { Time.utc 2013, 1, 1, 0, 0, 0 }
|
607
|
+
let(:to) { Time.utc 2013, 5, 7, 8, 12, 15 }
|
124
608
|
|
125
|
-
|
609
|
+
it { is_expected.to eq expected_result }
|
610
|
+
end
|
126
611
|
|
127
|
-
|
612
|
+
context 'when different years' do
|
613
|
+
let(:from) { Time.utc 2013, 1, 31, 23, 59, 59 }
|
614
|
+
let(:to) { Time.utc 2013, 6, 7, 8, 12, 14 }
|
615
|
+
|
616
|
+
it { is_expected.to eq expected_result }
|
617
|
+
end
|
128
618
|
end
|
129
619
|
|
130
|
-
|
131
|
-
|
132
|
-
|
620
|
+
context 'when past' do
|
621
|
+
context 'when the same year' do
|
622
|
+
let(:from) { Time.utc 2013, 5, 7, 8, 12, 15 }
|
623
|
+
let(:to) { Time.utc 2013, 1, 1, 0, 0, 0 }
|
624
|
+
|
625
|
+
it { is_expected.to eq expected_result }
|
626
|
+
end
|
133
627
|
|
134
|
-
|
628
|
+
context 'when different years' do
|
629
|
+
let(:from) { Time.utc 2013, 6, 7, 8, 12, 14 }
|
630
|
+
let(:to) { Time.utc 2013, 1, 31, 23, 59, 59 }
|
135
631
|
|
136
|
-
|
632
|
+
it { is_expected.to eq expected_result }
|
633
|
+
end
|
137
634
|
end
|
138
635
|
end
|
139
636
|
|
140
|
-
context 'years' do
|
141
|
-
|
142
|
-
from = Time.new 2013, 1, 1
|
143
|
-
to = Time.new 2014, 2, 2
|
637
|
+
context 'when years' do
|
638
|
+
let(:expected_result) { when_years }
|
144
639
|
|
145
|
-
|
640
|
+
context 'when future' do
|
641
|
+
let(:from) { Time.utc 2013, 1, 1, 0, 0, 0 }
|
642
|
+
let(:to) { Time.utc 2015, 5, 7, 8, 12, 15 }
|
146
643
|
|
147
|
-
|
148
|
-
|
644
|
+
it { is_expected.to eq expected_result }
|
645
|
+
|
646
|
+
context 'with DateTime class' do
|
647
|
+
let(:from) { DateTime.new 2013, 1, 1, 0, 0, 0 }
|
648
|
+
let(:to) { DateTime.new 2015, 5, 7, 8, 12, 15 }
|
149
649
|
|
150
|
-
|
151
|
-
|
152
|
-
|
650
|
+
it { is_expected.to eq expected_result }
|
651
|
+
end
|
652
|
+
end
|
153
653
|
|
154
|
-
|
654
|
+
context 'when past' do
|
655
|
+
let(:from) { Time.utc 2015, 5, 7, 8, 12, 15 }
|
656
|
+
let(:to) { Time.utc 2013, 1, 1, 0, 0, 0 }
|
155
657
|
|
156
|
-
|
658
|
+
it { is_expected.to eq expected_result }
|
157
659
|
end
|
158
660
|
end
|
661
|
+
end
|
159
662
|
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
663
|
+
context '#in_seconds' do
|
664
|
+
subject { Moments::Difference.new(from, to).in_seconds }
|
665
|
+
|
666
|
+
include_examples 'in a component',
|
667
|
+
when_seconds: 15,
|
668
|
+
when_minutes: (12 * 60) + 15,
|
669
|
+
when_hours: (8 * 60 * 60) + (12 * 60) + 15,
|
670
|
+
when_days: (6 * 24 * 60 * 60) + (8 * 60 * 60) + (12 * 60) + 15,
|
671
|
+
when_months:
|
672
|
+
((31 + 28 + 31 + 30) * 24 * 60 * 60) +
|
673
|
+
(6 * 24 * 60 * 60) +
|
674
|
+
(8 * 60 * 60) +
|
675
|
+
(12 * 60) +
|
676
|
+
15,
|
677
|
+
when_years:
|
678
|
+
(2 * 365 * 24 * 60 * 60) +
|
679
|
+
((31 + 28 + 31 + 30) * 24 * 60 * 60) +
|
680
|
+
(6 * 24 * 60 * 60) +
|
681
|
+
(8 * 60 * 60) +
|
682
|
+
(12 * 60) +
|
683
|
+
15
|
684
|
+
|
685
|
+
context 'with miliseconds' do
|
686
|
+
context 'when `to` is a bit greater' do
|
687
|
+
let(:from) { Time.utc(2020, 7, 11, 20, 26, 12) }
|
688
|
+
let(:to) { Time.utc(2020, 7, 11, 20, 28, 39.149092) }
|
689
|
+
|
690
|
+
it { is_expected.to eq 147 }
|
691
|
+
end
|
164
692
|
|
165
|
-
|
693
|
+
context 'when `from` is a bit greater' do
|
694
|
+
let(:from) { Time.utc(2020, 7, 11, 20, 26, 12.149092) }
|
695
|
+
let(:to) { Time.utc(2020, 7, 11, 20, 28, 39) }
|
166
696
|
|
167
|
-
|
697
|
+
it { is_expected.to eq 147 }
|
168
698
|
end
|
169
699
|
|
170
|
-
|
171
|
-
from
|
172
|
-
to
|
700
|
+
context 'when `to` is a lot greater' do
|
701
|
+
let(:from) { Time.utc(2020, 7, 11, 20, 26, 12) }
|
702
|
+
let(:to) { Time.utc(2020, 7, 11, 20, 28, 39.896152) }
|
703
|
+
|
704
|
+
it { is_expected.to eq 147 }
|
705
|
+
end
|
173
706
|
|
174
|
-
|
707
|
+
context 'when `from` is a lot greater' do
|
708
|
+
let(:from) { Time.utc(2020, 7, 11, 20, 26, 12.896152) }
|
709
|
+
let(:to) { Time.utc(2020, 7, 11, 20, 28, 39) }
|
175
710
|
|
176
|
-
|
711
|
+
it { is_expected.to eq 147 }
|
177
712
|
end
|
178
713
|
end
|
179
714
|
end
|
180
715
|
|
181
|
-
|
182
|
-
|
716
|
+
context '#in_minutes' do
|
717
|
+
subject { Moments::Difference.new(from, to).in_minutes }
|
718
|
+
|
719
|
+
include_examples 'in a component',
|
720
|
+
when_seconds: 0,
|
721
|
+
when_minutes: 12,
|
722
|
+
when_hours: (8 * 60) + 12,
|
723
|
+
when_days: (6 * 24 * 60) + (8 * 60) + 12,
|
724
|
+
when_months:
|
725
|
+
((31 + 28 + 31 + 30) * 24 * 60) +
|
726
|
+
(6 * 24 * 60) +
|
727
|
+
(8 * 60) +
|
728
|
+
12,
|
729
|
+
when_years:
|
730
|
+
(2 * 365 * 24 * 60) +
|
731
|
+
((31 + 28 + 31 + 30) * 24 * 60) +
|
732
|
+
(6 * 24 * 60) +
|
733
|
+
(8 * 60) +
|
734
|
+
12
|
735
|
+
end
|
736
|
+
|
737
|
+
context '#in_hours' do
|
738
|
+
subject { Moments::Difference.new(from, to).in_hours }
|
739
|
+
|
740
|
+
include_examples 'in a component',
|
741
|
+
when_seconds: 0,
|
742
|
+
when_minutes: 0,
|
743
|
+
when_hours: 8,
|
744
|
+
when_days: (6 * 24) + 8,
|
745
|
+
when_months:
|
746
|
+
((31 + 28 + 31 + 30) * 24) +
|
747
|
+
(6 * 24) +
|
748
|
+
8,
|
749
|
+
when_years:
|
750
|
+
(2 * 365 * 24) +
|
751
|
+
((31 + 28 + 31 + 30) * 24) +
|
752
|
+
(6 * 24) +
|
753
|
+
8
|
754
|
+
end
|
183
755
|
|
184
|
-
|
185
|
-
|
756
|
+
context '#in_days' do
|
757
|
+
subject { Moments::Difference.new(from, to).in_days }
|
758
|
+
|
759
|
+
include_examples 'in a component',
|
760
|
+
when_seconds: 0,
|
761
|
+
when_minutes: 0,
|
762
|
+
when_hours: 0,
|
763
|
+
when_days: 6,
|
764
|
+
when_months:
|
765
|
+
(31 + 28 + 31 + 30) +
|
766
|
+
6,
|
767
|
+
when_years:
|
768
|
+
(2 * 365) +
|
769
|
+
(31 + 28 + 31 + 30) +
|
770
|
+
6
|
186
771
|
end
|
187
772
|
|
188
|
-
|
189
|
-
|
773
|
+
context '#in_months' do
|
774
|
+
subject { Moments::Difference.new(from, to).in_months }
|
775
|
+
|
776
|
+
include_examples 'in a component',
|
777
|
+
when_seconds: 0,
|
778
|
+
when_minutes: 0,
|
779
|
+
when_hours: 0,
|
780
|
+
when_days: 0,
|
781
|
+
when_months: 4,
|
782
|
+
when_years: (2 * 12) + 4
|
783
|
+
end
|
784
|
+
|
785
|
+
context '#in_years' do
|
786
|
+
subject { Moments::Difference.new(from, to).in_years }
|
787
|
+
|
788
|
+
include_examples 'in a component',
|
789
|
+
when_seconds: 0,
|
790
|
+
when_minutes: 0,
|
791
|
+
when_hours: 0,
|
792
|
+
when_days: 0,
|
793
|
+
when_months: 0,
|
794
|
+
when_years: 2
|
190
795
|
|
191
|
-
|
192
|
-
|
796
|
+
context 'when `to` day is greater than `from` day' do
|
797
|
+
let(:from) { Time.utc 2013, 1, 1, 0, 0, 0 }
|
798
|
+
let(:to) { Time.utc 2015, 1, 7, 8, 12, 15 }
|
799
|
+
|
800
|
+
it { is_expected.to eq 2 }
|
801
|
+
end
|
802
|
+
|
803
|
+
context 'when `to` day is less than `from` day' do
|
804
|
+
let(:from) { Time.utc 2013, 1, 7, 0, 0, 0 }
|
805
|
+
let(:to) { Time.utc 2015, 1, 1, 8, 12, 15 }
|
806
|
+
|
807
|
+
it { is_expected.to eq 1 }
|
808
|
+
end
|
193
809
|
end
|
194
810
|
end
|