簡介
time
顧名思義,該包提供了時間相關(guān)的操作function
,使用需要 import "time"
數(shù)據(jù)類型
主要數(shù)據(jù)類型有以下幾種
type Time struct{ // 時間主要結(jié)構(gòu)
... // 內(nèi)容省略
}
// A Month specifies a month of the year (January = 1, ...).
type Month int // 月份別名,主要為了實現(xiàn)月份的輸出 實現(xiàn)了 Stringer 接口
// A Weekday specifies a day of the week (Sunday = 0, ...).
type WeekDay int // 一周中的第幾天, 實現(xiàn)了 Stringer 接口
type Duration int64 // 時間精度熟嫩,最小精度為納秒
Month
、WeekDay
沒有相關(guān)的API,只為了輸出string類型實現(xiàn)了 Stringer
接口,主要time
合住、Time
僚害、Duration
API
time 包 API
func Now() Time // 返回當前時間Time
func Unix(sec int64, nsec int64) Time // 返回sec(時間戳)烤送、nsec(納秒范圍)的時間描述Time
func Since(t Time) Duration // 返回指定時間距離當前時間已經(jīng)過去的時間
func Until(t Time) Duration // 返回指定時間局當前時間還有的時間
func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time // 指定日期、時間拳锚、時區(qū)牙丽,返回定時間Time
例子:
now := time.Now() // 獲取當前時間
dateTime := time.Date(2018, 10, 10, 0, 0, 0, 0, time.Local) // 獲取2018年10月10號0點0分0秒的時間Time
Time API
func (t Time) Unix() int64 // 返回時間戳,精度秒
func (t Time) UnixNano() int64 // 返回時間戳,精度納秒
func (t Time) UTC() Time // 返回 UTC 時區(qū)的時間Time
func (t Time) After(u Time) bool // 檢查時間是否在時間 u 之后
func (t Time) Before(u Time) bool // 檢查時間是否在時間 u 之前
func (t Time) Equal(u Time) bool // 檢查時間是否與 u 相同
func (t Time) IsZero() bool // 檢查是否為零時构罗,即格林威治時間0點
func (t Time) Date() (year int, month Month, day int) // 返回年月日
func (t Time) Year() int // 返回年
func (t Time) Month() Month // 返回月
func (t Time) Day() int // 返回日
func (t Time) Weekday() Weekday // 返回一周中的第幾天
func (t Time) ISOWeek() (year, week int) // 返回年和當前一年中第幾周
func (t Time) Clock() (hour, min, sec int) // 返回時分秒
func (t Time) Hour() int // 返回小時
func (t Time) Minute() int // 返回分鐘
func (t Time) Second() int // 返回秒
func (t Time) Nanosecond() int // 返回納秒部分值
func (t Time) YearDay() int // 返回一年中的第幾天
func (t Time) Add(d Duration) Time // 返回 time +d 的時間
func (t Time) Sub(u Time) Duration // 返回 time -d 的時間
func (t Time) AddDate(years int, months int, days int) Time // 返回加上指定年绰播、月谬泌、日的時間
func (t Time) Local() Time // 放回當前時區(qū)的time
func (t Time) In(loc *Location) Time // 設(shè)置時區(qū)
func (t Time) Location() *Location // 返回時區(qū)信息
例子:
// 獲取當前0點0分0秒的時間
now := time.Now()
hour, minute, second := now.Clock()
dayStartTimeStamp := now.Add(time.Duration(-1*(3600*hour+60*minute+second)) * time.Second)
addDate := now.AddDate(-1, 1, 1)
fmt.Println("Sub:", now.Sub(addDate))
Duration API
func (d Duration) Nanoseconds() int64 // 返回納秒級精度數(shù)據(jù)
func (d Duration) Seconds() float64 // 返回秒級精度數(shù)據(jù)
func (d Duration) Minutes() float64 // 返回分鐘級精度數(shù)據(jù)
func (d Duration) Hours() float64 // 返回小時級精度數(shù)據(jù)
Exmaple
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println("Now Time:", now) // 系統(tǒng)設(shè)置的時區(qū)時間
fmt.Println("Now Timestamp:", now.Unix())
fmt.Println("Now Timestamp Nano:", now.UnixNano())
fmt.Println("Now UTC:", now.UTC()) // UTC 時區(qū)
year, week := now.ISOWeek()
fmt.Printf("year:%v week:%v\n", year, week)
hour, minute, second := now.Clock()
fmt.Printf("%v:%v:%v\n", hour, minute, second)
fmt.Println("Houre:", now.Hour())
fmt.Println("Minute", now.Minute())
fmt.Println("Second", now.Second())
fmt.Println("NanoSecond:", now.Nanosecond())
// 獲取當天0點的時間戳
dayStartTimeStamp := now.Add(time.Duration(-1*(3600*hour+60*minute+second)) * time.Second)
fmt.Println(dayStartTimeStamp)
addDate := now.AddDate(-1, 1, 1)
fmt.Println("AddDate:", addDate)
fmt.Println("Sub:", now.Sub(addDate).Hours())
// 獲取指定日期的時間戳
curTime := time.Date(2018, 10, 10, 0, 0, 0, 0, time.Local)
fmt.Println(curTime)
// 0點時間
zeroTime := time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)
fmt.Println("zero:", zeroTime.IsZero())
fmt.Println(int(time.Since(curTime)) / 1e9)
fmt.Println(time.Since(curTime).Seconds())
fmt.Println(int(time.Until(curTime)) / 1e9)
}
// 輸出--時間相關(guān),每次輸出是時間點都不一樣
Now Time: 2018-10-14 16:21:11.575369147 +0800 CST m=+0.002621862
Now Timestamp: 1539505271
Now Timestamp Nano: 1539505271575369147
Now UTC: 2018-10-14 08:21:11.575369147 +0000 UTC
year:2018 week:41
16:21:11
Houre: 16
Minute 21
Second 11
NanoSecond: 575369147
2018-10-14 00:00:00.575369147 +0800 CST m=-58870.997378138
AddDate: 2017-11-15 16:21:11.575369147 +0800 CST
Sub: 7992
2018-10-10 00:00:00 +0800 CST
zero: true
404471
404471.576116895
-404471