golang之time

【自取】最近整理的,有需要可以领取学习:

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(单调时间,用于比较,测量时间)。time.Now()返回两种时间(clock和monotonic)分别用于显示和测量。

二、TIME

type Time struct{} // A Time represents an instant in time with nanosecond precision.

Time代表一个纳秒精度的时间点。

A Time represents an instant in time with nanosecond precision.

Programs using times should typically store and pass them as values, not pointers. That is, time variables and struct fields should be of type time.Time, not *time.Time.

Time零值代表时间点January 1, year 1, 00:00:00.000000000 UTC。

now := time.Now() time.Now().Day() time.Now().Minute() time.Now().Month()

type Duration int64 // 表示一段时间间隔,单位纳秒,可表示的最长时间大约290年。

type Location struct{}

type Month int

t := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)

Date panics if loc is nil.

func Now() Time

func (t Time) Unix() int64 //Unix returns t as a Unix time, the number of seconds elapsed since January 1, 1970 UTC.

三、Ticker定时器

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

A Ticker holds a channel that delivers “ticks” of a clock at intervals.

四、Timer

The Timer type represents a single event. When the Timer expires, the current time will be sent on C, unless the Timer was created by AfterFunc. A Timer must be created with NewTimer or AfterFunc.

参考:

Original: https://www.cnblogs.com/embedded-linux/p/13669343.html
Author: yuxi_o
Title: golang之time

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/8688/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

发表回复

登录后才能评论
免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部