分享
[Go - Basic] Time相关
Arboat · · 1004 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
import "time"
类型
type Time struct {
wall uint64
ext int64
loc *Location
}
type Month int
type Weekday int
type Duration int64
常用函数和方法
函数:
Now() Time 当前Time
Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time //返回一个设置的time类型
Since(t Time) Duration //time.Now().Sub(t)
Unix(sec int64, nsec int64) Time // 时间戳转时间 1sec = 1nsec * 1e6 , sec 10位时间戳
方法:
(t Time) Add(d Duration) Time // returns the time t+d.
(t Time) AddDate(years int, months int, days int) Time
(t Time) Sub(u Time) Duration //计算时间差
(t Time) Unix() int64 10位时间戳
(t Time) UnixNano() int64 16位时间戳
(t Time) Equal(u Time) bool // 比较两个time相等
(t Time) After(u Time) bool // reports whether the time instant t is after u.
(t Time) Before(u Time) bool // reports whether the time instant t is before u.
(t Time) IsZero() bool // reports whether t represents the zero time instant, January 1, year 1, 00:00:00 UTC.
(t Time) UTC() Time // returns t with the location set to UTC.
(t Time) Local() Time // returns t with the location set to local time.
(t Time) In(loc *Location) Time //设置为指定location
(t Time) Location() *Location // returns the time zone information associated with t.
(t Time) Zone() (name string, offset int) // name of the zone (such as "CET") and its offset in seconds east of UTC.
time和string转换
time -> string
(t Time) Format(layout string) string // layout :("2006年01月02日 15:04:05")
string -> time
Parse(layout, value string) (Time, error) // layout
获取本周周一零点时间戳(以周一为起始周)
timeNow := time.Now()
day := timeNow.Format("2006年01月02日 15:04:05")
offset := int(timeNow.Weekday() - time.Weekday(1))
if offset == -1 {
offset = 6
}
timeNowZeroStr := timeNow.Format("2006年01月02日")
t, _ := time.Parse("2006年01月02日", timeNowZeroStr)
thisMonday := t.AddDate(0, 0, -offset).Unix()
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信1004 次点击
下一篇:一次UDP通迅的问题排查
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
import "time"
类型
type Time struct {
wall uint64
ext int64
loc *Location
}
type Month int
type Weekday int
type Duration int64
常用函数和方法
函数:
Now() Time 当前Time
Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time //返回一个设置的time类型
Since(t Time) Duration //time.Now().Sub(t)
Unix(sec int64, nsec int64) Time // 时间戳转时间 1sec = 1nsec * 1e6 , sec 10位时间戳
方法:
(t Time) Add(d Duration) Time // returns the time t+d.
(t Time) AddDate(years int, months int, days int) Time
(t Time) Sub(u Time) Duration //计算时间差
(t Time) Unix() int64 10位时间戳
(t Time) UnixNano() int64 16位时间戳
(t Time) Equal(u Time) bool // 比较两个time相等
(t Time) After(u Time) bool // reports whether the time instant t is after u.
(t Time) Before(u Time) bool // reports whether the time instant t is before u.
(t Time) IsZero() bool // reports whether t represents the zero time instant, January 1, year 1, 00:00:00 UTC.
(t Time) UTC() Time // returns t with the location set to UTC.
(t Time) Local() Time // returns t with the location set to local time.
(t Time) In(loc *Location) Time //设置为指定location
(t Time) Location() *Location // returns the time zone information associated with t.
(t Time) Zone() (name string, offset int) // name of the zone (such as "CET") and its offset in seconds east of UTC.
time和string转换
time -> string
(t Time) Format(layout string) string // layout :("2006年01月02日 15:04:05")
string -> time
Parse(layout, value string) (Time, error) // layout
获取本周周一零点时间戳(以周一为起始周)
timeNow := time.Now()
day := timeNow.Format("2006年01月02日 15:04:05")
offset := int(timeNow.Weekday() - time.Weekday(1))
if offset == -1 {
offset = 6
}
timeNowZeroStr := timeNow.Format("2006年01月02日")
t, _ := time.Parse("2006年01月02日", timeNowZeroStr)
thisMonday := t.AddDate(0, 0, -offset).Unix()