Go 18.0 专业五


time

<h1>1. Time</h1> <p>时间和日期是我们编程中经常会用到的,本文主要介绍了Go语言内置的time包的基本用法。</p> <h3>1.1.1. time包</h3> <p>time包提供了时间的显示和测量用的函数。日历的计算采用的是公历。</p> <h3>1.1.2. 时间类型</h3> <p>time.Time类型表示时间。我们可以通过time.Now()函数获取当前的时间对象,然后获取时间对象的年月日时分秒等信息。示例代码如下:</p> <pre><code>func timeDemo() { now := time.Now() //获取当前时间 fmt.Printf(&amp;quot;current time:%v\n&amp;quot;, now) year := now.Year() //年 month := now.Month() //月 day := now.Day() //日 hour := now.Hour() //小时 minute := now.Minute() //分钟 second := now.Second() //秒 fmt.Printf(&amp;quot;%d-%02d-%02d %02d:%02d:%02d\n&amp;quot;, year, month, day, hour, minute, second) }</code></pre> <h3>1.1.3. 时间戳</h3> <p>时间戳是自1970年1月1日(08:00:00GMT)至当前时间的总毫秒数。它也被称为Unix时间戳(UnixTimestamp)。</p> <p>基于时间对象获取时间戳的示例代码如下:</p> <pre><code>func timestampDemo() { now := time.Now() //获取当前时间 timestamp1 := now.Unix() //时间戳 timestamp2 := now.UnixNano() //纳秒时间戳 fmt.Printf(&amp;quot;current timestamp1:%v\n&amp;quot;, timestamp1) fmt.Printf(&amp;quot;current timestamp2:%v\n&amp;quot;, timestamp2) }</code></pre> <p>使用time.Unix()函数可以将时间戳转为时间格式。</p> <pre><code>func timestampDemo2(timestamp int64) { timeObj := time.Unix(timestamp, 0) //将时间戳转为时间格式 fmt.Println(timeObj) year := timeObj.Year() //年 month := timeObj.Month() //月 day := timeObj.Day() //日 hour := timeObj.Hour() //小时 minute := timeObj.Minute() //分钟 second := timeObj.Second() //秒 fmt.Printf(&amp;quot;%d-%02d-%02d %02d:%02d:%02d\n&amp;quot;, year, month, day, hour, minute, second) }</code></pre> <h3>1.1.4. 时间间隔</h3> <p>time.Duration是time包定义的一个类型,它代表两个时间点之间经过的时间,以纳秒为单位。time.Duration表示一段时间间隔,可表示的最长时间段大约290年。</p> <p>time包中定义的时间间隔类型的常量如下:</p> <pre><code>const ( Nanosecond Duration = 1 Microsecond = 1000 * Nanosecond Millisecond = 1000 * Microsecond Second = 1000 * Millisecond Minute = 60 * Second Hour = 60 * Minute )</code></pre> <p>例如:time.Duration表示1纳秒,time.Second表示1秒。</p> <h3>1.1.5. 时间操作</h3> <h4>Add</h4> <p>我们在日常的编码过程中可能会遇到要求时间+时间间隔的需求,Go语言的时间对象有提供Add方法如下:</p> <pre><code> func (t Time) Add(d Duration) Time</code></pre> <p>举个例子,求一个小时之后的时间:</p> <pre><code>func main() { now := time.Now() later := now.Add(time.Hour) // 当前时间加1小时后的时间 fmt.Println(later) }</code></pre> <h4>Sub</h4> <p>求两个时间之间的差值:</p> <pre><code> func (t Time) Sub(u Time) Duration</code></pre> <p>返回一个时间段t-u。如果结果超出了Duration可以表示的最大值/最小值,将返回最大值/最小值。要获取时间点t-d(d为Duration),可以使用t.Add(-d)。</p> <h4>Equal</h4> <pre><code> func (t Time) Equal(u Time) bool</code></pre> <p>判断两个时间是否相同,会考虑时区的影响,因此不同时区标准的时间也可以正确比较。本方法和用t==u不同,这种方法还会比较地点和时区信息。</p> <h4>Before</h4> <pre><code> func (t Time) Before(u Time) bool</code></pre> <p>如果t代表的时间点在u之前,返回真;否则返回假。</p> <h4>After</h4> <pre><code> func (t Time) After(u Time) bool</code></pre> <p>如果t代表的时间点在u之后,返回真;否则返回假。</p> <h3>1.1.6. 定时器</h3> <p>使用time.Tick(时间间隔)来设置定时器,定时器的本质上是一个通道(channel)。</p> <pre><code>func tickDemo() { ticker := time.Tick(time.Second) //定义一个1秒间隔的定时器 for i := range ticker { fmt.Println(i)//每秒都会执行的任务 } }</code></pre> <h3>1.1.7. 时间格式化</h3> <p>时间类型有一个自带的方法Format进行格式化,需要注意的是Go语言中格式化时间模板不是常见的Y-m-d H:M:S而是使用Go的诞生时间2006年1月2号15点04分(记忆口诀为2006 1 2 3 4)。也许这就是技术人员的浪漫吧。</p> <p>补充:如果想格式化为12小时方式,需指定PM。</p> <pre><code>func formatDemo() { now := time.Now() // 格式化的模板为Go的出生时间2006年1月2号15点04分 Mon Jan // 24小时制 fmt.Println(now.Format(&amp;quot;2006-01-02 15:04:05.000 Mon Jan&amp;quot;)) // 12小时制 fmt.Println(now.Format(&amp;quot;2006-01-02 03:04:05.000 PM Mon Jan&amp;quot;)) fmt.Println(now.Format(&amp;quot;2006/01/02 15:04&amp;quot;)) fmt.Println(now.Format(&amp;quot;15:04 2006/01/02&amp;quot;)) fmt.Println(now.Format(&amp;quot;2006/01/02&amp;quot;)) }</code></pre> <h4>解析字符串格式的时间</h4> <pre><code>now := time.Now() fmt.Println(now) // 加载时区 loc, err := time.LoadLocation(&amp;quot;Asia/Shanghai&amp;quot;) if err != nil { fmt.Println(err) return } // 按照指定时区和指定格式解析字符串时间 timeObj, err := time.ParseInLocation(&amp;quot;2006/01/02 15:04:05&amp;quot;, &amp;quot;2019/08/04 14:15:20&amp;quot;, loc) if err != nil { fmt.Println(err) return } fmt.Println(timeObj) fmt.Println(timeObj.Sub(now))</code></pre>

页面列表

ITEM_HTML