问题:
const timezone = "Asia/Shanghai" func TimeFormat(date time.Time, pattern string) string { location, err := time.LoadLocation(timezone) date.In(location) return date.Format(pattern) }
1.在本地开发使用了时区是没有问题的,但是部署到服务器上面就会报错,因为容器镜像默认是UTC时区
解决方法:
const timezone = "Asia/Shanghai" func TimeFormat(date time.Time, pattern string) string { location, err := time.LoadLocation(timezone) if err != nil { location = time.FixedZone("CST", 8*3600) //替换上海时区方式 } date.In(location) return date.Format(pattern) }
总结:在网上找了很多方法,修改容器时区也没解决,最后还是排查找了很多资料解决的
标签:容器,string,pattern,golang,报错,location,time,date,timezone From: https://www.cnblogs.com/fuyuteng/p/18011678