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