time_as_int64_codec_test.go 857 B

12345678910111213141516171819202122232425262728293031
  1. package extra
  2. import (
  3. "github.com/json-iterator/go"
  4. "github.com/stretchr/testify/require"
  5. "testing"
  6. "time"
  7. )
  8. func Test_time_as_int64(t *testing.T) {
  9. should := require.New(t)
  10. RegisterTimeAsInt64Codec(time.Nanosecond)
  11. output, err := jsoniter.Marshal(time.Unix(1497952257, 1002))
  12. should.Nil(err)
  13. should.Equal("1497952257000001002", string(output))
  14. var val time.Time
  15. should.Nil(jsoniter.Unmarshal(output, &val))
  16. should.Equal(int64(1497952257000001002), val.UnixNano())
  17. }
  18. func Test_time_as_int64_keep_microsecond(t *testing.T) {
  19. t.Skip("conflict")
  20. should := require.New(t)
  21. RegisterTimeAsInt64Codec(time.Microsecond)
  22. output, err := jsoniter.Marshal(time.Unix(1, 1002))
  23. should.Nil(err)
  24. should.Equal("1000001", string(output))
  25. var val time.Time
  26. should.Nil(jsoniter.Unmarshal(output, &val))
  27. should.Equal(int64(1000001000), val.UnixNano())
  28. }