File size: 20,834 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
package rotation

import (
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

const timeFmt = "Jan 2 2006 3:04 pm"

func mustParse(t *testing.T, value string) time.Time {
	t.Helper()
	loc, err := time.LoadLocation("America/Chicago")
	if err != nil {
		t.Fatal(err)
	}
	tm, err := time.ParseInLocation(timeFmt, value, loc)
	if err != nil {
		t.Fatal(err)
	}
	tm = tm.In(loc)
	return tm
}

func TestRotation_StartEnd_BruteForce(t *testing.T) {
	loc, err := time.LoadLocation("America/Chicago")
	require.NoError(t, err)

	// check will walk through, in 30 sec. increments, from the start to the end time calling
	// both StartTime and EndTime on the rotation and asserting it gives the exact expectedHandoffs.
	check := func(rot *Rotation, start, end time.Time, expectedHandoffs ...string) {
		t.Helper()
		ts := start
		require.Equalf(t, expectedHandoffs[1], rot.EndTime(ts).String(), "first shift end %s", ts.String())
		require.Equalf(t, expectedHandoffs[0], rot.StartTime(ts).String(), "first shift start %s", ts.String())
		timesM := make(map[string]bool)
		var times []string
		for !ts.After(end) {
			res := rot.StartTime(ts).String()
			if !timesM[res] {
				timesM[res] = true
				times = append(times, res)
			}
			require.Contains(t, expectedHandoffs, res, "StartTime for %s", ts.String())

			res = rot.EndTime(ts).String()
			if !timesM[res] {
				timesM[res] = true
				times = append(times, res)
			}
			require.Contains(t, expectedHandoffs, res, "EndTime for %s", ts.String())

			ts = ts.Add(30 * time.Second)
		}
		assert.EqualValues(t, expectedHandoffs, times)
	}

	check(&Rotation{
		Type:        TypeHourly,
		ShiftLength: 24,
		Start:       time.Date(2020, time.October, 30, 1, 30, 0, 0, loc),
	},
		time.Date(2020, time.October, 30, 1, 0, 0, 0, loc),
		time.Date(2020, time.November, 3, 1, 0, 0, 0, loc),

		"2020-10-29 01:30:00 -0500 CDT", // StartTime as of 1AM on the 30th would be 1:30AM on the 29th
		"2020-10-30 01:30:00 -0500 CDT",
		"2020-10-31 01:30:00 -0500 CDT",
		"2020-11-01 01:30:00 -0500 CDT",
		"2020-11-02 01:30:00 -0600 CST",
		"2020-11-03 01:30:00 -0600 CST",
	)

	// Daily & 24 hour should be identical
	check(&Rotation{
		Type:        TypeDaily,
		ShiftLength: 1,
		Start:       time.Date(2020, time.October, 30, 1, 30, 0, 0, loc),
	},
		time.Date(2020, time.October, 30, 1, 0, 0, 0, loc),
		time.Date(2020, time.November, 3, 1, 0, 0, 0, loc),

		"2020-10-29 01:30:00 -0500 CDT",
		"2020-10-30 01:30:00 -0500 CDT",
		"2020-10-31 01:30:00 -0500 CDT",
		"2020-11-01 01:30:00 -0500 CDT",
		"2020-11-02 01:30:00 -0600 CST",
		"2020-11-03 01:30:00 -0600 CST",
	)

	check(&Rotation{
		Type:        TypeDaily,
		ShiftLength: 1,
		Start:       time.Date(2020, time.March, 6, 2, 30, 0, 0, loc),
	},
		time.Date(2020, time.March, 6, 1, 0, 0, 0, loc),
		time.Date(2020, time.March, 10, 1, 0, 0, 0, loc),

		"2020-03-05 02:30:00 -0600 CST", // StartTime as of 1AM on the 6th would be 1:30AM on the 5th
		"2020-03-06 02:30:00 -0600 CST",
		"2020-03-07 02:30:00 -0600 CST",
		"2020-03-08 03:00:00 -0500 CDT",
		"2020-03-09 02:30:00 -0500 CDT",
		"2020-03-10 02:30:00 -0500 CDT",
	)

	check(&Rotation{
		Type:        TypeHourly,
		ShiftLength: 1,
		Start:       time.Date(2020, time.March, 6, 2, 30, 0, 0, loc),
	},
		time.Date(2020, time.March, 8, 1, 0, 0, 0, loc),
		time.Date(2020, time.March, 8, 4, 0, 0, 0, loc),

		"2020-03-08 00:30:00 -0600 CST",
		"2020-03-08 01:30:00 -0600 CST",
		"2020-03-08 03:00:00 -0500 CDT",
		"2020-03-08 03:30:00 -0500 CDT",
		"2020-03-08 04:30:00 -0500 CDT",
	)
	check(&Rotation{
		Type:        TypeHourly,
		ShiftLength: 1,
		Start:       time.Date(2020, time.March, 6, 2, 0, 0, 0, loc),
	},
		time.Date(2020, time.March, 8, 1, 0, 0, 0, loc),
		time.Date(2020, time.March, 8, 4, 0, 0, 0, loc),

		"2020-03-08 01:00:00 -0600 CST",
		"2020-03-08 03:00:00 -0500 CDT",
		"2020-03-08 04:00:00 -0500 CDT",
		"2020-03-08 05:00:00 -0500 CDT",
	)

	check(&Rotation{
		Type:        TypeHourly,
		ShiftLength: 1,
		Start:       time.Date(2020, time.July, 6, 2, 30, 0, 0, loc),
	},
		time.Date(2020, time.November, 1, 0, 0, 0, 0, loc),
		time.Date(2020, time.November, 1, 4, 0, 0, 0, loc),

		"2020-10-31 23:30:00 -0500 CDT",
		"2020-11-01 00:30:00 -0500 CDT",
		"2020-11-01 01:30:00 -0500 CDT",
		"2020-11-01 02:30:00 -0600 CST",
		"2020-11-01 03:30:00 -0600 CST",
		"2020-11-01 04:30:00 -0600 CST",
	)
	check(&Rotation{
		Type:        TypeHourly,
		ShiftLength: 1,
		Start:       time.Date(2020, time.July, 6, 2, 0, 0, 0, loc),
	},
		time.Date(2020, time.November, 1, 0, 0, 0, 0, loc),
		time.Date(2020, time.November, 1, 4, 0, 0, 0, loc),

		"2020-11-01 00:00:00 -0500 CDT",
		"2020-11-01 01:00:00 -0500 CDT",
		"2020-11-01 02:00:00 -0600 CST",
		"2020-11-01 03:00:00 -0600 CST",
		"2020-11-01 04:00:00 -0600 CST",
		"2020-11-01 05:00:00 -0600 CST",
	)

	// monthly rotation sanity check
	check(&Rotation{
		Type:        TypeMonthly,
		ShiftLength: 1,
		Start:       time.Date(2020, time.January, 2, 1, 30, 0, 0, loc),
	},
		time.Date(2020, time.April, 5, 1, 0, 0, 0, loc),
		time.Date(2020, time.June, 12, 1, 0, 0, 0, loc),

		"2020-04-02 01:30:00 -0500 CDT",
		"2020-05-02 01:30:00 -0500 CDT",
		"2020-06-02 01:30:00 -0500 CDT",
		"2020-07-02 01:30:00 -0500 CDT",
	)

	// check monthly rotations with shift from daylight savings to standard time
	check(&Rotation{
		Type:        TypeMonthly,
		ShiftLength: 1,
		Start:       time.Date(2020, time.October, 1, 1, 30, 0, 0, loc),
	},
		time.Date(2020, time.October, 2, 1, 0, 0, 0, loc),
		time.Date(2020, time.December, 2, 1, 0, 0, 0, loc),

		"2020-10-01 01:30:00 -0500 CDT",
		"2020-11-01 01:30:00 -0500 CDT",
		"2020-12-01 01:30:00 -0600 CST",
		"2021-01-01 01:30:00 -0600 CST",
	)

	loc, err = time.LoadLocation("Australia/Lord_Howe")
	require.NoError(t, err)

	// check when DT ends using a non-cst tz (hourly)
	check(&Rotation{
		Type:        TypeHourly,
		ShiftLength: 1,
		Start:       time.Date(2021, time.April, 3, 2, 30, 0, 0, loc),
	},
		time.Date(2021, time.April, 4, 1, 0, 0, 0, loc),
		time.Date(2021, time.April, 4, 4, 0, 0, 0, loc),

		"2021-04-04 00:30:00 +1100 +11",
		"2021-04-04 01:30:00 +1100 +11",   // clock goes back, extra 30 minutes on-call from 1:30-2
		"2021-04-04 02:30:00 +1030 +1030", // new shift starts at 2:30
		"2021-04-04 03:30:00 +1030 +1030",
		"2021-04-04 04:30:00 +1030 +1030",
	)

	// DST ending using a tz with a 30 minute offset change (daily)
	// handoff at end
	check(&Rotation{
		Type:        TypeDaily,
		ShiftLength: 1,
		Start:       time.Date(2021, time.April, 1, 2, 30, 0, 0, loc),
	},
		time.Date(2021, time.April, 2, 1, 0, 0, 0, loc),
		time.Date(2021, time.April, 6, 1, 0, 0, 0, loc),

		"2021-04-01 02:30:00 +1100 +11",
		"2021-04-02 02:30:00 +1100 +11",
		"2021-04-03 02:30:00 +1100 +11",
		"2021-04-04 02:30:00 +1030 +1030", // new offset changes 30 minutes
		"2021-04-05 02:30:00 +1030 +1030",
		"2021-04-06 02:30:00 +1030 +1030",
	)

	// DST ending using a tz with a 30 minute offset change
	// handoff at start
	check(&Rotation{
		Type:        TypeDaily,
		ShiftLength: 1,
		Start:       time.Date(2021, time.April, 2, 2, 0, 0, 0, loc),
	},
		time.Date(2021, time.April, 2, 1, 0, 0, 0, loc),
		time.Date(2021, time.April, 6, 1, 0, 0, 0, loc),

		"2021-04-01 02:00:00 +1100 +11",
		"2021-04-02 02:00:00 +1100 +11",
		"2021-04-03 02:00:00 +1100 +11",
		"2021-04-04 02:00:00 +1030 +1030", // TZ switches at start, +30 minutes on-call for this shift
		"2021-04-05 02:00:00 +1030 +1030",
		"2021-04-06 02:00:00 +1030 +1030",
	)

	// daily as 24 hours - sanity check
	check(&Rotation{
		Type:        TypeHourly,
		ShiftLength: 24,
		Start:       time.Date(2021, time.April, 2, 2, 0, 0, 0, loc),
	},
		time.Date(2021, time.April, 2, 1, 0, 0, 0, loc),
		time.Date(2021, time.April, 6, 1, 0, 0, 0, loc),

		"2021-04-01 02:00:00 +1100 +11",
		"2021-04-02 02:00:00 +1100 +11",
		"2021-04-03 02:00:00 +1100 +11",
		"2021-04-04 02:00:00 +1030 +1030", // TZ switches at start, +30 minutes on-call for this shift
		"2021-04-05 02:00:00 +1030 +1030",
		"2021-04-06 02:00:00 +1030 +1030",
	)

	// check when DST starts for non-CST timezone (Lord_Howe)
	check(&Rotation{
		Type:        TypeHourly,
		ShiftLength: 24,
		Start:       time.Date(2020, time.October, 2, 1, 30, 0, 0, loc),
	},
		time.Date(2020, time.October, 2, 1, 0, 0, 0, loc),
		time.Date(2020, time.October, 6, 1, 0, 0, 0, loc),

		"2020-10-01 01:30:00 +1030 +1030",
		"2020-10-02 01:30:00 +1030 +1030",
		"2020-10-03 01:30:00 +1030 +1030",
		"2020-10-04 01:30:00 +1030 +1030", // On 10/04 02:00 clocks go forward 30 mins.
		"2020-10-05 01:30:00 +1100 +11",
		"2020-10-06 01:30:00 +1100 +11",
	)

	// Daily & 24 hour should be identical
	check(&Rotation{
		Type:        TypeDaily,
		ShiftLength: 1,
		Start:       time.Date(2020, time.October, 2, 1, 30, 0, 0, loc),
	},
		time.Date(2020, time.October, 2, 1, 0, 0, 0, loc),
		time.Date(2020, time.October, 6, 1, 0, 0, 0, loc),

		"2020-10-01 01:30:00 +1030 +1030",
		"2020-10-02 01:30:00 +1030 +1030",
		"2020-10-03 01:30:00 +1030 +1030",
		"2020-10-04 01:30:00 +1030 +1030", // On 10/04 02:00 clocks go forward 30 mins.
		"2020-10-05 01:30:00 +1100 +11",
		"2020-10-06 01:30:00 +1100 +11",
	)

	// check when DST starts for non-CST timezone (Lord_Howe)
	// handoff time is in between the 30 min forwarding interval.
	check(&Rotation{
		Type:        TypeDaily,
		ShiftLength: 1,
		Start:       time.Date(2020, time.October, 2, 2, 15, 0, 0, loc),
	},
		time.Date(2020, time.October, 2, 1, 0, 0, 0, loc),
		time.Date(2020, time.October, 6, 1, 0, 0, 0, loc),

		"2020-10-01 02:15:00 +1030 +1030",
		"2020-10-02 02:15:00 +1030 +1030",
		"2020-10-03 02:15:00 +1030 +1030",
		"2020-10-04 02:30:00 +1100 +11", // On 10/04 02:00 clocks go forward 30 mins. Meaning 2:15 time does not actually exist anymore (since 2:00 becomes 2:30). Next immediate time that exists is 2:30.
		"2020-10-05 02:15:00 +1100 +11",
		"2020-10-06 02:15:00 +1100 +11",
	)
}

func TestRotation_EndTime_DST(t *testing.T) {
	tFmt := timeFmt + " (-07:00)"
	rot := &Rotation{
		Type:  TypeHourly,
		Start: mustParse(t, "Jan 1 2017 1:00 am"),
	}
	t.Logf("Rotation Start=%s", rot.Start.Format(tFmt))

	test := func(start, end time.Time) {
		t.Helper()
		t.Run("", func(t *testing.T) {
			t.Helper()
			t.Logf("Shift Start=%s", start.Format(tFmt))
			e := rot.EndTime(start)
			if !e.Equal(end) {
				t.Errorf("got '%s' want '%s'", e.Format(tFmt), end.Format(tFmt))
			}
		})
	}

	start := rot.Start.AddDate(0, 2, 11) // mar 11 1:00am
	end := start.Add(time.Hour)          // same time (we skip a shift)
	test(start, end)

	start = rot.Start.AddDate(0, 10, 4) // nov 5 1:00am
	end = start.Add(time.Hour * 2)      // 2 hours after
	test(start, end)
}

func TestRotation_EndTime_ConfigChange(t *testing.T) {
	rot := &Rotation{
		Type:        TypeHourly,
		Start:       mustParse(t, "Jan 1 2017 12:00 am"),
		ShiftLength: 12,
	}

	start := mustParse(t, "Jan 3 2017 6:00 am")
	result := rot.EndTime(start)

	expected := mustParse(t, "Jan 3 2017 12:00 pm")
	if !result.Equal(expected) {
		t.Errorf("EndTime=%s; want %s", result.Format(timeFmt), expected.Format(timeFmt))
	}
}

func TestRotation_EndTime(t *testing.T) {

	t.Run("cycle forward", func(t *testing.T) {
		// 2am june 1st
		orig := time.Date(2020, time.June, 1, 3, 0, 0, 0, time.UTC)
		rot := &Rotation{
			Type:        TypeHourly,
			ShiftLength: 10,
			Start:       orig,
		}

		res := rot.EndTime(orig.Add(30 * time.Hour))
		// 7pm following day (40 hours)
		assert.Equal(t, "2020-06-02 19:00:00 +0000 UTC", res.String())
	})
	t.Run("cycle back", func(t *testing.T) {
		// 2am june 1st
		orig := time.Date(2020, time.June, 1, 3, 0, 0, 0, time.UTC)
		rot := &Rotation{
			Type:        TypeHourly,
			ShiftLength: 10,
			Start:       orig,
		}

		res := rot.EndTime(orig.Add(-30 * time.Hour))
		// 7am previous day (-30 hours + 10 hours)
		assert.Equal(t, "2020-05-31 07:00:00 +0000 UTC", res.String())
	})

	test := func(start, end string, len int, dur time.Duration, typ Type) {
		t.Run(string(typ), func(t *testing.T) {
			s := mustParse(t, start)
			e := mustParse(t, end)
			dur = dur.Round(time.Second)
			if e.Sub(s).Round(time.Second) != dur {
				t.Fatalf("bad test data: end-start=%s; want %s", e.Sub(s).Round(time.Second).String(), dur.String())
			}
			rot := &Rotation{
				Type:        typ,
				ShiftLength: len,
				Start:       s,
			}

			result := rot.EndTime(s)
			if !result.Equal(e) {
				t.Errorf("got '%s'; want '%s'", result.Format(timeFmt), end)
			}
			if result.Sub(s).Round(time.Second) != dur {
				t.Errorf("duration was '%s'; want '%s'", result.Sub(s).Round(time.Second).String(), dur.String())
			}
		})
	}

	type dat struct {
		s   string
		l   int
		exp string
		dur time.Duration
	}

	// monthly
	data := []dat{
		{s: "Jun 1 2017 12:00 am", l: 1, exp: "Jul 1 2017 12:00 am", dur: time.Hour * 24 * 30},
		{s: "Jul 1 2017 12:00 am", l: 2, exp: "Sep 1 2017 12:00 am", dur: time.Hour * 24 * 31 * 2},

		// DST tests
		{s: "Mar 1 2017 12:00 am", l: 1, exp: "Apr 1 2017 12:00 am", dur: time.Hour*24*31 - time.Hour},
		{s: "Nov 1 2017 12:00 am", l: 2, exp: "Jan 1 2018 12:00 am", dur: time.Hour*(24*(30+31)) + time.Hour},
	}
	for _, d := range data {
		test(d.s, d.exp, d.l, d.dur, TypeMonthly)
	}

	// weekly
	data = []dat{
		{s: "Jun 10 2017 8:00 am", l: 1, exp: "Jun 17 2017 8:00 am", dur: time.Hour * 24 * 7},
		{s: "Jun 10 2017 8:00 am", l: 2, exp: "Jun 24 2017 8:00 am", dur: time.Hour * 24 * 7 * 2},

		// DST tests
		{s: "Mar 10 2017 8:00 am", l: 1, exp: "Mar 17 2017 8:00 am", dur: time.Hour*24*7 - time.Hour},
		{s: "Nov 4 2017 8:00 am", l: 1, exp: "Nov 11 2017 8:00 am", dur: time.Hour*24*7 + time.Hour},
	}
	for _, d := range data {
		test(d.s, d.exp, d.l, d.dur, TypeWeekly)
	}

	// daily
	data = []dat{
		{s: "Jun 10 2017 8:00 am", l: 1, exp: "Jun 11 2017 8:00 am", dur: time.Hour * 24},
		{s: "Jun 10 2017 8:00 am", l: 2, exp: "Jun 12 2017 8:00 am", dur: time.Hour * 24 * 2},

		// DST tests
		{s: "Mar 11 2017 8:00 am", l: 1, exp: "Mar 12 2017 8:00 am", dur: time.Hour * 23},
		{s: "Nov 4 2017 8:00 am", l: 1, exp: "Nov 5 2017 8:00 am", dur: time.Hour * 25},
	}
	for _, d := range data {
		test(d.s, d.exp, d.l, d.dur, TypeDaily)
	}

	// hourly
	data = []dat{
		{s: "Jun 10 2017 8:00 am", l: 1, exp: "Jun 10 2017 9:00 am", dur: time.Hour},
		{s: "Jun 10 2017 8:00 am", l: 2, exp: "Jun 10 2017 10:00 am", dur: time.Hour * 2},

		// DST tests
		{s: "Mar 12 2017 12:00 am", l: 3, exp: "Mar 12 2017 3:00 am", dur: time.Hour * 2},
		{s: "Nov 5 2017 12:00 am", l: 3, exp: "Nov 5 2017 3:00 am", dur: time.Hour * 4},
	}
	for _, d := range data {
		test(d.s, d.exp, d.l, d.dur, TypeHourly)
	}

	t.Run("subsequent calls (hourly)", func(t *testing.T) {
		orig := time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)
		r := &Rotation{
			Type:        TypeHourly,
			ShiftLength: 1,
			Start:       orig,
		}
		ts := r.EndTime(orig.Add(-2 * time.Hour))
		assert.Equal(t, orig.Add(-time.Hour).String(), ts.String())

		ts = r.EndTime(ts)
		assert.Equal(t, orig.String(), ts.String())

		ts = r.EndTime(ts)
		assert.Equal(t, orig.Add(time.Hour).String(), ts.String())
	})

	t.Run("subsequent calls (daily)", func(t *testing.T) {
		orig := time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)
		r := &Rotation{
			Type:        TypeDaily,
			ShiftLength: 1,
			Start:       orig,
		}
		ts := r.EndTime(orig.AddDate(0, 0, -2))
		assert.Equal(t, orig.AddDate(0, 0, -1).String(), ts.String())

		ts = r.EndTime(ts)
		assert.Equal(t, orig.String(), ts.String())

		ts = r.EndTime(ts)
		assert.Equal(t, orig.AddDate(0, 0, 1).String(), ts.String())
	})

	t.Run("subsequent calls (monthly)", func(t *testing.T) {
		orig := time.Date(2020, time.January, 10, 0, 0, 0, 0, time.UTC)
		r := &Rotation{
			Type:        TypeMonthly,
			ShiftLength: 1,
			Start:       orig,
		}
		ts := r.EndTime(orig.AddDate(0, -2, 0))
		assert.Equal(t, orig.AddDate(0, -1, 0).String(), ts.String())

		ts = r.EndTime(ts)
		assert.Equal(t, orig.String(), ts.String())

		ts = r.EndTime(ts)
		assert.Equal(t, orig.AddDate(0, 1, 0).String(), ts.String())
	})
}

