time_marshalling_test.go 1.0 KB

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