golang中time比較的時區問題

2021-09-13 03:57:03 字數 527 閱讀 5968

最近發現程式中有乙個時間**比較老是不對

at, err := time.parse("2006-01-02 15:04:05", authtime)

if err != nil

nt := time.now()

fmt.println(nt, at)

if nt.after(at)

列印nt和at後發現at明明在nt之前,但是nt.after返回了false.

fmt.println(nt.unix(), at.unix())
列印nt和at的unix時間戳後發現,at時間增加了8個小時.

檢視time.parse文件後發現,這裡解析的是utc時間,與中國時間相差8個小時.

使用time.parseinlocation即可解決問題

time.parseinlocation("2006-01-02 15:04:05", authtime, time.local)

golang中的標準庫time

time.time型別表示時間。我們可以通過time.now 函式獲取當前的時間物件,然後獲取時間物件的年月日時分秒等資訊。示例 如下 func main 時間戳是自1970年1月1日 08 00 00gmt 至當前時間的總毫秒數。它也被稱為unix時間戳 unixtimestamp 基於時間物件獲...

golang中時間 time 的相關操作

golang中時間相關操作,主要是用time包的函式,time中最主要又包含了time.time這個物件。funcgetnow funcformatunixtime funcgetyear 將2016 07 27 08 46 15這樣的時間字串轉換時間戳 funcstrtounix fmt.prin...

Golang中的struct比較

可比較的資料型別除了上述三種外,還有boolean,complex,pointer,channel,inte ce和array 不可比較的資料型別包括,slice,map,和function 不包含不可比較的成員變數 reflect.deepequal函式,指標和例項均可以比較 無論有沒有包含不可比...