时间类型
时间类型:time.Time
我们可以通过time
包中的Now
函数获取当前本地时间,然后使用时间类型的方法得到当前本地时间的年、月、日等信息。
Now()
函数格式:
// // Now returns the current local time.
func Now() Time
示例:
now := time.Now()
fmt.Println(now) // 2020-08-08 13:25:18.5419281 +0800 CST m=+0.001996701
fmt.Println("年:", now.Year()) // 年: 2020
fmt.Println("月:", now.Month()) // 月: August
fmt.Println("日:", now.Day()) // 日: 8
fmt.Println("小时:", now.Hour()) // 小时: 13
fmt.Println("分钟:", now.Minute()) // 分钟: 25
fmt.Println("秒:", now.Second()) // 秒: 18
fmt.Println("纳秒:", now.Nanosecond()) // 纳秒: 541928100
时间类型还有其它方法:
Date()
:返回年月日
Clock()
:返回时分秒
更多方法函数参考包文档中文版
时间戳
时间戳是自1970年1月1日(08:00:00GMT)至当前时间的总毫秒数。它也被称为Unix
时间戳(UnixTimestamp)。
获取时间戳:
now := time.Now()
timestamp1 := now.Unix() //时间戳
timestamp2 := now.UnixNano() //纳秒时间戳
fmt.Println(timestamp1, "秒") // 1596866180 秒
fmt.Println(timestamp2, "纳秒") // 1596866180018784800 纳秒
time
包中的Unix()
函数可以将时间戳转换为时间格式:
timed := time.Unix(timestamp1, 0)
fmt.Println("年:", timed.Year()) // 年: 2020
fmt.Println("月:", timed.Month()) // 月: August
fmt.Println("日:", timed.Day()) // 日: 8
fmt.Println("小时:", timed.Hour()) // 小时: 14
fmt.Println("分钟:", timed.Minute()) // 分钟: 7
fmt.Println("秒:", timed.Second()) // 秒: 47
fmt.Println("纳秒:", timed.Nanosecond()) // 纳秒: 0
其中Unix()
函数的格式是:
func Unix(sec int64, nsec int64) Time
其接收秒数和纳秒数,返回值是时间类型。
时间间隔
time.Duration
是time包
定义的一个类型(type Duration int64
),它代表两个时间点之间经过的时间,以纳秒为单位。
time
包中定义了一些Duration
类型的常量:
const (
Nanosecond Duration = 1
Microsecond = 1000 * Nanosecond
Millisecond = 1000 * Microsecond
Second = 1000 * Millisecond
Minute = 60 * Second
Hour = 60 * Minute
)
使用示例:
fmt.Println(time.Second) // 1s
fmt.Println(time.Microsecond) // 1µs
时间操作
Add()方法
给当前时间增加一定的时间间隔,得到新的时间。如果传入的时间间隔是负的,则可以实现当前时间减去一定的时间间隔。
格式:
// Add returns the time t+d.
func (t Time) Add(d Duration) Time
示例:
now := time.Now()
fmt.Println(now) // 2020-08-08 14:34:46.3158009 +0800 CST m=+0.001981901
later := now.Add(3*time.Hour + time.Second) // 当前时间增加3小时和1秒钟
fmt.Println(later) // 2020-08-08 17:34:47.3158009 +0800 CST m=+10801.001981901
Sub()方法
返回两个时间的时间间隔。如果结果超出了时间间隔的范围,返回边界值。
格式:
// Sub returns the duration t-u.
func (t Time) Sub(u Time) Duration
示例(接上例):
duration := later.Sub(now)
fmt.Println(duration) // 3h0m1s
Equal()方法
判断两个时间是否相等,考虑时区的影响,例如,6:00 +0200和4:00 UTC相等。使用==
比较两个时间不会考虑时区;大部分代码都会使用Equal()
而不是==
。
格式:
// Equal reports whether t and u represent the same time instant.
func (t Time) Equal(u Time) bool
Before()和After()方法
判断时间先后。
格式:
// Before reports whether the time instant t is before u.
func (t Time) Before(u Time) bool
// After reports whether the time instant t is after u.
func (t Time) After(u Time) bool
示例(接上例):
fmt.Println(later.Before(now)) // false
fmt.Println(later.After(now)) // true
定时器
使用time
包中的Tick()
函数来设置定时器,定时器的本质上是一个通道(channel)。
Tick()
函数格式:
func Tick(d Duration) <-chan Time
示例:
ticker := time.Tick(time.Second) //定义一个1秒间隔的定时器
for t := range ticker {
fmt.Println(t) //每秒都会执行的任务
}
一定时间后部分输出:
2020-08-08 15:00:15.9648025 +0800 CST m=+1.012436201
2020-08-08 15:00:16.9658258 +0800 CST m=+2.013459501
2020-08-08 15:00:17.9650674 +0800 CST m=+3.012701101
2020-08-08 15:00:18.9658949 +0800 CST m=+4.013528601
2020-08-08 15:00:19.9660939 +0800 CST m=+5.013727601
2020-08-08 15:00:20.9649029 +0800 CST m=+6.012536601
2020-08-08 15:00:21.9653321 +0800 CST m=+7.012965801
2020-08-08 15:00:22.9648979 +0800 CST m=+8.012531601
2020-08-08 15:00:23.9646915 +0800 CST m=+9.012325201
时间格式化
时间类型有一个自带的方法Format()
进行格式化,需要注意的是Go语言中格式化时间模板不是常见的Y-m-d H:M:S
而是使用Go的诞生时间2006年1月2号15点04分05秒。
示例:
now := time.Now()
// 24小时制
fmt.Println(now.Format("2006-01-02 15:04:05.000 Mon Jan")) // 2020-08-08 15:34:23.164 Sat Aug
fmt.Println(now.Format("Mon Jan 2006-01-02 15:04:05")) // Sat Aug 2020-08-08 15:34:23
// 12小时制,注意需要指定 PM
fmt.Println(now.Format("2006-01-02 03:04:05.000 PM")) // 2020-08-08 03:34:23.164 PM
fmt.Println(now.Format("2006/01/02 15:04")) // 2020/08/08 15:34
fmt.Println(now.Format("15:04 2006/01/02")) // 15:34 2020/08/08
fmt.Println(now.Format("2006/01/02")) // 2020/08/08
解析格式化的时间
time
包有两个函数可以解析字符串格式的时间:
func Parse(layout, value string) (Time, error)
func ParseInLocation(layout, value string, loc *Location) (Time, error)
Parse()
示例:
const longForm = "Jan 2, 2006 at 3:04pm (MST)"
t, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)")
fmt.Println(t) // 2013-02-03 19:54:00 -0800 PST
const shortForm = "2006-Jan-02"
t, _ = time.Parse(shortForm, "2013-Feb-03")
fmt.Println(t) // 2013-02-03 00:00:00 +0000 UTC
对于Parse()
函数,如果参数value
带有时区信息,那么以value
中的时区信息为准,否则默认时区是UTC
。
ParseInLocation()
示例:
loc, _ := time.LoadLocation("Europe/Berlin") // 加载时区
const longForm = "Jan 2, 2006 at 3:04pm (MST)"
t, _ := time.ParseInLocation(longForm, "Jul 9, 2012 at 5:02am (CEST)", loc)
fmt.Println(t) // 2012-07-09 05:02:00 +0200 CEST
const shortForm = "2006-Jan-02"
t, _ = time.ParseInLocation(shortForm, "2012-Jul-09", loc)
fmt.Println(t) // 2012-07-09 00:00:00 +0200 CEST
对于ParseInLocation()
函数,时区以参数loc
为准。