time_marshalling_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. package jsonlog // import "github.com/docker/docker/daemon/logger/jsonfilelog/jsonlog"
  2. import (
  3. "testing"
  4. "time"
  5. "gotest.tools/v3/assert"
  6. is "gotest.tools/v3/assert/cmp"
  7. )
  8. func TestFastTimeMarshalJSONWithInvalidYear(t *testing.T) {
  9. aTime := time.Date(-1, 1, 1, 0, 0, 0, 0, time.Local)
  10. _, err := fastTimeMarshalJSON(aTime)
  11. assert.Check(t, is.ErrorContains(err, "year outside of range"))
  12. anotherTime := time.Date(10000, 1, 1, 0, 0, 0, 0, time.Local)
  13. _, err = fastTimeMarshalJSON(anotherTime)
  14. assert.Check(t, is.ErrorContains(err, "year outside of range"))
  15. }
  16. func TestFastTimeMarshalJSON(t *testing.T) {
  17. aTime := time.Date(2015, 5, 29, 11, 1, 2, 3, time.UTC)
  18. json, err := fastTimeMarshalJSON(aTime)
  19. assert.NilError(t, err)
  20. assert.Check(t, is.Equal("\"2015-05-29T11:01:02.000000003Z\"", json))
  21. location, err := time.LoadLocation("Europe/Paris")
  22. assert.NilError(t, err)
  23. aTime = time.Date(2015, 5, 29, 11, 1, 2, 3, location)
  24. json, err = fastTimeMarshalJSON(aTime)
  25. assert.NilError(t, err)
  26. assert.Check(t, is.Equal("\"2015-05-29T11:01:02.000000003+02:00\"", json))
  27. }