golang容器部署时区报错 missing Location in call to Date

问题:

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)
}

总结:在网上找了很多方法,修改容器时区也没解决,最后还是排查找了很多资料解决的

posted @ 2024-02-08 11:16  努力乄小白  阅读(97)  评论(0编辑  收藏  举报