2

Golang时区设置

 1 month ago
source link: https://studygolang.com/articles/13018?fr=sidebar
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Golang时区设置

hell0cat · 2018-05-23 16:33:07 · 29589 次点击 · 预计阅读时间 1 分钟 · 大约8小时之前 开始浏览    
这是一个创建于 2018-05-23 16:33:07 的文章,其中的信息可能已经有所发展或是发生改变。

在部署PHP项目的时候,在php的入口文件添加一行:

date_default_timezone_set('PRC');

时间格式化输出的时区为东八区北京时间,无需关系系统所在时区。

在Go语言上,go语言的time.Now()返回的是当地时区时间,直接用:

time.Now().Format("2006-01-02 15:04:05")

输出的是当地时区时间。

go语言并没有全局设置时区这么一个东西,每次输出时间都需要调用一个In()函数改变时区:

var cstSh, _ = time.LoadLocation("Asia/Shanghai") //上海
fmt.Println("SH : ", time.Now().In(cstSh).Format("2006-01-02 15:04:05"))

在windows系统上,没有安装go语言环境的情况下,time.LoadLocation会加载失败。

var cstZone = time.FixedZone("CST", 8*3600)       // 东八
fmt.Println("SH : ", time.Now().In(cstZone).Format("2006-01-02 15:04:05"))

最好的办法是用time.FixedZone


有疑问加站长微信联系(非本文作者)

280

本文来自:开源中国博客

感谢作者:hell0cat

查看原文:Golang时区设置

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK