學會 Go 中的時間處理

作爲程序員,我們經常需要對時間進行處理。在 Go 中,標準庫 time 提供了對應的能力。

本文將介紹 time 庫中一些重要的函數和方法,希望能幫助到那些一遇到 Go 時間處理問題就需要百度的童鞋。

應對時區問題

在編程中,我們經常會遭遇八小時時間差問題。這是由時區差異引起的,爲了能更好地解決它們,我們需要理解幾個時間定義標準。

GMT(Greenwich Mean Time),格林威治平時。GMT 根據地球的自轉和公轉來計算時間,它規定太陽每天經過位於英國倫敦郊區的皇家格林威治天文臺的時間爲中午 12 點。GMT 是前世界標準時。

UTC(Coordinated Universal Time),協調世界時。UTC 比 GMT 更精準,它根據原子鐘來計算時間。在不需要精確到秒的情況下,可以認爲 UTC=GMT。UTC 是現世界標準時。

從格林威治本初子午線起,往東爲正,往西爲負,全球共劃分爲 24 個標準時區,相鄰時區相差一個小時。

package main

import (
 "fmt"
 "time"
)

func main() {
 fmt.Println(time.Now())
}

中國大陸使用的是東八時區的標準時,即北京時間 CST,China Standard Time。

$ go run main.go 
2022-07-17 16:37:31.186043 +0800 CST m=+0.000066647

這是默認時區下的結果,time.Now()的打印中會標註+0800 CST

假設我們是在美國洛杉磯時區下,那得到的結果是什麼呢?

TZ="America/Los_Angeles" go run main.go
2022-07-17 01:39:12.391505 -0700 PDT m=+0.000069514

可以看到,此時的結果是-0700 PDT 時間,即 PDT(Pacific Daylight Time)太平洋夏季時間。由於時區差異,兩次執行的時間結果相差了 15 小時。

注意,在使用 Docker 容器時,系統默認的時區就是 UTC 時間(0 時區),和我們實際需要的北京時間相差八個小時,這是導致八小時時間差問題的經典場景。

時區問題的應對策略,可以詳細查看 src/time/zoneinfo_unix.go 中 initLocal() 函數的加載邏輯。例如,可以通過指定環境變量 TZ,修改 / etc/localtime 文件等方式來解決。

因爲時區問題非常重要,所以放在了文章第一部分講述。下面開始介紹 time 庫的使用。

時間瞬間 time.Time

time 庫,最核心的對象是 time.Time 結構體。它的定義如下,用以表示某個瞬間的時間。

type Time struct {
  // wall and ext encode the wall time seconds, wall time nanoseconds,
 // and optional monotonic clock reading in nanoseconds.
   wall uint64
   ext  int64
   loc *Location
}

計算機在時間處理上,主要涉及到兩種時鐘。

wall 和 ext 字段就是用於記錄牆上時鐘和單調時鐘,精度爲納秒。字段的對應位數上關聯着用於確定時間的具體年、月、日、小時、分鐘、秒等信息。

loc 字段記錄時區位置,當 loc 爲 nil 時,默認爲 UTC 時間。

因爲 time.Time 用於表示具有納秒精度的時間瞬間,在程序中通常應該將它作爲值存儲和傳遞,而不是指針。

即在時間變量或者結構體字段中,我們應該使用 time.Time,而非 *time.Time。

獲取 time.Time

我們可以通過 Now 函數獲取當前本地時間

func Now() Time {}

也可以通過 Date 函數,根據年、月、日等時間和時區參數獲取指定時間

func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time {}
轉換時間戳

計算機世界中,將 UTC 時間 1970 年 1 月 1 日 0 時 0 分 0 秒作爲 Unix 時間 0。所謂的時間瞬間轉換爲 Unix 時間戳,即計算的是從 Unix 時間 0 到指定瞬間所經過的秒數、微秒數等。

