golang 的time包之time


先看看有哪些類型

Time

時間類型,包含了秒和納秒以及Location

Month

type Month int 月份.定義了十二個月的常量

Weekday

type Weekday int 周,定義了一周的七天

Duration

type Duration int64 持續時間.定義了以下持續時間類型.多用於時間的加減 需要傳入Duration做為參數的時候.可以直接傳入time.Second

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

Location

在time包里有兩個時區變量:

  • time.UTC utc時間
  • time.Local 本地時間

FixedZone(name string, offset int) *Location
設置時區名,以及與UTC0的時間偏差.返回Location

時間格式化

Format(layout string) string
傳入目標模板(Mon Jan 02 15:04:05 -0700 2006).時間以這個為准
			p(t.Format("3:04PM"))
			p(t.Format("Mon Jan _2 15:04:05 2006"))
			p(t.Format("2006-01-02T15:04:05.999999-07:00"))
			p(t.Format("2006-01-02T15:04:05Z07:00"))
			fmt.Printf("%d-%02d-%02dT%02d:%02d:%02d-00:00\n",
			t.Year(), t.Month(), t.Day(),
			t.Hour(), t.Minute(), t.Second())
		
Parse(layout, value string) (Time, error)
將字符竄轉換為Time類型.
		p := fmt.Println

		withNanos := "2006-01-02 15:04:05"
		t, _ := time.Parse(withNanos, "2013-10-05 18:30:50")
		p(t.Year())
		
ParseDuration(s string) (Duration, error)
將字duration符竄("ns", "us" (or "碌s"), "ms", "s", "m", "h".)轉換為Duration類型.就是納秒
			p := fmt.Println

			t, _ := time.ParseDuration("1h")
			p(t.Seconds())
		

Time相關

time常用函數

Now() Time
獲取當前時間,返回Time類型
Unix(sec int64, nsec int64) Time
根據秒數和納秒,返回Time類型
Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
設置年月日返回,Time類型
Since(t Time) Duration
返回與當前時間的時間差

time常用方法

After(u Time) bool
時間類型比較,是否在Time之后
Before(u Time) bool
時間類型比較,是否在Time之前
Equal(u Time) bool
比較兩個時間是否相等
IsZero() bool
判斷時間是否為零值,如果sec和nsec兩個屬性都是0的話,則該時間類型為0
Date() (year int, month Month, day int)
返回年月日,三個參數
Year() int
返回年份
Month() Month
返回月份.是Month類型
Day() int
返回多少號
Weekday() Weekday
返回星期幾,是Weekday類型
ISOWeek() (year, week int)
返回年份,和該填是在這年的第幾周.
Clock() (hour, min, sec int)
返回小時,分鍾,秒
Hour() int
返回小時
Minute() int
返回分鍾
Second() int
返回秒數
Nanosecond() int
返回納秒
Add(d Duration) Time
為一個時間,添加的時間類型為Duration.更精確到納秒.比起AddDate
Sub(u Time) Duration
計算兩個時間的差.返回類型Duration
AddDate(years int, months int, days int) Time
添加時間.以年月日為參數
UTC() Time
設置location為UTC,然后返回時間.就是utc為0.比中國晚了八個小時.
Local() Time
設置location為本地時間.就是電腦時間.
In(loc *Location) Time
設置location為指定location
Location() *Location
獲取時間的Location,如果是nic,返回UTC,如果為空,則代表本地
Zone() (name string, offset int)
返回時區,以及與utc的時間偏差
Unix() int64
返回時間戳,自從1970年1月1號到現在
UnixNano() int64
返回時間戳.包含納秒
		func main() {
		    now := time.Now()
		    secs := now.Unix()
		    nanos := now.UnixNano()
		    fmt.Println(now)
		    millis := nanos / 1000000

		    fmt.Println(secs)
		    fmt.Println(millis)
		    fmt.Println(nanos)

		    fmt.Println(time.Unix(secs, 0))
		    fmt.Println(time.Unix(0, nanos))
		}
	
GobEncode() ([]byte, error)
編碼為gob
GobDecode(buf []byte) error
從gob解碼
MarshalJSON() ([]byte, error)
編列為json
UnmarshalJSON(data []byte) (err error)
解碼為json
	func main() {
	p := fmt.Println

	now := time.Now()
	p(now)

	d := time.Duration(7200 * 1000 * 1000 * 1000)
	p(d)

	then := time.Date(
		2013, 1, 7, 20, 34, 58, 651387237, time.UTC)

	p(then)
	p(then.Year())
	p(then.Month())
	p(then.Day())
	p(then.Hour())
	p(then.Minute())
	p(then.Second())
	p(then.Nanosecond())
	p(then.Location())
	p(then.Weekday())

	p(then.Before(now))
	p(then.After(now))
	p(then.Equal(now))

	p(then.Date())
	p(then.ISOWeek())
	p("----------")
	p(now.UTC())
	p(now.Local())
	p(now.Location())
	p(now.Zone())
	p(now.Unix())
	p(time.Unix(now.Unix(), 0))
	p(now.UnixNano())
	p(time.Unix(0, now.UnixNano()))
	p(now.GobEncode())
	p(now.MarshalJSON())
	p(time.Since(now))
	p("----------")
	diff := now.Sub(then)
	p(diff)

	p(diff.Hours())
	p(diff.Minutes())
	p(diff.Seconds())
	p(diff.Nanoseconds())
	p(then.Add(diff))
	p(then.Add(-diff))

	p(d)
	p(d.Hours())
	p(d.Minutes())
	p(d.Seconds())
	p(d.Nanoseconds())
	p(then.Add(d))
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM