【go語言學習】標準庫之time
阿新 • • 發佈:2020-11-19
time包提供了時間的顯示和測量用的函式。日曆的計算採用的是公曆。
type Duration int64
const (
Nanosecond Duration = 1
Microsecond = 1000 * Nanosecond
Millisecond = 1000 * Microsecond
Second = 1000 * Millisecond
Minute = 60 * Second
Hour = 60 * Minute
)
一、時間型別
go語言原始碼:
package time func Now() Time {}
通過time包的time.Now()函式獲取當前時間的時間物件,進而可以呼叫該物件的方法獲取年月日時分秒的資訊。
程式碼示例:
package main import ( "fmt" "time" ) func main() { // 1.獲取當前時間的時間型別 now := time.Now() fmt.Printf("%T\n", now) fmt.Println("年: ", now.Year()) fmt.Println("月: ", now.Month()) fmt.Println("日: ", now.Day()) fmt.Println("時: ", now.Hour()) fmt.Println("分: ", now.Minute()) fmt.Println("秒: ", now.Second()) fmt.Println("星期: ", now.Weekday()) // 2.獲取指定時間的時間型別 t := time.Date(1997, 6, 5, 10, 20, 30, 000, time.Local) fmt.Printf("%T, %v\n", t, t) }
執行結果
time.Time
年: 2020
月: October
日: 6
時: 16
分: 15
秒: 18
星期: Tuesday
time.Time, 1997-06-05 10:20:30 +0800 CST
二、時間戳
時間戳是自1970年1月1日(08:00:00GMT)至當前時間的總毫秒數。它也被稱為Unix時間戳(UnixTimestamp)。
go語言原始碼:
// 獲取毫米時間戳 func (t Time) Unix() int64 {} // 獲取納秒時間戳 func (t Time) UnixNano() int64 {} // 通過毫秒時間戳獲取時間物件 func Unix(sec int64, nsec int64) Time {}
程式碼示例:
package main
import (
"fmt"
"time"
)
func main() {
// 1.獲取當前時間的時間戳
now := time.Now()
fmt.Println(now.Unix())
fmt.Println(now.UnixNano())
// 2.獲取指定時間的時間戳
t := time.Date(1997, 6, 5, 10, 20, 30, 000, time.Local)
fmt.Println(t.Unix())
fmt.Println(t.UnixNano())
// 3.通過時間戳獲取時間物件
timeStamp := now.Unix()
timeObj := time.Unix(timeStamp, 0)
fmt.Printf("%T, %v\n", timeObj, timeObj)
}
執行結果:
1601972490
1601972490960538000
865477230
865477230000000000
time.Time, 2020-10-06 16:21:30 +0800 CST
三、時間格式化
時間型別有一個自帶的方法Format進行格式化,需要注意的是Go語言中格式化時間模板不是常見的Y-m-d H:M:S而是使用Go的誕生時間2006年1月2號15點04分05秒000毫秒(記憶口訣為2006 1 2 3 4 5 000)。
package main
import (
"fmt"
"time"
)
func main() {
s := "1997年07月01日 10:23:21.089"
t, _ := time.Parse("2006年1月2日 15:04:05.999", s)
fmt.Printf("%T, %v\n", t, t)
// 獲取年月日
year, month, day := t.Date()
fmt.Println(year, month, day)
hour, minute, second := t.Clock()
fmt.Println(hour, minute, second)
}
執行結果
time.Time, 1997-07-01 10:23:21.089 +0000 UTC
1997 July 1
10 23 21
四、時間操作
Add
獲取一個時間之前或之後的時間物件。
func (t Time) Add(d Duration) Time{}
Equal
判斷時間是否相等,會有時區影響。
func (t Time) Equal(u Time) bool{}
Before
如果t代表的時間點在u之前,返回真;否則返回假。
func (t Time) Before(u Time) bool{}
After
如果t代表的時間點在u之後,返回真;否則返回假。
func (t Time) After(u Time) bool{}
程式碼示例
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Printf("%T, %v\n", now, now)
// Add
after := now.Add(time.Hour)
before := now.Add(-time.Hour)
fmt.Printf("%T, %v\n", after, after)
fmt.Printf("%T, %v\n", before, before)
// Equal
fmt.Println(now.Equal(after))
// After
fmt.Println(now.After(after))
// Before
fmt.Println(now.Before(after))
}
執行結果
time.Time, 2020-10-06 16:48:59.5954102 +0800 CST m=+0.002001501
time.Time, 2020-10-06 17:48:59.5954102 +0800 CST m=+3600.002001501
time.Time, 2020-10-06 15:48:59.5954102 +0800 CST m=-3599.997998499
false
false
true
五、定時器與睡眠
定時器
使用time.Tick(時間間隔)來設定定時器,定時器的本質上是一個通道(channel)。
go語言原始碼:
func Tick(d Duration) <-chan Time {}
示例程式碼
package main
import (
"fmt"
"time"
)
func main() {
ticker := time.Tick(time.Second)
for v := range ticker {
fmt.Println(v)
}
}
每間隔1s列印一個時間物件。
睡眠sleep
示例程式碼:
package main
import (
"fmt"
"time"
)
func main() {
for {
time.Sleep(1 * time.Second)
fmt.Println("hello world")
}
}
每間隔1s列印一句"hello world"。