func (t Time) Unix() int64 {}      // 從 Unix 時間 0 經過的秒數
func (t Time) UnixMicro() int64 {} // 從 Unix 時間 0 經過的微秒數
func (t Time) UnixMilli() int64 {} // 從 Unix 時間 0 經過的毫秒數
func (t Time) UnixNano() int64 {}  // 從 Unix 時間 0 經過的納秒數
獲取基本字段
 t := time.Now()
 fmt.Println(t.Date())      // 2022 July 17
 fmt.Println(t.Year())      // 2022
 fmt.Println(t.Month())     // July
 fmt.Println(t.ISOWeek())   // 2022 28
 fmt.Println(t.Clock())     // 22 21 56
 fmt.Println(t.Day())       // 17
 fmt.Println(t.Weekday())   // Sunday
 fmt.Println(t.Hour())      // 22
 fmt.Println(t.Minute())    // 21
 fmt.Println(t.Second())    // 56
 fmt.Println(t.Nanosecond())// 494313000
 fmt.Println(t.YearDay())   // 198

持續時間 time.Duration

持續時間 time.Duration 用於表示兩個時間瞬間 time.Time 之間所經過的時間。它通過 int64 表示納秒計數,能表示的極限大約爲 290 年。

// A Duration represents the elapsed time between two instants
// as an int64 nanosecond count. The representation limits the
// largest representable duration to approximately 290 years.
type Duration int64

在 Go 中,持續時間只是一個以納秒爲單位的數字而已。如果持續時間等於 1000000000,則它代表的含義是 1 秒或 1000 毫秒或 1000000 微秒或 1000000000 納秒。

例如,相隔 1 小時的兩個時間瞬間 time.Time 值,它們之間的持續時間 time.Duration 值爲

1*60*60*1000*1000*1000

Go 的 time 包中定義了這些持續時間常量值

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

同時,time.Duration 提供了能獲取各時間粒度數值的方法

func (d Duration) Nanoseconds() int64 {}   // 納秒
func (d Duration) Microseconds() int64 {}  // 微秒
func (d Duration) Milliseconds() int64 {}  // 毫秒
func (d Duration) Seconds() float64 {}     // 秒
func (d Duration) Minutes() float64 {}     // 分鐘
func (d Duration) Hours() float64 {}       // 小時

時間計算

在學習了時間瞬間和持續時間之後,我們來看如何做時間計算。

func (t Time) Add(d Duration) Time {}
func (t Time) Sub(u Time) Duration {}
func (t Time) AddDate(years int, months int, days int) Time {}

當然,基於當前時間瞬間 time.Now() 的計算是最普遍的需求。因此,time 包還提供了以下便捷的時間計算函數。

func Since(t Time) Duration {}

Since 函數是 time.Now().Sub(t) 的快捷方法。

func Until(t Time) Duration {}

Until 函數是 t.Sub(time.Now()) 的快捷方法。

使用示例
 t := time.Now()
 fmt.Println(t)                      // 2022-07-17 22:41:06.001567 +0800 CST m=+0.000057466

 //時間增加 1小時
 fmt.Println(t.Add(time.Hour * 1))   // 2022-07-17 23:41:06.001567 +0800 CST m=+3600.000057466
 //時間增加 15 分鐘
 fmt.Println(t.Add(time.Minute * 15))// 2022-07-17 22:56:06.001567 +0800 CST m=+900.000057466
 //時間增加 10 秒鐘
 fmt.Println(t.Add(time.Second * 10))// 2022-07-17 22:41:16.001567 +0800 CST m=+10.000057466

 //時間減少 1 小時
 fmt.Println(t.Add(-time.Hour * 1))  // 2022-07-17 21:41:06.001567 +0800 CST m=-3599.999942534
 //時間減少 15 分鐘
 fmt.Println(t.Add(-time.Minute * 15))// 2022-07-17 22:26:06.001567 +0800 CST m=-899.999942534
 //時間減少 10 秒鐘
 fmt.Println(t.Add(-time.Second * 10))// 2022-07-17 22:40:56.001567 +0800 CST m=-9.999942534

 time.Sleep(time.Second * 5)
 t2 := time.Now()
 // 計算 t 到 t2 的持續時間
 fmt.Println(t2.Sub(t))              // 5.004318874s
 // 1 年之後的時間
 t3 := t2.AddDate(1, 0, 0)
 // 計算從 t 到當前的持續時間
 fmt.Println(time.Since(t))          // 5.004442316s
 // 計算現在到明年的持續時間
 fmt.Println(time.Until(t3))         // 8759h59m59.999864s

