Go语言基础之time包

时间类型

time.Time类型表示时间。我们可以通过time.Now()函数获取当前的时间对象,然后获取时间对象的年月日时分秒等信息。示例代码如下:

func timeDemo() {
	now := time.Now() //获取当前时间
	fmt.Printf("current time:%v\n", now)

	year := now.Year()     //年
	month := now.Month()   //月
	day := now.Day()       //日
	hour := now.Hour()     //小时
	minute := now.Minute() //分钟
	second := now.Second() //秒
	fmt.Printf("%d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second)
}

时间戳

时间戳是自1970年1月1日(08:00:00GMT)至当前时间的总毫秒数。它也被称为Unix时间戳(UnixTimestamp)。

基于时间对象获取时间戳的示例代码如下:

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()       //返回值now是time.Time类型,time.Time是个时间结构体
	timestamp := now.Unix() //time.Time类型转换成时间戳
	fmt.Printf("now:%v\ntimestamp:%v\n", now, timestamp)
	//使用time.Unix()函数可以将时间戳转为时间格式。
	ns := now.Nanosecond()//获取当前时间的ns数
	time0 := time.Unix(timestamp, int64(ns))//使用now获得的时间戳和now的纳秒数
	//去将时间戳转换成Time类型,其中第二个参数为0,表示忽略ns数,精确到秒
	fmt.Printf("time0:%v\n", time0)
}

now:2020-03-03 12:22:28.2477751 +0800 CST m=+0.013010801
timestamp:1583209348
time0:2020-03-03 12:22:28.2477751 +0800 CST

时间间隔

time.Duration是time包定义的一个类型,它代表两个时间点之间经过的时间,以纳秒为单位。time.Duration表示一段时间间隔,可表示的最长时间段大约290年。

time包中定义的时间间隔类型的常量如下:

const (
    Nanosecond  Duration = 1
    Microsecond          = 1000 * Nanosecond
    Millisecond          = 1000 * Microsecond
    Second               = 1000 * Millisecond
    Minute               = 60 * Second
    Hour                 = 60 * Minute
)

例如:time.Duration表示1纳秒,time.Second表示1秒。

时间操作

Add
我们在日常的编码过程中可能会遇到要求时间+时间间隔的需求,Go语言的时间对象有提供Add方法如下:

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now() //返回值now是time.Time类型,time.Time是个时间结构体
	fmt.Printf("now:%v\n", now)
	oneHourLate := now.Add(time.Hour) //求一个小时以后的时间,返回值是time.Time
	fmt.Printf("oneHourLate:%v\n", oneHourLate)
}

now:2020-03-03 12:29:34.7310868 +0800 CST m=+0.012992601
oneHourLate:2020-03-03 13:29:34.7310868 +0800 CST m=+3600.012992601

Sub
求两个时间之间的差值:

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now() //返回值now是time.Time类型,time.Time是个时间结构体
	oneHourLate := now.Add(time.Hour) //求一个小时以后的时间,返回值是time.Time
	howlong := oneHourLate.Sub(now) //time.Time调用Sub方法,参数也是一个time.Time,
	//返回值:oneHourLate-now是一个Duration类型,也就是int64
	fmt.Printf("Type:%T	howlong:%v\n", howlong, howlong)
}

Type:time.Duration howlong:1h0m0s

Equal
判断两个时间是否相同,会考虑时区的影响,因此不同时区标准的时间也可以正确比较。本方法和用t==u不同,这种方法还会比较地点和时区信息。

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()       //返回值now是time.Time类型,time.Time是个时间结构体
	timestamp := now.Unix() //time.Time类型转换成时间戳
	//使用time.Unix()函数可以将时间戳转为时间格式。
	ns := now.Nanosecond()
	time0 := time.Unix(timestamp, int64(ns))

	if time0.Equal(now) {
		fmt.Println("now和time0是同一时间")
	} else {
		fmt.Println("now和time0不是同一时间")
	}
}

now和time0是同一时间

After和Before

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()              //返回值now是time.Time类型,time.Time是个时间结构体
	oneHourLate := now.Add(time.Hour) //求一个小时以后的时间,返回值是time.Time
	if oneHourLate.After(now) {
		fmt.Println("oneHourLate在now之后")
	} else {
		fmt.Println("oneHourLate在now之前")
	}
	if now.Before(oneHourLate) {
		fmt.Println("now在oneHourLate之前")
	} else {
		fmt.Println("now在oneHourLate之后")
	}
}

oneHourLate在now之后
now在oneHourLate之前

定时器

使用time.Tick(时间间隔)来设置定时器,定时器的本质上是一个通道(channel)。

package main

import (
	"fmt"
	"time"
)

func tickDemo() {
	ticker := time.Tick(time.Second) //定义一个1秒间隔的定时器
	for i := range ticker {
		fmt.Println(i) //每秒都会执行的任务
	}
}
func main() {
	tickDemo()
}

时间格式化

Go的诞生时间2006年1月2号15点04分(记忆口诀为2006 1 2 3 4)。
补充:如果想格式化为12小时方式,需指定PM、AM。

package main

import (
	"fmt"
	"time"
)

func formatDemo() {
	now := time.Now()
	// 格式化的模板为Go的出生时间2006年1月2号15点04分 Mon Jan
	// 24小时制
	fmt.Println(now.Format("2006-01-02 15:04:05.000 Mon Jan"))
	// 12小时制
	fmt.Println(now.Format("2006-01-02 03:04:05.000 PM Mon Jan"))
	fmt.Println(now.Format("2006/01/02 15:04"))
	fmt.Println(now.Format("15:04 2006/01/02"))
	fmt.Println(now.Format("2006/01/02"))
	fmt.Println(now.Format("2006,01,02,15,04,000"))
}
func main() {
	formatDemo()
}

2020-03-03 12:57:22.890 Tue Mar
2020-03-03 12:57:22.890 PM Tue Mar
2020/03/03 12:57
12:57 2020/03/03
2020/03/03
2020,03,03,12,57,000

解析字符串格式的时间

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()
	fmt.Println(now)
	// 加载时区
	loc, err := time.LoadLocation("Asia/Shanghai")
	if err != nil {
		fmt.Println(err)
		return
	}
	// 按照指定时区和指定格式解析字符串时间
	timeObj, err := time.ParseInLocation("2006,01,02 15:04:05", "2020,03,03 10:15:20", loc)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(timeObj)
	fmt.Println(now.Sub(timeObj))
}

2020-03-03 13:13:36.6856889 +0800 CST m=+0.016990901
2020-03-03 10:15:20 +0800 CST
2h58m16.6856889s

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值