utils_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package timeutils
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. )
  7. func TestGetTimestamp(t *testing.T) {
  8. now := time.Now()
  9. cases := []struct{ in, expected string }{
  10. {"0", "-62167305600"}, // 0 gets parsed year 0
  11. // Partial RFC3339 strings get parsed with second precision
  12. {"2006-01-02T15:04:05.999999999+07:00", "1136189045"},
  13. {"2006-01-02T15:04:05.999999999Z", "1136214245"},
  14. {"2006-01-02T15:04:05.999999999", "1136214245"},
  15. {"2006-01-02T15:04:05", "1136214245"},
  16. {"2006-01-02T15:04", "1136214240"},
  17. {"2006-01-02T15", "1136214000"},
  18. {"2006-01-02T", "1136160000"},
  19. {"2006-01-02", "1136160000"},
  20. {"2006", "1136073600"},
  21. {"2015-05-13T20:39:09Z", "1431549549"},
  22. // unix timestamps returned as is
  23. {"1136073600", "1136073600"},
  24. // Durations
  25. {"1m", fmt.Sprintf("%d", now.Add(-1*time.Minute).Unix())},
  26. {"1.5h", fmt.Sprintf("%d", now.Add(-90*time.Minute).Unix())},
  27. {"1h30m", fmt.Sprintf("%d", now.Add(-90*time.Minute).Unix())},
  28. // String fallback
  29. {"invalid", "invalid"},
  30. }
  31. for _, c := range cases {
  32. o := GetTimestamp(c.in, now)
  33. if o != c.expected {
  34. t.Fatalf("wrong value for '%s'. expected:'%s' got:'%s'", c.in, c.expected, o)
  35. }
  36. }
  37. }