格式化時間

在其他語言中,一般會使用通用的時間模板來格式化時間。例如 Python,它使用 %Y 代表年、%m 代表月、%d 代表日等。

但是,Go 不一樣,它使用固定的時間(需要注意,使用其他的時間是不可以的)作爲佈局模板,而這個固定時間是 Go 語言的誕生時間。

Mon Jan 2 15:04:05 MST 2006

格式化時間涉及到兩個轉換函數

func Parse(layout, value string) (Time, error) {}
func (t Time) Format(layout string) string {}
示例
const (
   layoutISO = "2006-01-02"
   layoutUS  = "January 2, 2006"
)
date := "2012-08-09"
t, _ := time.Parse(layoutISO, date)
fmt.Println(t)                  // 2012-08-09 00:00:00 +0000 UTC
fmt.Println(t.Format(layoutUS)) // August 9, 2012

在 time 庫中,Go 提供了一些預定義的佈局模板常量,這些可以直接拿來使用。

const (
 Layout      = "01/02 03:04:05PM '06 -0700" // The reference time, in numerical order.
 ANSIC       = "Mon Jan _2 15:04:05 2006"
 UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
 RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
 RFC822      = "02 Jan 06 15:04 MST"
 RFC822Z     = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
 RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
 RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
 RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
 RFC3339     = "2006-01-02T15:04:05Z07:00"
 RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
 Kitchen     = "3:04PM"
 // Handy time stamps.
 Stamp      = "Jan _2 15:04:05"
 StampMilli = "Jan _2 15:04:05.000"
 StampMicro = "Jan _2 15:04:05.000000"
 StampNano  = "Jan _2 15:04:05.000000000"
)

下面是我們可選的佈局參數對照表

年         06/2006
月         01/1/Jan/January
日         02/2/_2
星期       Mon/Monday
小時       03/3/15
分         04/4
秒         05/5
毫秒       .000/.999
微秒       .000000/.999999
納秒       .000000000/.999999999
am/pm     PM/pm
時區       MST
時區小時數差-0700/-07/-07:00/Z0700/Z07:00

時區轉換

在文章開頭,我們介紹了時區問題。如果在代碼中,需要獲取同一個 time.Time 在不同時區下的結果,我們可以使用它的 In 方法。

func (t Time) In(loc *Location) Time {}

它的使用非常簡單,直接看示例代碼

now := time.Now()
fmt.Println(now)          // 2022-07-18 21:19:59.9636 +0800 CST m=+0.000069242

loc, _ := time.LoadLocation("UTC")
fmt.Println(now.In(loc)) // 2022-07-18 13:19:59.9636 +0000 UTC

loc, _ = time.LoadLocation("Europe/Berlin")
fmt.Println(now.In(loc)) // 2022-07-18 15:19:59.9636 +0200 CEST

loc, _ = time.LoadLocation("America/New_York")
fmt.Println(now.In(loc)) // 2022-07-18 09:19:59.9636 -0400 EDT

loc, _ = time.LoadLocation("Asia/Dubai")
fmt.Println(now.In(loc)) // 2022-07-18 17:19:59.9636 +0400 +04

總結

整體而言,time 庫提供的時間處理函數和方法,基本滿足我們的使用需求。

有意思的是,Go 時間格式化轉換必須採用 Go 誕生時間,確實有夠自戀。

機器鈴砍菜刀

歡迎添加小菜刀微信

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/mNFzC7FHgu73Ot0QC7MAnA