MCPcopy Index your code
hub / github.com/coder/coder / TestFindWeek

Function TestFindWeek

coderd/schedule/autostop_test.go:606–684  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

604}
605
606func TestFindWeek(t *testing.T) {
607 t.Parallel()
608
609 timezones := []string{
610 "UTC",
611 "America/Los_Angeles",
612 "America/New_York",
613 "Europe/Dublin",
614 "Europe/London",
615 "Europe/Paris",
616 "Asia/Kolkata", // India (UTC+5:30)
617 "Asia/Tokyo",
618 "Australia/Sydney",
619 "Australia/Brisbane",
620 }
621
622 for _, tz := range timezones {
623 t.Run("Loc/"+tz, func(t *testing.T) {
624 t.Parallel()
625
626 loc, err := time.LoadLocation(tz)
627 require.NoError(t, err)
628
629 now := time.Now().In(loc)
630 currentWeek, err := schedule.WeeksSinceEpoch(now)
631 require.NoError(t, err)
632
633 diffMonday := now.Weekday() - time.Monday
634 if now.Weekday() == time.Sunday {
635 // Sunday is 0, but Monday is the first day of the week in the
636 // code.
637 diffMonday = 6
638 }
639 currentWeekMondayExpected := now.AddDate(0, 0, -int(diffMonday))
640 require.Equal(t, time.Monday, currentWeekMondayExpected.Weekday())
641 y, m, d := currentWeekMondayExpected.Date()
642 // Change to midnight.
643 currentWeekMondayExpected = time.Date(y, m, d, 0, 0, 0, 0, loc)
644
645 currentWeekMonday, err := schedule.GetMondayOfWeek(now.Location(), currentWeek)
646 require.NoError(t, err)
647 require.Equal(t, time.Monday, currentWeekMonday.Weekday())
648 require.Equal(t, currentWeekMondayExpected, currentWeekMonday)
649
650 t.Log("now", now)
651 t.Log("currentWeek", currentWeek)
652 t.Log("currentMonday", currentWeekMonday)
653
654 // Loop through every single Monday and Sunday for the next 100
655 // years and make sure the week calculations are correct.
656 for i := int64(1); i < 52*100; i++ {
657 msg := fmt.Sprintf("week %d", i)
658
659 monday := currentWeekMonday.AddDate(0, 0, int(i*7))
660 y, m, d := monday.Date()
661 monday = time.Date(y, m, d, 0, 0, 0, 0, loc)
662 require.Equal(t, monday.Weekday(), time.Monday, msg)
663 t.Log(msg, "monday", monday)

Callers

nothing calls this directly

Calls 6

WeeksSinceEpochFunction · 0.92
GetMondayOfWeekFunction · 0.92
LocationMethod · 0.80
LogMethod · 0.80
RunMethod · 0.65
EqualMethod · 0.45

Tested by

no test coverage detected