golang中的time包

先来理解两个时钟

  • 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 main

import (
	"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 main

import (
	"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:18
10minutes ago is 2021-06-03 11:37:18

Now is 2021-06-03 11:38:18
8 hours ago is 2021-06-03 03:38:18

Now is 2021-06-03 11:38:18
1 day ago is 2021-06-02 11:38:18

Now is 2021-06-03 11:38:18
after 10 minutes is 2021-06-03 11:48:18

Now is 2021-06-03 11:38:18
after 8 hours is 2021-06-03 19:38:18

Now is 2021-06-03 11:38:18
after 1 day is 2021-06-04 11:38:18

sub 10 minutes duration equal -10m0s
sub 8 hours duration equal -8h0m0s
sub 1 day duration equal -24h0m0s
current time sub 1 day is 2021-06-02 11:38:18

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

引用:

https://pkg.go.dev/time#hdr-Monotonic_Clocks

https://www.cnblogs.com/nyist-xsk/p/11448348.html

https://taobig.org/?p=752

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值