我希望time.Now().In(location)对象在同一时刻比较为相同,即使location对象不同,只要location对象是使用相同的name字符串创建的。
为什么这种期望不成立?
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
}
阿晨1998
相关分类