加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Go-内置time包

发布时间:2020-12-16 09:19:57 所属栏目:大数据 来源:网络整理
导读:一.导入包 import "time" 二.转换成Time对象 获取当前时间:time. Now () 自定义时间:time. Date(year int,month Month,day,hour,min,sec,nsec int,loc *Location) 三.Time对象的方法 年月日周 Date() : 返回时间的日期信息 返回值int类型 Year() : 返回年 返

一.导入包

import "time"

二.转换成Time对象

  • 获取当前时间:time. Now ()
  • 自定义时间:time. Date(year int,month Month,day,hour,min,sec,nsec int,loc *Location)

三.Time对象的方法

年月日周

  • Date() : 返回时间的日期信息 返回值int类型
  • Year() : 返回年 返回值int类型
  • Month () : 返回月 返回值Month类型
  • Day () : 返回日 返回值int类型
  • YearDay() : 一年中对应的天 返回值int类型

年,星期范围编号

  • ISOWeek () : 返回年,星期范围编号int类型

时分秒

  • Clock () : 返回 时分秒 返回值int类型

  • Hour () : 返回时返回值int类型

  • Minute () : 返回分 返回值int类型

  • Second () : 返回秒 返回值int类型

  • Nanosecond () : 返回纳秒 返回值int类型

时间戳

  • Unix() :时间戳 返回值 int64
  • UnixNano() :时间戳(纳秒) 返回值 int64

时区

  • Location ():时区返回值 *Location
  • Zone() :时区偏差 返回值string+int

四.时间序列化和返序列化以及比较与计算

参考https://www.jianshu.com/p/9d5636d34f17

列化和返序列化

func (t Time) MarshalBinary() ([]byte,error) {} // 时间序列化

func (t Time) UnmarshalBinary(data []byte) error {} // 反序列化

func (t Time) MarshalJSON() ([]byte,error) {} // 时间序列化

func (t Time) MarshalText() ([]byte,error) {} // 时间序列化

func (t Time) GobEncode() ([]byte,error) {} // 时间序列化

func (t Time) GobDecode() ([]byte,error) {} // 时间序列化

比较与计算

func (t Time) IsZero() bool {} // 是否是零时时间

func (t Time) After(u Time) bool {} // 时间在u 之前

func (t Time) Before(u Time) bool {} // 时间在u 之后

func (t Time) Equal(u Time) bool {} // 时间与u 相同

func (t Time) Add(d Duration) Time {} // 返回t +d 的时间点

func (t Time) Sub(u Time) Duration {} // 返回 t-u

func (t Time) AddDate(years int,months int,days int) Time {} 返回增加了给出的年份、月份和天

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读