utils_test.go 929 B

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