I have a time.Time
created using time.Date()
. I then calculate the number of nanoseconds between 1970/1/1 00:00:00.000000000
and that time.
I then take the nanoseconds and turn them back into a time.Time
using time.Unix()
.
However, if I compare the reconstituted time with the original using ==
, it returns false. If I subtract these 2 times, the resulting duration is 0. If I compare these 2 times using time.Equal()
, it returns true.
If I create another time using time.Date()
with the same values as the first time, using ==
to compare this new time and the original time results in a true.
This is the code that demonstrates this (Golang Playground):
package main
import (
"fmt"
"time"
)
func main() {
t1 := time.Date(2016, 4, 14, 1, 30, 30, 222000000, time.UTC)
base := time.Date(1970, 1, 1, 0, 0, 0, 0, t1.Location())
nsFrom1970 :=t1.Sub(base).Nanoseconds() // Calculate the number of nanoseconds from 1970/1/1 to t1
t2 := time.Unix(0, nsFrom1970)
fmt.Println(t1)
fmt.Println(t2)
fmt.Println(t1.Sub(t2)) // 0
fmt.Println(t1 == t2) //false
fmt.Println(t1.Equal(t2)) //true
t3 := time.Date(2100, 2, 1, 21, 21, 21, 222000000, time.UTC)
fmt.Println(t1 == t3) //true
}
Why does the reconstituted time return false when compared with the original time?