go时间解析

1350368559 · · 317 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

标准时间格式字符串转为time类型

https://pkg.go.dev/time?utm_source=gopls#Parse
func time.Parse(layout string, value string) (time.Time, error)
其中layout的时间必须是"2006-01-02 15:04:05"这个时间

vmcoreTime="2021-04-07 07:45:22"
time, _ := time.Parse("2006-01-02 15:04:05", vmcoreTime)
fmt.Println(time)
fmt.Println(reflect.TypeOf(time))

输出:
time.Time
2021-04-07 07:45:22 +0000 UTC

把系统reboot的时间转为标准时间输出

https://developer.aliyun.com/article/132070
last reboot -F -w -x -i | grep reboot
reboot system boot 0.0.0.0 Wed Apr 7 07:47:52 2021

https://golang.org/src/time/format.go
const (<br/>ANSIC = "Mon Jan _2 15:04:05 2006"<br/>)
https://pkg.go.dev/time?utm_source=gopls#ParseInLocation
func time.ParseInLocation(layout string, value string, loc *time.Location) (time.Time, error)

fmt.Println(rebootTime)
location, _ := time.LoadLocation("Asia/Shanghai")
time, _ := time.ParseInLocation(time.ANSIC, rebootTime, location)
fmt.Println(time)

Wed Apr 7 07:47:52 2021
2021-04-07 07:47:52 +0800 CST


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

本文来自:51CTO博客

感谢作者:1350368559

查看原文:go时间解析

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

317 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传