time_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 TestParseFutureTime_11am_FutureTime(t *testing.T) {
  12. d, err := ParseFutureTime("11am", base)
  13. require.Nil(t, err)
  14. require.Equal(t, time.Date(2021, 12, 10, 11, 0, 0, 0, time.UTC), d) // Same day
  15. }
  16. func TestParseFutureTime_9am_PastTime(t *testing.T) {
  17. d, err := ParseFutureTime("9am", base)
  18. require.Nil(t, err)
  19. require.Equal(t, time.Date(2021, 12, 11, 9, 0, 0, 0, time.UTC), d) // Next day
  20. }
  21. func TestParseFutureTime_Monday_10_30pm_FutureTime(t *testing.T) {
  22. d, err := ParseFutureTime("Monday, 10:30pm", base)
  23. require.Nil(t, err)
  24. require.Equal(t, time.Date(2021, 12, 13, 22, 30, 0, 0, time.UTC), d)
  25. }
  26. func TestParseFutureTime_30m(t *testing.T) {
  27. d, err := ParseFutureTime("30m", base)
  28. require.Nil(t, err)
  29. require.Equal(t, time.Date(2021, 12, 10, 10, 47, 23, 0, time.UTC), d)
  30. }
  31. func TestParseFutureTime_30min(t *testing.T) {
  32. d, err := ParseFutureTime("30min", base)
  33. require.Nil(t, err)
  34. require.Equal(t, time.Date(2021, 12, 10, 10, 47, 23, 0, time.UTC), d)
  35. }
  36. func TestParseFutureTime_3h(t *testing.T) {
  37. d, err := ParseFutureTime("3h", base)
  38. require.Nil(t, err)
  39. require.Equal(t, time.Date(2021, 12, 10, 13, 17, 23, 0, time.UTC), d)
  40. }
  41. func TestParseFutureTime_1day(t *testing.T) {
  42. d, err := ParseFutureTime("1 day", base)
  43. require.Nil(t, err)
  44. require.Equal(t, time.Date(2021, 12, 11, 10, 17, 23, 0, time.UTC), d)
  45. }
  46. func TestParseFutureTime_UnixTime(t *testing.T) {
  47. d, err := ParseFutureTime("1639183911", base)
  48. require.Nil(t, err)
  49. require.Equal(t, time.Date(2021, 12, 11, 0, 51, 51, 0, time.UTC), d)
  50. }