iso8601_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package utils
  3. import (
  4. "fmt"
  5. "testing"
  6. "time"
  7. "github.com/google/go-cmp/cmp"
  8. )
  9. var _ = fmt.Print
  10. func TestISO8601(t *testing.T) {
  11. now := time.Now()
  12. tt := func(raw string, expected time.Time) {
  13. actual, err := ISO8601Parse(raw)
  14. if err != nil {
  15. t.Fatalf("Parsing: %#v failed with error: %s", raw, err)
  16. }
  17. if diff := cmp.Diff(expected, actual); diff != "" {
  18. t.Fatalf("Parsing: %#v failed:\n%s", raw, diff)
  19. }
  20. }
  21. tt(ISO8601Format(now), now)
  22. tt("2023-02-08T07:24:09.551975+00:00", time.Date(2023, 2, 8, 7, 24, 9, 551975000, time.UTC))
  23. tt("2023-02-08T07:24:09.551975Z", time.Date(2023, 2, 8, 7, 24, 9, 551975000, time.UTC))
  24. tt("2023", time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC))
  25. tt("2023-11-13", time.Date(2023, 11, 13, 0, 0, 0, 0, time.UTC))
  26. tt("2023-11-13 07:23", time.Date(2023, 11, 13, 7, 23, 0, 0, time.UTC))
  27. tt("2023-11-13 07:23:01", time.Date(2023, 11, 13, 7, 23, 1, 0, time.UTC))
  28. tt("2023-11-13 07:23:01.", time.Date(2023, 11, 13, 7, 23, 1, 0, time.UTC))
  29. tt("2023-11-13 07:23:01.0", time.Date(2023, 11, 13, 7, 23, 1, 0, time.UTC))
  30. tt("2023-11-13 07:23:01.1", time.Date(2023, 11, 13, 7, 23, 1, 100000000, time.UTC))
  31. tt("202311-13 07", time.Date(2023, 11, 13, 7, 0, 0, 0, time.UTC))
  32. tt("20231113 0705", time.Date(2023, 11, 13, 7, 5, 0, 0, time.UTC))
  33. }