I am expecting time.Now().In(location)
objects to compare as identical if they are for the same moment, even though the location
objects are different, as long as the location
objects are created using the same name
string.
Why doesn't that expectation hold?
package main
import (
"fmt"
"time"
"log"
)
func main() {
const USPacificTimeZone = "US/Pacific"
location, err := time.LoadLocation(USPacificTimeZone)
if err != nil {
log.Fatal(fmt.Sprintf("Couldn't load timezone: %v", err))
}
// Exactly the same as location above, just a new instance
location2, err := time.LoadLocation(USPacificTimeZone)
if err != nil {
log.Fatal(fmt.Sprintf("Couldn't load timezone: %v", err))
}
now := time.Now()
fmt.Printf("Times using same location object: %v\n",
now.In(location) == now.In(location))
// prints: Times using same location object: true
// Using (the identical) location2 causes the times to be different???
fmt.Printf("Times using different location objects: %v\n",
now.In(location) == now.In(location2))
// prints: Times using different location objects: false
}