asdine / storm

Simple and powerful toolkit for BoltDB
MIT License
2.06k stars 139 forks source link

time.Time is loosing precision when saved/loaded #242

Open tgirod opened 5 years ago

tgirod commented 5 years ago

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.

asdine commented 5 years ago

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