func TestRotation_Normalize(t *testing.T) {

	test := func(valid bool, r Rotation) {
		name := "valid"
		if !valid {
			name = "invalid"
		}
		t.Run(name, func(t *testing.T) {
			_, err := r.Normalize()
			if valid && err != nil {
				t.Errorf("err = %v; want nil", err)
			} else if !valid && err == nil {
				t.Errorf("got nil err; want non-nil")
			}
		})
	}

	valid := []Rotation{
		{Name: "Default", ShiftLength: 1, Type: TypeWeekly, Description: "Default Rotation"},
	}
	invalid := []Rotation{
		{Name: "D", ShiftLength: -100, Type: TypeWeekly, Description: "Default Rotation"},
	}
	for _, r := range valid {
		test(true, r)
	}
	for _, r := range invalid {
		test(false, r)
	}
}

func TestRotation_FutureStart(t *testing.T) {
	rot := Rotation{
		Type:        TypeDaily,
		ShiftLength: 1,

		// StartTime and EndTime should work correctly even if Start
		// is in the future.
		Start: time.Date(2019, 0, 10, 0, 0, 0, 0, time.UTC),
	}

	assert.Equal(t, time.Date(2019, 0, 6, 0, 0, 0, 0, time.UTC),
		rot.EndTime(time.Date(2019, 0, 5, 0, 0, 0, 0, time.UTC)),
	)
	assert.Equal(t, time.Date(2019, 0, 5, 0, 0, 0, 0, time.UTC),
		rot.StartTime(time.Date(2019, 0, 5, 0, 0, 0, 0, time.UTC)),
	)
	require.Equal(t, time.Date(2019, 0, 4, 0, 0, 0, 0, time.UTC),
		rot.StartTime(time.Date(2019, 0, 5, 0, 0, 0, -1, time.UTC)),
	)
	assert.Equal(t, time.Date(2019, 0, 11, 0, 0, 0, 0, time.UTC),
		rot.EndTime(time.Date(2019, 0, 10, 0, 0, 0, 0, time.UTC)),
	)
	assert.Equal(t, time.Date(2019, 0, 10, 0, 0, 0, 0, time.UTC),
		rot.StartTime(time.Date(2019, 0, 10, 0, 0, 0, 0, time.UTC)),
	)
	assert.Equal(t, time.Date(2019, 0, 9, 0, 0, 0, 0, time.UTC),
		rot.StartTime(time.Date(2019, 0, 10, 0, 0, 0, -1, time.UTC)),
	)
}

