Time comparison

1,308 views
Skip to first unread message

rif

unread,
Jul 15, 2013, 12:59:43 PM7/15/13
to golan...@googlegroups.com
I find the first two false values surprising, can someone explain?


package main

import (
    "fmt"
    "reflect"
    "time"
)

func main() {
    var t1 time.Time
    s := t1.In(time.UTC).Format(time.RFC3339)
    t2, err := time.Parse(time.RFC3339, s)
    fmt.Println(err, reflect.DeepEqual(t1, t2), t1 == t2, t1.Equal(t2), t1, t2)
}

<nil> false false true 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC

http://play.golang.org/p/0RyQfFqk7s


DisposaBoy

unread,
Jul 15, 2013, 1:27:33 PM7/15/13
to golan...@googlegroups.com
use printf to look at what's inside the time objects http://play.golang.org/p/idc9iePEj0

rif

unread,
Jul 15, 2013, 1:32:10 PM7/15/13
to golan...@googlegroups.com
Can you make a correct version?

Kamil Kisiel

unread,
Jul 15, 2013, 1:38:16 PM7/15/13
to golan...@googlegroups.com

GreatOdinsRaven

unread,
Jul 15, 2013, 1:43:02 PM7/15/13
to golan...@googlegroups.com
The issue is that DeepEqual compares the entire Time struct -  seconds, nanoseconds, and the location pointers. t1 uses the zero value for Time, so the Location pointer is nil, and that is interpreted to mean UTC. 

You then make a string outta that and parse it back into a *new* time value, but this one *does* have the Location set - Parse is smart enough to figure that one out. 

Solutions:
1. Explicitly set the Location
2. Don't use DeepEqual

rif

unread,
Jul 15, 2013, 1:49:57 PM7/15/13
to golan...@googlegroups.com
Thanks a lot. It is clear now (albeit still somehow not intuitive).

GreatOdinsRaven

unread,
Jul 15, 2013, 3:13:42 PM7/15/13
to golan...@googlegroups.com


On Monday, July 15, 2013 11:49:57 AM UTC-6, rif wrote:
Thanks a lot. It is clear now (albeit still somehow not intuitive)

Agree about the non-intuitive part. I'd really like for UTC to be the nil Location value and *only* the nil value, vs the current situation where it's either nil or explicit. 
Reply all
Reply to author
Forward
0 new messages