/
computation_spec.rb
596 lines (505 loc) · 22.7 KB
/
computation_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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
require 'spec_helper'
describe WorkingHours::Computation do
include WorkingHours::Computation
describe '#add_days' do
it 'can add working days to date' do
date = Date.new(1991, 11, 15) #Friday
expect(add_days(date, 2)).to eq(Date.new(1991, 11, 19)) # Tuesday
end
it 'can substract working days from date' do
date = Date.new(1991, 11, 15) #Friday
expect(add_days(date, -7)).to eq(Date.new(1991, 11, 6)) # Wednesday
end
it 'can add working days to time' do
time = Time.local(1991, 11, 15, 14, 00, 42)
expect(add_days(time, 1)).to eq(Time.local(1991, 11, 18, 14, 00, 42)) # Monday
end
it 'can add working days to ActiveSupport::TimeWithZone' do
time = Time.utc(1991, 11, 15, 14, 00, 42)
time_monday = Time.utc(1991, 11, 18, 14, 00, 42)
time_with_zone = ActiveSupport::TimeWithZone.new(time, 'Tokyo')
expect(add_days(time_with_zone, 1)).to eq(ActiveSupport::TimeWithZone.new(time_monday, 'Tokyo'))
end
it 'skips non worked days' do
time = Date.new(2014, 4, 7) # Monday
WorkingHours::Config.working_hours = {mon: {'09:00' => '17:00'}, wed: {'09:00' => '17:00'}}
expect(add_days(time, 1)).to eq(Date.new(2014, 4, 9)) # Wednesday
end
it 'skips non worked days when origin is not worked' do
time = Date.new(2014, 4, 8) # Tuesday
WorkingHours::Config.working_hours = {mon: {'09:00' => '17:00'}, wed: {'09:00' => '17:00'}, thu: {'09:00' => '17:00'}, sun: {'09:00' => '17:00'}}
expect(add_days(time, 1)).to eq(Date.new(2014, 4, 10)) # Thursday
expect(add_days(time, -1)).to eq(Date.new(2014, 4, 6)) # Sunday
end
it 'skips holidays' do
time = Date.new(2014, 4, 7) # Monday
WorkingHours::Config.holidays = [Date.new(2014, 4, 8)] # Tuesday
expect(add_days(time, 1)).to eq(Date.new(2014, 4, 9)) # Wednesday
end
it 'skips holidays when origin is holiday' do
time = Date.new(2014, 4, 9) # Wednesday
WorkingHours::Config.holidays = [time] # Wednesday
expect(add_days(time, 1)).to eq(Date.new(2014, 4, 11)) # Friday
expect(add_days(time, -1)).to eq(Date.new(2014, 4, 7)) # Monday
end
it 'skips holidays and non worked days' do
time = Date.new(2014, 4, 7) # Monday
WorkingHours::Config.holidays = [Date.new(2014, 4, 9)] # Wednesday
WorkingHours::Config.working_hours = {mon: {'09:00' => '17:00'}, wed: {'09:00' => '17:00'}}
expect(add_days(time, 3)).to eq(Date.new(2014, 4, 21))
end
it 'returns the original value when adding 0 days' do
time = Date.new(2014, 4, 7)
WorkingHours::Config.holidays = [time]
expect(add_days(time, 0)).to eq(time)
end
it 'accepts time given from any time zone' do
time = Time.utc(1991, 11, 14, 21, 0, 0) # Thursday 21 pm UTC
WorkingHours::Config.time_zone = 'Tokyo' # But we are at tokyo, so it's already Friday 6 am
monday = Time.new(1991, 11, 18, 6, 0, 0, "+09:00") # so one working day later, we are monday (Tokyo)
expect(add_days(time, 1)).to eq(monday)
end
end
describe '#add_hours' do
it 'adds working hours' do
time = Time.utc(1991, 11, 15, 14, 00, 42) # Friday
expect(add_hours(time, 2)).to eq(Time.utc(1991, 11, 15, 16, 00, 42))
end
it 'can substract working hours' do
time = Time.utc(1991, 11, 18, 14, 00, 42) # Monday
expect(add_hours(time, -7)).to eq(Time.utc(1991, 11, 15, 15, 00, 42)) # Friday
end
it 'accepts time given from any time zone' do
time = Time.utc(1991, 11, 15, 7, 0, 0) # Friday 7 am UTC
WorkingHours::Config.time_zone = 'Tokyo' # But we are at tokyo, so it's already 4 pm
monday = Time.new(1991, 11, 18, 11, 0, 0, "+09:00") # so 3 working hours later, we are monday (Tokyo)
expect(add_hours(time, 3)).to eq(monday)
end
it 'moves correctly with multiple timespans' do
WorkingHours::Config.working_hours = {mon: {'07:00' => '12:00', '13:00' => '18:00'}}
time = Time.utc(1991, 11, 11, 5) # Monday 6 am UTC
expect(add_hours(time, 6)).to eq(Time.utc(1991, 11, 11, 14))
end
end
describe '#add_minutes' do
it 'adds working minutes' do
time = Time.utc(1991, 11, 15, 16, 30, 42) # Friday
expect(add_minutes(time, 45)).to eq(Time.utc(1991, 11, 18, 9, 15, 42))
end
end
describe '#add_seconds' do
it 'adds working seconds' do
time = Time.utc(1991, 11, 15, 16, 59, 42) # Friday
expect(add_seconds(time, 120)).to eq(Time.utc(1991, 11, 18, 9, 1, 42))
end
it 'Calls precompiled only once' do
precompiled = WorkingHours::Config.precompiled
expect(WorkingHours::Config).to receive(:precompiled).once.and_return(precompiled) # in_config_zone and add_seconds
time = Time.utc(1991, 11, 15, 16, 59, 42) # Friday
add_seconds(time, 120)
end
it 'supports midnight when advancing time' do
WorkingHours::Config.working_hours = {:mon => {'00:00' => '24:00'}}
time = Time.utc(2014, 4, 7, 23, 59, 30) # Monday
expect(add_seconds(time, 60)).to eq(Time.utc(2014, 4, 14, 0, 0, 30))
end
it 'supports midnight when rewinding time' do
WorkingHours::Config.working_hours = {:mon => {'00:00' => '24:00'}, :tue => {'08:00' => '18:00'}}
time = Time.utc(2014, 4, 8, 0, 0, 30) # Tuesday
expect(add_seconds(time, -60)).to eq(Time.utc(2014, 4, 7, 23, 59, 00))
end
it 'honors miliseconds in the base time and increment (but return rounded result)' do
# Rounding the base time or increments before the end would yield a wrong result
time = Time.utc(1991, 11, 15, 16, 59, 42.25) # +250ms
expect(add_seconds(time, 120.4)).to eq(Time.utc(1991, 11, 18, 9, 1, 43))
end
end
describe '#advance_to_working_time' do
it 'jumps non-working day' do
WorkingHours::Config.holidays = [Date.new(2014, 5, 1)]
expect(advance_to_working_time(Time.utc(2014, 5, 1, 12, 0))).to eq(Time.utc(2014, 5, 2, 9, 0))
expect(advance_to_working_time(Time.utc(2014, 6, 1, 12, 0))).to eq(Time.utc(2014, 6, 2, 9, 0))
end
it 'returns self during working hours' do
expect(advance_to_working_time(Time.utc(2014, 4, 7, 9, 0))).to eq(Time.utc(2014, 4, 7, 9, 0))
expect(advance_to_working_time(Time.utc(2014, 4, 7, 16, 59))).to eq(Time.utc(2014, 4, 7, 16, 59))
end
it 'jumps outside working hours' do
expect(advance_to_working_time(Time.utc(2014, 4, 7, 8, 59))).to eq(Time.utc(2014, 4, 7, 9, 0))
expect(advance_to_working_time(Time.utc(2014, 4, 7, 17, 0))).to eq(Time.utc(2014, 4, 8, 9, 0))
end
it 'move between timespans' do
WorkingHours::Config.working_hours = {mon: {'07:00' => '12:00', '13:00' => '18:00'}}
expect(advance_to_working_time(Time.utc(2014, 4, 7, 11, 59))).to eq(Time.utc(2014, 4, 7, 11, 59))
expect(advance_to_working_time(Time.utc(2014, 4, 7, 12, 0))).to eq(Time.utc(2014, 4, 7, 13, 0))
expect(advance_to_working_time(Time.utc(2014, 4, 7, 12, 59))).to eq(Time.utc(2014, 4, 7, 13, 0))
expect(advance_to_working_time(Time.utc(2014, 4, 7, 13, 0))).to eq(Time.utc(2014, 4, 7, 13, 0))
end
it 'works with any input timezone (converts to config)' do
# Monday 0 am (-09:00) is 9am in UTC time, working time!
expect(advance_to_working_time(Time.new(2014, 4, 7, 0, 0, 0 , "-09:00"))).to eq(Time.utc(2014, 4, 7, 9))
expect(advance_to_working_time(Time.new(2014, 4, 7, 22, 0, 0 , "+02:00"))).to eq(Time.utc(2014, 4, 8, 9))
end
it 'returns time in config zone' do
WorkingHours::Config.time_zone = 'Tokyo'
expect(advance_to_working_time(Time.new(2014, 4, 7, 0, 0, 0)).zone).to eq('JST')
end
it 'do not leak nanoseconds when advancing' do
expect(advance_to_working_time(Time.utc(2014, 4, 7, 5, 0, 0, 123456.789))).to eq(Time.utc(2014, 4, 7, 9, 0, 0, 0))
end
end
describe '#advance_to_closing_time' do
it 'jumps non-working day' do
WorkingHours::Config.holidays = [Date.new(2014, 5, 1)]
holiday = Time.utc(2014, 5, 1, 12, 0)
friday_closing = Time.utc(2014, 5, 2, 17, 0)
sunday = Time.utc(2014, 6, 1, 12, 0)
monday_closing = Time.utc(2014, 6, 2, 17, 0)
expect(advance_to_closing_time(holiday)).to eq(friday_closing)
expect(advance_to_closing_time(sunday)).to eq(monday_closing)
end
it 'moves to the closing time during working hours' do
in_open_time = Time.utc(2014, 4, 7, 12, 0)
closing_time = Time.utc(2014, 4, 7, 17, 0)
expect(advance_to_closing_time(in_open_time)).to eq(closing_time)
end
it 'jumps outside working hours' do
monday_before_opening = Time.utc(2014, 4, 7, 8, 59)
monday_closing = Time.utc(2014, 4, 7, 17, 0)
tuesday_closing = Time.utc(2014, 4, 8, 17, 0)
expect(advance_to_closing_time(monday_before_opening)).to eq(monday_closing)
expect(advance_to_closing_time(monday_closing)).to eq(tuesday_closing)
end
context 'moving between timespans' do
before do
WorkingHours::Config.working_hours = {
mon: {'07:00' => '12:00', '13:00' => '18:00'},
tue: {'09:00' => '17:00'},
wed: {'09:00' => '17:00'},
thu: {'09:00' => '17:00'},
fri: {'09:00' => '17:00'}
}
end
let(:monday_morning) { Time.utc(2014, 4, 7, 10) }
let(:morning_closing) { Time.utc(2014, 4, 7, 12) }
let(:afternoon_closing) { Time.utc(2014, 4, 7, 18) }
let(:monday_break) { Time.utc(2014, 4, 7, 12) }
let(:tuesday_closing) { Time.utc(2014, 4, 8, 17) }
it 'moves from morning to end of morning slot' do
expect(advance_to_closing_time(monday_morning)).to eq(morning_closing)
end
it 'moves from break time to end of afternoon slot' do
expect(advance_to_closing_time(monday_break)).to eq(afternoon_closing)
end
it 'moves from afternoon closing slot to next day' do
expect(advance_to_closing_time(afternoon_closing)).to eq(tuesday_closing)
end
end
context 'supporting midnight' do
before do
WorkingHours::Config.working_hours = {
mon: {'00:00' => '24:00'},
tue: {'09:00' => '17:00'}
}
end
let(:monday_morning) { Time.utc(2014, 4, 7, 0) }
let(:monday_closing) { Time.utc(2014, 4, 7) + WorkingHours::Config::MIDNIGHT }
let(:tuesday_closing) { Time.utc(2014, 4, 8, 17) }
let(:sunday) { Time.utc(2014, 4, 6, 17) }
it 'moves from morning to midnight' do
expect(advance_to_closing_time(monday_morning)).to eq(monday_closing)
end
it 'moves from midnight to end of next slot' do
expect(advance_to_closing_time(monday_closing)).to eq(tuesday_closing)
end
it 'moves over midnight' do
expect(advance_to_closing_time(sunday)).to eq(monday_closing)
end
it 'give precise computation with nothing other than miliseconds' do
pending "iso8601 is not precise enough on AS < 4" if ActiveSupport::VERSION::MAJOR <= 4
expect(advance_to_closing_time(monday_morning).iso8601(25)).to eq("2014-04-07T23:59:59.9999990000000000000000000Z")
end
end
it 'works with any input timezone (converts to config)' do
# Monday 0 am (-09:00) is 9am in UTC time, working time!
monday_morning = Time.new(2014, 4, 7, 0, 0, 0 , "-09:00")
monday_closing = Time.new(2014, 4, 7, 12, 0, 0 , "-05:00")
monday_night = Time.new(2014, 4, 7, 22, 0, 0, "+02:00")
tuesday_evening = Time.utc(2014, 4, 8, 17)
expect(advance_to_closing_time(monday_morning)).to eq(monday_closing)
expect(advance_to_closing_time(monday_night)).to eq(tuesday_evening)
end
it 'returns time in config zone' do
WorkingHours::Config.time_zone = 'Tokyo'
expect(advance_to_closing_time(Time.new(2014, 4, 7, 0, 0, 0)).zone).to eq('JST')
end
it 'do not leak nanoseconds when advancing' do
expect(advance_to_closing_time(Time.utc(2014, 4, 7, 5, 0, 0, 123456.789))).to eq(Time.utc(2014, 4, 7, 17, 0, 0, 0))
end
end
describe '#next_working_time' do
it 'jumps non-working day' do
WorkingHours::Config.holidays = [Date.new(2014, 5, 1)]
holiday = Time.utc(2014, 5, 1, 12, 0)
sunday = Time.utc(2014, 6, 1, 12, 0)
expect(next_working_time(holiday)).to eq(Time.utc(2014, 5, 2, 9, 0))
expect(next_working_time(sunday)).to eq(Time.utc(2014, 6, 2, 9, 0))
end
it 'moves to the following timespan during working hours' do
monday = Time.utc(2014, 4, 7, 12, 0)
tuesday = Time.utc(2014, 4, 8, 9, 0)
expect(next_working_time(monday)).to eq(tuesday)
end
it 'jumps outside working hours' do
monday_before_opening = Time.utc(2014, 4, 7, 8, 59)
monday_opening = Time.utc(2014, 4, 7, 9, 0)
monday_closing = Time.utc(2014, 4, 7, 17, 0)
tuesday_opening = Time.utc(2014, 4, 8, 9, 0)
expect(next_working_time(monday_before_opening)).to eq(monday_opening)
expect(next_working_time(monday_closing)).to eq(tuesday_opening)
end
context 'move between timespans' do
before do
WorkingHours::Config.working_hours = {
mon: {'07:00' => '12:00', '13:00' => '18:00'},
tue: {'09:00' => '17:00'},
wed: {'09:00' => '17:00'},
thu: {'09:00' => '17:00'},
fri: {'09:00' => '17:00'}
}
end
let(:monday_morning) { Time.utc(2014, 4, 7, 10) }
let(:monday_afternoon) { Time.utc(2014, 4, 7, 13) }
let(:monday_break) { Time.utc(2014, 4, 7, 12) }
let(:tuesday_morning) { Time.utc(2014, 4, 8, 9) }
it 'moves from morning to afternoon slot' do
expect(next_working_time(monday_morning)).to eq(monday_afternoon)
end
it 'moves from break time to afternoon slot' do
expect(next_working_time(monday_break)).to eq(monday_afternoon)
end
it 'moves from afternoon slot to next day' do
expect(next_working_time(monday_afternoon)).to eq(tuesday_morning)
end
end
it 'works with any input timezone (converts to config)' do
# Monday 0 am (-09:00) is 9am in UTC time, working time!
monday_morning = Time.new(2014, 4, 7, 0, 0, 0 , "-09:00")
monday_night = Time.new(2014, 4, 7, 22, 0, 0, "+02:00")
tuesday_morning = Time.utc(2014, 4, 8, 9)
expect(next_working_time(monday_morning)).to eq(tuesday_morning)
expect(next_working_time(monday_night)).to eq(tuesday_morning)
end
it 'returns time in config zone' do
WorkingHours::Config.time_zone = 'Tokyo'
expect(next_working_time(Time.new(2014, 4, 7, 0, 0, 0)).zone).to eq('JST')
end
end
describe '#return_to_working_time' do
it 'jumps non-working day' do
WorkingHours::Config.holidays = [Date.new(2014, 5, 1)]
expect(return_to_working_time(Time.utc(2014, 5, 1, 12, 0))).to eq(Time.utc(2014, 4, 30, 17))
expect(return_to_working_time(Time.utc(2014, 6, 1, 12, 0))).to eq(Time.utc(2014, 5, 30, 17))
end
it 'returns self during working hours' do
expect(return_to_working_time(Time.utc(2014, 4, 7, 9, 1))).to eq(Time.utc(2014, 4, 7, 9, 1))
expect(return_to_working_time(Time.utc(2014, 4, 7, 17, 0))).to eq(Time.utc(2014, 4, 7, 17, 0))
end
it 'jumps outside working hours' do
expect(return_to_working_time(Time.utc(2014, 4, 7, 17, 1))).to eq(Time.utc(2014, 4, 7, 17, 0))
expect(return_to_working_time(Time.utc(2014, 4, 8, 9, 0))).to eq(Time.utc(2014, 4, 7, 17, 0))
end
it 'move between timespans' do
WorkingHours::Config.working_hours = {mon: {'07:00' => '12:00', '13:00' => '18:00'}}
expect(return_to_working_time(Time.utc(2014, 4, 7, 13, 1))).to eq(Time.utc(2014, 4, 7, 13, 1))
expect(return_to_working_time(Time.utc(2014, 4, 7, 13, 0))).to eq(Time.utc(2014, 4, 7, 12, 0))
expect(return_to_working_time(Time.utc(2014, 4, 7, 12, 1))).to eq(Time.utc(2014, 4, 7, 12, 0))
expect(return_to_working_time(Time.utc(2014, 4, 7, 12, 0))).to eq(Time.utc(2014, 4, 7, 12, 0))
end
it 'works with any input timezone (converts to config)' do
# Monday 1 am (-09:00) is 10am in UTC time, working time!
expect(return_to_working_time(Time.new(2014, 4, 7, 1, 0, 0 , "-09:00"))).to eq(Time.utc(2014, 4, 7, 10))
expect(return_to_working_time(Time.new(2014, 4, 7, 22, 0, 0 , "+02:00"))).to eq(Time.utc(2014, 4, 7, 17))
end
it 'returns time in config zone' do
WorkingHours::Config.time_zone = 'Tokyo'
expect(return_to_working_time(Time.new(2014, 4, 7, 1, 0, 0)).zone).to eq('JST')
end
end
describe '#working_day?' do
it 'returns true on working day' do
expect(working_day?(Date.new(2014, 4, 7))).to be(true)
end
it 'skips holidays' do
WorkingHours::Config.holidays = [Date.new(2014, 5, 1)]
expect(working_day?(Date.new(2014, 5, 1))).to be(false)
end
it 'skips non working days' do
expect(working_day?(Date.new(2014, 4, 6))).to be(false)
end
end
describe '#in_working_hours?' do
it 'returns false in non-working day' do
WorkingHours::Config.holidays = [Date.new(2014, 5, 1)]
expect(in_working_hours?(Time.utc(2014, 5, 1, 12, 0))).to be(false)
expect(in_working_hours?(Time.utc(2014, 6, 1, 12, 0))).to be(false)
end
it 'returns true during working hours' do
expect(in_working_hours?(Time.utc(2014, 4, 7, 9, 0))).to be(true)
expect(in_working_hours?(Time.utc(2014, 4, 7, 16, 59))).to be(true)
end
it 'returns false outside working hours' do
expect(in_working_hours?(Time.utc(2014, 4, 7, 8, 59))).to be(false)
expect(in_working_hours?(Time.utc(2014, 4, 7, 17, 0))).to be(false)
end
it 'works with multiple timespan' do
WorkingHours::Config.working_hours = {mon: {'07:00' => '12:00', '13:00' => '18:00'}}
expect(in_working_hours?(Time.utc(2014, 4, 7, 11, 59))).to be(true)
expect(in_working_hours?(Time.utc(2014, 4, 7, 12, 0))).to be(false)
expect(in_working_hours?(Time.utc(2014, 4, 7, 12, 59))).to be(false)
expect(in_working_hours?(Time.utc(2014, 4, 7, 13, 0))).to be(true)
end
it 'works with any timezone' do
# Monday 00:00 am UTC is 09:00 am Tokyo, working time !
WorkingHours::Config.time_zone = 'Tokyo'
expect(in_working_hours?(Time.utc(2014, 4, 7, 0, 0))).to be(true)
end
end
describe '#working_days_between' do
it 'returns 0 if same date' do
expect(working_days_between(
Date.new(1991, 11, 15), # friday
Date.new(1991, 11, 15)
)).to eq(0)
end
it 'returns 0 if time in same day' do
expect(working_days_between(
Time.utc(1991, 11, 15, 8), # friday
Time.utc(1991, 11, 15, 4)
)).to eq(0)
end
it 'counts working days' do
expect(working_days_between(
Date.new(1991, 11, 15), # friday to friday
Date.new(1991, 11, 22)
)).to eq(5)
end
it 'returns negative if params are reversed' do
expect(working_days_between(
Date.new(1991, 11, 22), # friday to friday
Date.new(1991, 11, 15)
)).to eq(-5)
end
context 'consider time at end of day' do
it 'returns 0 from friday to saturday' do
expect(working_days_between(
Date.new(1991, 11, 15), # friday to saturday
Date.new(1991, 11, 16)
)).to eq(0)
end
it 'returns 1 from sunday to monday' do
expect(working_days_between(
Date.new(1991, 11, 17), # sunday to monday
Date.new(1991, 11, 18)
)).to eq(1)
end
end
end
describe '#working_time_between' do
it 'returns 0 if same time' do
expect(working_time_between(
Time.utc(2014, 4, 7, 8),
Time.utc(2014, 4, 7, 8)
)).to eq(0)
end
it 'returns 0 during non working time' do
expect(working_time_between(
Time.utc(2014, 4, 11, 20), # Friday evening
Time.utc(2014, 4, 14, 5) # Monday early
)).to eq(0)
end
it 'ignores miliseconds' do
expect(working_time_between(
Time.utc(2014, 4, 13, 9, 10, 24.01),
Time.utc(2014, 4, 14, 9, 10, 24.02),
)).to eq(624)
end
it 'returns distance in same period' do
expect(working_time_between(
Time.utc(2014, 4, 7, 10),
Time.utc(2014, 4, 7, 15)
)).to eq(5.hours)
end
it 'returns negative if params are reversed' do
expect(working_time_between(
Time.utc(2014, 4, 7, 15),
Time.utc(2014, 4, 7, 10)
)).to eq(-5.hours)
end
it 'returns full day if outside period' do
expect(working_time_between(
Time.utc(2014, 4, 7, 7),
Time.utc(2014, 4, 7, 20)
)).to eq(8.hours)
end
it 'supports midnight' do
WorkingHours::Config.working_hours = {:mon => {'00:00' => '24:00'}}
expect(working_time_between(
Time.utc(2014, 4, 6, 12),
Time.utc(2016, 4, 6, 12)
)).to eq(24.hours * 105) # 105 complete mondays in 2 years
end
it 'handles multiple timespans' do
WorkingHours::Config.working_hours = {
mon: {'07:00' => '12:00', '13:00' => '18:00'}
}
expect(working_time_between(
Time.utc(2014, 4, 7, 11, 59),
Time.utc(2014, 4, 7, 13, 1)
)).to eq(2.minutes)
expect(working_time_between(
Time.utc(2014, 4, 7, 11),
Time.utc(2014, 4, 14, 13)
)).to eq(11.hours)
end
it 'works with any timezone (converts to config)' do
expect(working_time_between(
Time.new(2014, 4, 7, 1, 0, 0, "-09:00"), # Monday 10am in UTC
Time.new(2014, 4, 7, 15, 0, 0, "-04:00"), # Monday 7pm in UTC
)).to eq(7.hours)
end
it 'uses precise computation to avoid useless loops' do
# +200 usec on each time, using floating point would cause
# precision issues and require several iterations
expect(self).to receive(:advance_to_working_time).twice.and_call_original
expect(working_time_between(
Time.utc(2014, 4, 7, 5, 0, 0, 200),
Time.utc(2014, 4, 7, 15, 0, 0, 200),
)).to eq(6.hours)
end
it 'do not cause infinite loop if the time is not advancing properly' do
# simulate some computation/precision error
expect(self).to receive(:advance_to_working_time).twice do |time|
time.change(hour: 9) - 0.0001
end
expect { working_time_between(
Time.utc(2014, 4, 7, 5, 0, 0),
Time.utc(2014, 4, 7, 15, 0, 0),
) }.to raise_error(RuntimeError, /Invalid loop detected in working_time_between \(from=2014-04-07T08:59:59.999/)
end
# generates two times with +0ms, +250ms, +500ms, +750ms and +1s
# then for each combination compare the result with a ruby diff
context 'with precise miliseconds timings' do
reference = Time.utc(2014, 4, 7, 10)
0.step(1.0, 0.25) do |offset1|
0.step(1.0, 0.25) do |offset2|
from = reference + offset1
to = reference + offset2
it "returns expected value (#{(to - from).round}) for #{offset1} — #{offset2} interval" do
expect(working_time_between(from, to)).to eq((to - from).round)
end
end
end
end
end
end