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