|
1 | | -import * as moment from "moment"; |
2 | | -import { durationInSecondsExcludingWeekends } from "./weekend"; |
3 | | - |
4 | | -describe("Weekend exclusion", () => { |
5 | | - it("handles dates on the same weekday", () => { |
6 | | - const beginning = moment("2019-04-03T08:34:50Z"); |
7 | | - const end = moment("2019-04-03T13:34:50Z"); |
8 | | - |
9 | | - const duration = durationInSecondsExcludingWeekends(beginning, end); |
10 | | - |
11 | | - expect(duration).toEqual(18000); |
12 | | - }); |
13 | | - |
14 | | - it("handles dates without weekends", () => { |
15 | | - const beginning = moment("2019-04-03T08:34:50Z"); |
16 | | - const end = moment("2019-04-04T13:34:50Z"); |
17 | | - |
18 | | - const duration = durationInSecondsExcludingWeekends(beginning, end); |
19 | | - |
20 | | - expect(duration).toEqual(104400); |
21 | | - }); |
22 | | - |
23 | | - it("handles whole weekends", () => { |
24 | | - const beginning = moment.utc("2019-04-06T00:00:00Z"); |
25 | | - const end = moment.utc("2019-04-08T00:00:00Z"); |
26 | | - |
27 | | - const duration = durationInSecondsExcludingWeekends(beginning, end); |
28 | | - |
29 | | - expect(duration).toEqual(0); |
30 | | - }); |
31 | | - |
32 | | - it("handles partial weekends", () => { |
33 | | - const beginning = moment.utc("2019-04-06T08:00:00Z"); |
34 | | - const end = moment.utc("2019-04-07T08:00:00Z"); |
35 | | - |
36 | | - const duration = durationInSecondsExcludingWeekends(beginning, end); |
37 | | - |
38 | | - expect(duration).toEqual(0); |
39 | | - }); |
40 | | - |
41 | | - it("does not exclude weekends from the calculation if the provided range does not contain any weekend days", () => { |
42 | | - const beginning = moment("2019-04-03T08:34:50Z"); |
43 | | - const end = moment("2019-04-05T13:34:51Z"); |
44 | | - |
45 | | - const duration = durationInSecondsExcludingWeekends(beginning, end); |
46 | | - |
47 | | - expect(duration).toEqual(190801); |
48 | | - }); |
49 | | - |
50 | | - it("excludes weekends if the interval contains one or more whole weekends", () => { |
51 | | - const beginning = moment("2019-04-03T08:34:50Z"); |
52 | | - const end = moment("2019-04-18T13:34:51Z"); |
53 | | - |
54 | | - const duration = durationInSecondsExcludingWeekends(beginning, end); |
55 | | - |
56 | | - // Without exclusion: 1314001 seconds |
57 | | - // Excluded: two weekends, 96 hours, 172800 seconds |
58 | | - expect(duration).toEqual(1314001 - 2 * 48 * 60 * 60); |
59 | | - }); |
60 | | - |
61 | | - it("excludes weekends if the interval contains one or more whole weekends and a partial weekend", () => { |
62 | | - const beginning = moment("2019-04-03T08:34:50Z"); |
63 | | - const end = moment("2019-04-21T13:34:51Z"); |
64 | | - |
65 | | - const duration = durationInSecondsExcludingWeekends(beginning, end); |
66 | | - |
67 | | - expect(duration).toEqual( |
68 | | - 1573201 - 2 * 48 * 60 * 60 - 24 * 60 * 60 - (13 * 60 * 60 + 34 * 60 + 51) |
69 | | - ); |
70 | | - }); |
71 | | - |
72 | | - it("accounts for business hours (09:00 - 17:00) only", () => { |
73 | | - const usesCases = [ |
74 | | - { |
75 | | - // Same day |
76 | | - start: moment.utc("2019-04-01T01:00:00Z"), |
77 | | - end: moment.utc("2019-04-01T23:00:00Z"), |
78 | | - duration: 8 * 60 * 60 |
79 | | - }, |
80 | | - { |
81 | | - // Spans over multiple days |
82 | | - start: moment.utc("2019-04-01T01:00:00Z"), |
83 | | - end: moment.utc("2019-04-02T23:00:00Z"), |
84 | | - duration: 16 * 60 * 60 |
85 | | - }, |
86 | | - { |
87 | | - // Spans over multiple weeks |
88 | | - start: moment.utc("2019-04-01T01:00:00Z"), |
89 | | - end: moment.utc("2019-04-30T23:00:00Z"), |
90 | | - duration: 22 * 8 * 60 * 60 |
91 | | - } |
92 | | - ]; |
93 | | - |
94 | | - usesCases.forEach(usesCase => { |
95 | | - expect( |
96 | | - durationInSecondsExcludingWeekends(usesCase.start, usesCase.end, true) |
97 | | - ).toEqual(usesCase.duration); |
98 | | - }); |
| 1 | +import * as moment from 'moment'; |
| 2 | +import { durationInSecondsExcludingWeekends } from './weekend'; |
| 3 | + |
| 4 | +describe('Weekend exclusion', () => { |
| 5 | + it('handles dates on the same weekday', () => { |
| 6 | + const beginning = moment('2019-04-03T08:34:50Z'); |
| 7 | + const end = moment('2019-04-03T13:34:50Z'); |
| 8 | + |
| 9 | + const duration = durationInSecondsExcludingWeekends(beginning, end); |
| 10 | + |
| 11 | + expect(duration).toEqual(18000); |
| 12 | + }); |
| 13 | + |
| 14 | + it('handles dates without weekends', () => { |
| 15 | + const beginning = moment('2019-04-03T08:34:50Z'); |
| 16 | + const end = moment('2019-04-04T13:34:50Z'); |
| 17 | + |
| 18 | + const duration = durationInSecondsExcludingWeekends(beginning, end); |
| 19 | + |
| 20 | + expect(duration).toEqual(104400); |
| 21 | + }); |
| 22 | + |
| 23 | + it('handles whole weekends', () => { |
| 24 | + const beginning = moment.utc('2019-04-06T00:00:00Z'); |
| 25 | + const end = moment.utc('2019-04-08T00:00:00Z'); |
| 26 | + |
| 27 | + const duration = durationInSecondsExcludingWeekends(beginning, end); |
| 28 | + |
| 29 | + expect(duration).toEqual(0); |
| 30 | + }); |
| 31 | + |
| 32 | + it('handles partial weekends', () => { |
| 33 | + const beginning = moment.utc('2019-04-06T08:00:00Z'); |
| 34 | + const end = moment.utc('2019-04-07T08:00:00Z'); |
| 35 | + |
| 36 | + const duration = durationInSecondsExcludingWeekends(beginning, end); |
| 37 | + |
| 38 | + expect(duration).toEqual(0); |
| 39 | + }); |
| 40 | + |
| 41 | + it('does not exclude weekends from the calculation if the provided range does not contain any weekend days', () => { |
| 42 | + const beginning = moment('2019-04-03T08:34:50Z'); |
| 43 | + const end = moment('2019-04-05T13:34:51Z'); |
| 44 | + |
| 45 | + const duration = durationInSecondsExcludingWeekends(beginning, end); |
| 46 | + |
| 47 | + expect(duration).toEqual(190801); |
| 48 | + }); |
| 49 | + |
| 50 | + it('excludes weekends if the interval contains one or more whole weekends', () => { |
| 51 | + const beginning = moment('2019-04-03T08:34:50Z'); |
| 52 | + const end = moment('2019-04-18T13:34:51Z'); |
| 53 | + |
| 54 | + const duration = durationInSecondsExcludingWeekends(beginning, end); |
| 55 | + |
| 56 | + // Without exclusion: 1314001 seconds |
| 57 | + // Excluded: two weekends, 96 hours, 172800 seconds |
| 58 | + expect(duration).toEqual(1314001 - 2 * 48 * 60 * 60); |
| 59 | + }); |
| 60 | + |
| 61 | + it('excludes weekends if the interval contains one or more whole weekends and a partial weekend', () => { |
| 62 | + const beginning = moment('2019-04-03T08:34:50Z'); |
| 63 | + const end = moment('2019-04-21T13:34:51Z'); |
| 64 | + |
| 65 | + const duration = durationInSecondsExcludingWeekends(beginning, end); |
| 66 | + |
| 67 | + expect(duration).toEqual( |
| 68 | + 1573201 - 2 * 48 * 60 * 60 - 24 * 60 * 60 - (13 * 60 * 60 + 34 * 60 + 51) |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + it('accounts for business hours (09:00 - 17:00) only', () => { |
| 73 | + const usesCases = [ |
| 74 | + { |
| 75 | + // Same day |
| 76 | + start: moment.utc('2019-04-01T01:00:00Z'), |
| 77 | + end: moment.utc('2019-04-01T23:00:00Z'), |
| 78 | + duration: 8 * 60 * 60, |
| 79 | + }, |
| 80 | + { |
| 81 | + // Spans over multiple days |
| 82 | + start: moment.utc('2019-04-01T01:00:00Z'), |
| 83 | + end: moment.utc('2019-04-02T23:00:00Z'), |
| 84 | + duration: 16 * 60 * 60, |
| 85 | + }, |
| 86 | + { |
| 87 | + // Spans over multiple weeks |
| 88 | + start: moment.utc('2019-04-01T01:00:00Z'), |
| 89 | + end: moment.utc('2019-04-30T23:00:00Z'), |
| 90 | + duration: 22 * 8 * 60 * 60, |
| 91 | + }, |
| 92 | + ]; |
| 93 | + |
| 94 | + usesCases.forEach(usesCase => { |
| 95 | + expect( |
| 96 | + durationInSecondsExcludingWeekends(usesCase.start, usesCase.end, true) |
| 97 | + ).toEqual(usesCase.duration); |
99 | 98 | }); |
| 99 | + }); |
100 | 100 | }); |
0 commit comments