Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Marshal and Unmarshal a time.Time, I got diff value. It is work in golang 1.8, but golang 1.9 #22957

Closed
zhijiay opened this issue Dec 1, 2017 · 3 comments

Comments

@zhijiay
Copy link

zhijiay commented Dec 1, 2017

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go1.9.2 linux/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

ubuntu

go test follow file

package simple_struct

import (
    "time"
    "encoding/json"
    "github.com/stretchr/testify/assert"
    "testing"
)

type SimpleStruct struct {
    CreateTime time.Time
    Other      string
}

func NewSimpleStruct() *SimpleStruct {
    return &SimpleStruct{
        CreateTime: time.Now(),
    }
}

func Test(t *testing.T) {
    s1 := NewSimpleStruct()
    data, err := json.Marshal(s1)
    assert.Nil(t, err)
    s2 := &SimpleStruct{}
    err = json.Unmarshal(data, s2)
    assert.Nil(t, err)
    assert.Equal(t, s2, s1) // work in golang 1.8, but golang 1.9
  
}


@zhijiay
Copy link
Author

zhijiay commented Dec 1, 2017

Error Trace:	simple_test.go:28
Error:      	Not equal: 
            	expected: &simple_struct.SimpleStruct{CreateTime:time.Time{wall:0x1e13a63e, ext:63647709442, loc:(*time.Location)(0x871cc0)}, Other:""}
            	actual: &simple_struct.SimpleStruct{CreateTime:time.Time{wall:0xbe805e609e13a63e, ext:2691802, loc:(*time.Location)(0x871cc0)}, Other:""}
            	
            	Diff:
            	--- Expected
            	+++ Actual
            	@@ -1,3 +1,3 @@
            	 (*simple_struct.SimpleStruct)({
            	- CreateTime: (time.Time) 2017-12-01 15:17:22.504604222 +0800 CST,
            	+ CreateTime: (time.Time) 2017-12-01 15:17:22.504604222 +0800 CST m=+0.002691802,

@bradfitz
Copy link
Contributor

bradfitz commented Dec 1, 2017

Yes, this was the change to time.Time in Go 1.9. You'll need to update your test to not test so deeply.

See:
https://golang.org/doc/go1.9#monotonic-time
https://golang.org/pkg/time/#hdr-Monotonic_Clocks

Notably, this part:

The canonical way to strip a monotonic clock reading is to use t = t.Round(0).

Alternatively, for less brittle comparisons, see:
https://github.com/google/go-cmp

@bradfitz bradfitz closed this as completed Dec 1, 2017
@zhijiay
Copy link
Author

zhijiay commented Dec 1, 2017

thanks

@golang golang locked and limited conversation to collaborators Dec 1, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants