storm icon indicating copy to clipboard operation
storm copied to clipboard

time.Time is loosing precision when saved/loaded

Open tgirod opened this issue 5 years ago • 1 comments

Consider this:

type Student struct {
	ID        int
	CreatedAt time.Time
}

func main() {
	s := Student{
		ID:        1,
		CreatedAt: time.Now(),
	}
	fmt.Println("original")
	fmt.Println(s)
	// db, _ := storm.Open("my.db", storm.Codec(gob.Codec))
	db, _ := storm.Open("my.db")
	if err := db.Save(&s); err != nil {
		panic(err)
	}
	fmt.Println("saved")
	fmt.Println(s)

	s2 := new(Student)
	if err := db.One("ID", s.ID, s2); err != nil {
		panic(err)
	}
	fmt.Println("fetched")
	fmt.Println(*s2)
}

Simple: the student is saved in the database, then reloaded. I expect the two objects to be the same. Here is what I get:

go run test.go 
original
{1 2018-12-19 10:27:11.293548697 +0100 CET m=+0.000199613}
saved
{1 2018-12-19 10:27:11.293548697 +0100 CET m=+0.000199613}
fetched
{1 2018-12-19 10:27:11.293548697 +0100 CET}

Why is time.Time loosing precision in the process ? I have the same problem with gob.Codec.

tgirod avatar Dec 19 '18 09:12 tgirod

This is not related to Storm, this is related to the way JSON and Gob encode and decode time. If you need more information you can take a look at this issue: https://github.com/golang/go/issues/17875

asdine avatar Dec 22 '18 20:12 asdine