func TestRotation_StartTime(t *testing.T) {
	test := func(start, end string, len int, dur time.Duration, typ Type) {
		t.Run(string(typ), func(t *testing.T) {
			s := mustParse(t, start)
			e := mustParse(t, end)
			dur = dur.Round(time.Second)
			if e.Sub(s).Round(time.Second) != dur {
				t.Fatalf("bad test data: end-start=%s; want %s", e.Sub(s).Round(time.Second).String(), dur.String())
			}
			rot := &Rotation{
				Type:        typ,
				ShiftLength: len,
				Start:       s,
			}

			result := rot.StartTime(s)
			if !result.Equal(s) {
				t.Errorf("got '%s'; want '%s'", result.Format(timeFmt), start)
			}

			if result.Sub(e).Round(time.Second) != -(dur) {
				t.Errorf("duration was '%s'; want '%s'", result.Sub(e).Round(time.Second).String(), dur.String())
			}
		})
	}

	type dat struct {
		s   string
		l   int
		exp string
		dur time.Duration
	}

	// monthly
	data := []dat{
		{s: "Jun 1 2017 12:00 am", l: 1, exp: "Jul 1 2017 12:00 am", dur: time.Hour * 24 * 30},
		{s: "Jul 1 2017 12:00 am", l: 2, exp: "Sep 1 2017 12:00 am", dur: time.Hour * 24 * 31 * 2},

		// DST tests
		{s: "Mar 1 2017 12:00 am", l: 1, exp: "Apr 1 2017 12:00 am", dur: time.Hour*24*31 - time.Hour},
		{s: "Nov 1 2017 12:00 am", l: 2, exp: "Jan 1 2018 12:00 am", dur: time.Hour*(24*(31+30)) + time.Hour},
	}
	for _, d := range data {
		test(d.s, d.exp, d.l, d.dur, TypeMonthly)
	}

	// weekly
	data = []dat{
		{s: "Jun 10 2017 8:00 am", l: 1, exp: "Jun 17 2017 8:00 am", dur: time.Hour * 24 * 7},
		{s: "Jun 10 2017 8:00 am", l: 2, exp: "Jun 24 2017 8:00 am", dur: time.Hour * 24 * 7 * 2},

		// DST tests
		{s: "Mar 10 2017 8:00 am", l: 1, exp: "Mar 17 2017 8:00 am", dur: time.Hour*24*7 - time.Hour},
		{s: "Nov 4 2017 8:00 am", l: 1, exp: "Nov 11 2017 8:00 am", dur: time.Hour*24*7 + time.Hour},
	}
	for _, d := range data {
		test(d.s, d.exp, d.l, d.dur, TypeWeekly)
	}

	// weekly but with different start timestamp for comparison
	data = []dat{
		{s: "Jun 10 2017 8:00 am", l: 1, exp: "Jun 17 2017 8:00 am", dur: time.Hour * 24 * 7},
	}
	for _, d := range data {
		test("Jun 16 2017 8:00 am", d.exp, d.l, time.Hour*24, TypeWeekly)
	}

	// daily
	data = []dat{
		{s: "Jun 10 2017 8:00 am", l: 1, exp: "Jun 11 2017 8:00 am", dur: time.Hour * 24},
		{s: "Jun 10 2017 8:00 am", l: 2, exp: "Jun 12 2017 8:00 am", dur: time.Hour * 24 * 2},

		// DST tests
		{s: "Mar 11 2017 8:00 am", l: 1, exp: "Mar 12 2017 8:00 am", dur: time.Hour * 23},
		{s: "Nov 4 2017 8:00 am", l: 1, exp: "Nov 5 2017 8:00 am", dur: time.Hour * 25},
	}
	for _, d := range data {
		test(d.s, d.exp, d.l, d.dur, TypeDaily)
	}

	// hourly
	data = []dat{
		{s: "Jun 10 2017 8:00 am", l: 1, exp: "Jun 10 2017 9:00 am", dur: time.Hour},
		{s: "Jun 10 2017 8:00 am", l: 2, exp: "Jun 10 2017 10:00 am", dur: time.Hour * 2},

		// DST tests
		{s: "Mar 12 2017 12:00 am", l: 3, exp: "Mar 12 2017 3:00 am", dur: time.Hour * 2},
		{s: "Nov 5 2017 12:00 am", l: 3, exp: "Nov 5 2017 3:00 am", dur: time.Hour * 4},
	}
	for _, d := range data {
		test(d.s, d.exp, d.l, d.dur, TypeHourly)
	}
}