time_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package util
  2. import (
  3. "github.com/stretchr/testify/require"
  4. "testing"
  5. "time"
  6. )
  7. var (
  8. // 2021-12-10 10:17:23 (Friday)
  9. base = time.Date(2021, 12, 10, 10, 17, 23, 0, time.UTC)
  10. )
  11. func TestNextOccurrenceUTC_NextDate(t *testing.T) {
  12. loc, err := time.LoadLocation("America/New_York")
  13. require.Nil(t, err)
  14. timeOfDay := time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC) // Run at midnight UTC
  15. nowInFairfieldCT := time.Date(2023, time.January, 10, 22, 19, 12, 0, loc)
  16. nextRunTme := NextOccurrenceUTC(timeOfDay, nowInFairfieldCT)
  17. require.Equal(t, time.Date(2023, time.January, 12, 0, 0, 0, 0, time.UTC), nextRunTme)
  18. }
  19. func TestNextOccurrenceUTC_SameDay(t *testing.T) {
  20. loc, err := time.LoadLocation("America/New_York")
  21. require.Nil(t, err)
  22. timeOfDay := time.Date(0, 0, 0, 4, 0, 0, 0, time.UTC) // Run at 4am UTC
  23. nowInFairfieldCT := time.Date(2023, time.January, 10, 22, 19, 12, 0, loc)
  24. nextRunTme := NextOccurrenceUTC(timeOfDay, nowInFairfieldCT)
  25. require.Equal(t, time.Date(2023, time.January, 11, 4, 0, 0, 0, time.UTC), nextRunTme)
  26. }
  27. func TestParseFutureTime_11am_FutureTime(t *testing.T) {
  28. d, err := ParseFutureTime("11am", base)
  29. require.Nil(t, err)
  30. require.Equal(t, time.Date(2021, 12, 10, 11, 0, 0, 0, time.UTC), d) // Same day
  31. }
  32. func TestParseFutureTime_9am_PastTime(t *testing.T) {
  33. d, err := ParseFutureTime("9am", base)
  34. require.Nil(t, err)
  35. require.Equal(t, time.Date(2021, 12, 11, 9, 0, 0, 0, time.UTC), d) // Next day
  36. }
  37. func TestParseFutureTime_Monday_10_30pm_FutureTime(t *testing.T) {
  38. d, err := ParseFutureTime("Monday, 10:30pm", base)
  39. require.Nil(t, err)
  40. require.Equal(t, time.Date(2021, 12, 13, 22, 30, 0, 0, time.UTC), d)
  41. }
  42. func TestParseFutureTime_30m(t *testing.T) {
  43. d, err := ParseFutureTime("30m", base)
  44. require.Nil(t, err)
  45. require.Equal(t, time.Date(2021, 12, 10, 10, 47, 23, 0, time.UTC), d)
  46. }
  47. func TestParseFutureTime_30min(t *testing.T) {
  48. d, err := ParseFutureTime("30min", base)
  49. require.Nil(t, err)
  50. require.Equal(t, time.Date(2021, 12, 10, 10, 47, 23, 0, time.UTC), d)
  51. }
  52. func TestParseFutureTime_3h(t *testing.T) {
  53. d, err := ParseFutureTime("3h", base)
  54. require.Nil(t, err)
  55. require.Equal(t, time.Date(2021, 12, 10, 13, 17, 23, 0, time.UTC), d)
  56. }
  57. func TestParseFutureTime_1day(t *testing.T) {
  58. d, err := ParseFutureTime("1 day", base)
  59. require.Nil(t, err)
  60. require.Equal(t, time.Date(2021, 12, 11, 10, 17, 23, 0, time.UTC), d)
  61. }
  62. func TestParseFutureTime_UnixTime(t *testing.T) {
  63. d, err := ParseFutureTime("1639183911", base)
  64. require.Nil(t, err)
  65. require.Equal(t, time.Date(2021, 12, 11, 0, 51, 51, 0, time.UTC), d)
  66. }
  67. func TestParseDuration(t *testing.T) {
  68. d, err := ParseDuration("2d")
  69. require.Nil(t, err)
  70. require.Equal(t, 48*time.Hour, d)
  71. d, err = ParseDuration("2h")
  72. require.Nil(t, err)
  73. require.Equal(t, 2*time.Hour, d)
  74. d, err = ParseDuration("0")
  75. require.Nil(t, err)
  76. require.Equal(t, time.Duration(0), d)
  77. }
  78. func TestFormatDuration(t *testing.T) {
  79. values := []struct {
  80. duration time.Duration
  81. expected string
  82. }{
  83. {24 * time.Second, "24s"},
  84. {56 * time.Minute, "56m"},
  85. {time.Hour, "1h"},
  86. {2 * time.Hour, "2h"},
  87. {24 * time.Hour, "1d"},
  88. {3 * 24 * time.Hour, "3d"},
  89. }
  90. for _, value := range values {
  91. require.Equal(t, value.expected, FormatDuration(value.duration))
  92. d, err := ParseDuration(FormatDuration(value.duration))
  93. require.Nil(t, err)
  94. require.Equalf(t, value.duration, d, "duration does not match: %v != %v", value.duration, d)
  95. }
  96. }
  97. func TestFormatDuration_Rounded(t *testing.T) {
  98. require.Equal(t, "1d", FormatDuration(47*time.Hour))
  99. }