服务器时区设置

231 阅读1分钟
//不论服务器部署在何地, 时间都是UTC+8对应的时间 
cstZone := time.FixedZone("CST", 3600*8) 
now := time.Now().In(cstZone) 
fmt.Println(now, now.Unix()) // 2022-11-04 11:47:34.419571 +0800 CST 1667533654 

//不论服务器部署在何地, 时间都是UTC对应的时间 
utcZone := time.FixedZone("UTC", 0) 
now = time.Now().In(utcZone) 
fmt.Println(now, now.Unix()) // 2022-11-04 03:47:34.419611 +0000 UTC 1667533654
//时间是UTC+8对应的时间 
var cstZone = time.FixedZone("CST", 8*3600) 
now := time.Now().In(cstZone) 
fmt.Println(now, now.Unix()) // 2022-11-04 14:13:18.964937 +0800 CST 1667542398 

//时间是UTC对应的时间 
utcZone, _ := time.LoadLocation("UTC") 
now = now.In(utcZone)
fmt.Println(now, now.Unix()) // 2022-11-04 06:13:18.964937 +0000 UTC 1667542398 

//时间是UTC+8对应的时间 
cstZone2, _ := time.LoadLocation("Asia/Shanghai") 
now = now.In(cstZone2) 
fmt.Println(now, now.Unix()) // 2022-11-04 14:13:18.964937 +0800 CST 1667542398