博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
golang中的time包
阅读量:2145 次
发布时间:2019-04-30

本文共 4773 字,大约阅读时间需要 15 分钟。

先来理解两个时钟

  • wall clock

  • monotonic clock

下面引用time包的介绍

Operating systems provide both a “wall clock,” which is subject to changes for clock synchronization, and a “monotonic clock,” which is not. The general rule is that the wall clock is for telling time and the monotonic clock is for measuring time. Rather than split the API, in this package the Time returned by time.Now contains both a wall clock reading and a monotonic clock reading; later time-telling operations use the wall clock reading, but later time-measuring operations, specifically comparisons and subtractions, use the monotonic clock reading.

wall clock 简单点说就是来描述时间、告知时间、输出时间的,本身就代表一个时间点

monotonic clock 用来度量时间,比如:计算时间差、比较两个时间等

借用另一篇文章的描述

Wall clock(time)就是我们一般意义上的时间,就像墙上钟所指示的时间。

Monotonic clock(time)字面意思是单调时间,实际上它指的是从某个点开始后(比如系统启动以后)流逝的时间,jiffies一定是单调递增的!

看个示例代码

package mainimport (	"fmt"	"time")func main() {	t := time.Now() // wall clock	fmt.Printf("Format Y-m-d h:M:S %s\n", t.Format("2006-01-02 15:04:05"))	time.Sleep(time.Duration(10000))	fmt.Printf("Time elapsed %d nanoseconds\n", time.Now().Sub(t)) // monotonic clock	const shortForm = "2006-01-02 15:04:05" // go setting format,必须是这个form才能解析???	t1, _ := time.ParseInLocation(shortForm, "2021-04-12 04:41:21", time.Local)	fmt.Printf("%d", t1.Unix())	//时间转换的模板,golang里面只能是 "2006-01-02 15:04:05" (go的诞生时间)	timeTemplate1 := "2006-01-02 15:04:05"	timeTemplate2 := "2006-01-02"	timeTemplate3 := "15:04:05"	t2 := int64(1618173681) // Unix()需要int64参数	fmt.Println(time.Unix(t2, 0).Format(timeTemplate1))	fmt.Println(time.Unix(t2, 0).Format(timeTemplate2))	fmt.Println(time.Unix(t2, 0).Format(timeTemplate3))}

使用time的几种操作

  • 时间字符串转时间戳

  • 时间戳转时间字符串

  • 计算时间差

  • 时间的比较

时间字符串转时间戳

引用上面的小示例

const shortForm = "2006-01-02 15:04:05" // go setting format,必须是这个form才能解析???	t1, _ := time.ParseInLocation(shortForm, "2021-04-12 04:41:21", time.Local)	fmt.Printf("%d", t1.Unix())

go提供一种方法来识别你提供的时间格式,不过格式里面的时间必须是go的诞生时间

时间戳转时间字符串

引用小示例

//时间转换的模板,golang里面只能是 "2006-01-02 15:04:05" (go的诞生时间)	timeTemplate1 := "2006-01-02 15:04:05"	timeTemplate2 := "2006-01-02"	timeTemplate3 := "15:04:05"	t2 := int64(1618173681) // Unix()需要int64参数	fmt.Println(time.Unix(t2, 0).Format(timeTemplate1))	fmt.Println(time.Unix(t2, 0).Format(timeTemplate2))	fmt.Println(time.Unix(t2, 0).Format(timeTemplate3))

只要时间格式写对了,转化还是很方便的,注意时间戳要转换为int64

计算时间差

直接看示例

package mainimport (	"fmt"	"time")func main() {	now := time.Now()	// ParseDuration parses a duration string.	// A duration string is a possibly signed sequence of decimal numbers,	// each with optional fraction and a unit suffix,	// such as "300ms", "-1.5h" or "2h45m".	//  Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".	timeTemplate := "2006-01-02 15:04:05"	// 10分钟前	fmt.Printf("Now is %s\n", now.Format(timeTemplate))	m, _ := time.ParseDuration("-1m")	m1 := now.Add(m)	fmt.Printf("10minutes ago is %s\n", m1.Format(timeTemplate))	fmt.Print("\n")	// 8个小时前	fmt.Printf("Now is %s\n", now.Format(timeTemplate))	h, _ := time.ParseDuration("-1h")	h1 := now.Add(8 * h)	fmt.Printf("8 hours ago is %s\n", h1.Format(timeTemplate))	fmt.Print("\n")	// 一天前	fmt.Printf("Now is %s\n", now.Format(timeTemplate))	d, _ := time.ParseDuration("-24h")	d1 := now.Add(d)	fmt.Printf("1 day ago is %s\n", d1.Format(timeTemplate))	fmt.Print("\n")	// 10分钟后	fmt.Printf("Now is %s\n", now.Format(timeTemplate))	mm, _ := time.ParseDuration("10m")	mm1 := now.Add(mm)	fmt.Printf("after 10 minutes is %s\n", mm1.Format(timeTemplate))	fmt.Print("\n")	// 8小时后	fmt.Printf("Now is %s\n", now.Format(timeTemplate))	hh, _ := time.ParseDuration("8h")	hh1 := now.Add(hh)	fmt.Printf("after 8 hours is %s\n", hh1.Format(timeTemplate))	fmt.Print("\n")	// 一天后	fmt.Printf("Now is %s\n", now.Format(timeTemplate))	dd, _ := time.ParseDuration("24h")	dd1 := now.Add(dd)	fmt.Printf("after 1 day is %s\n", dd1.Format(timeTemplate))	fmt.Print("\n")	// Sub 计算两个时间差	subM := now.Sub(mm1)	fmt.Printf("sub 10 minutes duration equal %s\n", subM.String())	subH := now.Sub(hh1)	fmt.Printf("sub 8 hours duration equal %s\n", subH.String())	subD := now.Sub(dd1)	fmt.Printf("sub 1 day duration equal %s\n", subD.String())	fmt.Printf("current time sub 1 day is %s\n", now.Add(subD).Format(timeTemplate))}

看下输出内容

Now is 2021-06-03 11:38:1810minutes ago is 2021-06-03 11:37:18Now is 2021-06-03 11:38:188 hours ago is 2021-06-03 03:38:18Now is 2021-06-03 11:38:181 day ago is 2021-06-02 11:38:18Now is 2021-06-03 11:38:18after 10 minutes is 2021-06-03 11:48:18Now is 2021-06-03 11:38:18after 8 hours is 2021-06-03 19:38:18Now is 2021-06-03 11:38:18after 1 day is 2021-06-04 11:38:18sub 10 minutes duration equal -10m0ssub 8 hours duration equal -8h0m0ssub 1 day duration equal -24h0m0scurrent time sub 1 day is 2021-06-02 11:38:18

parseDuration操作就是直接在当前时间上做修改,而Sub操作就是产生一个duration,提供给后面的时间操作使用

引用:

转载地址:http://mzrgf.baihongyu.com/

你可能感兴趣的文章
web.xml配置监听器,加载数据库信息配置文件ServletContextListener
查看>>
结构型模式之桥接模式(Bridge)
查看>>
行为型模式之状态模式(State)
查看>>
行为型模式之策略模式(Strategy)
查看>>
行为型模式之模板方法模式(TemplateMethod)
查看>>
行为型模式之访问者模式(Visitor)
查看>>
大小端详解
查看>>
source insight使用方法简介
查看>>
<stdarg.h>头文件的使用
查看>>
C++/C 宏定义(define)中# ## 的含义 宏拼接
查看>>
Git安装配置
查看>>
linux中fork()函数详解
查看>>
C语言字符、字符串操作偏僻函数总结
查看>>
Git的Patch功能
查看>>
分析C语言的声明
查看>>
TCP为什么是三次握手,为什么不是两次或者四次 && TCP四次挥手
查看>>
C结构体、C++结构体、C++类的区别
查看>>
进程和线程的概念、区别和联系
查看>>
CMake 入门实战
查看>>
绑定CPU逻辑核心的利器——taskset
查看>>