time_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. }