日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

golang中的time详情

發(fā)布時間:2025/6/15 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 golang中的time详情 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

簡介

介紹Go 標準庫time常用導(dǎo)出函數(shù),結(jié)構(gòu)體及其方法。

import "time"

time包提供顯示和計算時間用的函數(shù)。

## 時間處理類型

Go 中時間處理依賴的數(shù)據(jù)類型:time.Time,time.Month,time.Weekday,time.Duration,time.Location

time.Time 時間點

time.Time 代表一個納秒精度的時間點.

/usr/local/go/src/time/time.go 定義如下:

type Time struct {sec int64 // 從1年1月1日 00:00:00 UTC 至今過去的秒數(shù)nsec int32 // 最近一秒到下一秒過去的納秒數(shù)loc *Location // 時區(qū) }

時間變量標識一個具體的時間點,演示如下:

var t time.Time // 定義 time.Time 類型變量 t = time.Now() // 獲取當前時間 fmt.Printf("時間: %v, 時區(qū): %v, 時間類型: %T\n", t, t.Location(), t) // 時間: 2017-02-22 09:06:05.816187261 +0800 CST, 時區(qū): Local, 時間類型: time.Time// time.UTC() time 返回UTC 時區(qū)的時間 fmt.Printf("時間: %v, 時區(qū): %v, 時間類型: %T\n", t.UTC(), t.UTC().Location(), t) // 時間: 2017-02-22 01:07:15.179280004 +0000 UTC, 時區(qū): UTC, 時間類型: time.Time

代碼中應(yīng)使用time.Time 類型值來保存和傳遞時間,而不能用指針。表示時間的變量和字段,應(yīng)為time.Time類型,而不是*time.Time類型

每一個時間點都具有時區(qū)信息,當計算時間的表示格式時,如Format、Hour和Year等方法,都會考慮該信息。Local、UTC和In方法返回一個指定時區(qū)(但指向同一時間點)的Time。修改時區(qū)信息只是會改變其表示,不會修改被表示的時間點

time.Month 月份

time.Month代表一年中的某個月

/usr/local/go/src/time/time.go定義如下:

type Month int const (January Month = 1 + iotaFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember )

time.Weekday 星期

time.Weekday代表一周的周幾。

/usr/local/go/src/time/time.go定義如下:

type Weekday int

和星期相關(guān)的常量:

const (Sunday Weekday = iotaMondayTuesdayWednesdayThursdayFridaySaturday )

time.Duration 時間段

time.Duration類型代表兩個時間點之間經(jīng)過的納秒數(shù),可表示的最長時間段約為290年。

/usr/local/go/src/time/time.go定義如下:

type Duration int64

涉及常量如下:

const (Nanosecond Duration = 1Microsecond = 1000 * NanosecondMillisecond = 1000 * MicrosecondSecond = 1000 * MillisecondMinute = 60 * SecondHour = 60 * Minute )

time.Location 時區(qū)

Location代表一個地點,以及該地點所在的時區(qū)信息。北京時間可以使用Asia/Shanghai

/usr/local/go/src/time/zoneinfo.go中定義:

type Location struct {name stringzone []zonetx []zoneTranscacheStart int64cacheEnd int64cacheZone *zone }

預(yù)定義時區(qū)變量:

var UTC *Location = &utcLoc var Local *Location = &localLoc

time.Time 方法

介紹time.Time接受者的方法:獲取時間點,獲取時間相關(guān)信息,時間比較,計算和序列化操作

獲取一個時間的方法

  • func Now() Time {} // 當前本地時間
  • func Unix(sec int64, nsec int64) Time {} // 根據(jù)時間戳返回本地時間
  • func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time {} // 返回指定時間
// 當前本地時間 t = time.Now() fmt.Println("'time.Now': ", t)// 根據(jù)時間戳返回本地時間 t_by_unix := time.Unix(1487780010, 0) fmt.Println("'time.Unix': ", t_by_unix)// 返回指定時間 t_by_date := time.Date(2017, time.Month(2), 23, 1, 30, 30, 0, l) fmt.Println("'time.Date': ", t_by_date)

時間顯示

  • func (t Time) UTC() Time {} // 獲取指定時間在UTC 時區(qū)的時間表示
  • func (t Time) Local() Time {} // 以本地時區(qū)表示
  • func (t Time) In(loc *Location) Time {} // 時間在指定時區(qū)的表示
  • func (t Time) Format(layout string) string {} // 按指定格式顯示時間
// 獲取指定時間在UTC 時區(qū)的時間表示 t_by_utc := t.UTC() fmt.Println("'t.UTC': ", t_by_utc)// 獲取本地時間表示 t_by_local := t.Local() fmt.Println("'t.Local': ", t_by_local)// 時間在指定時區(qū)的表示 t_in := t.In(time.UTC) fmt.Println("'t.In': ", t_in)// Format fmt.Println("t.Format", t.Format(time.RFC3339))

獲取日期信息

  • 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 {} // 一年中對應(yīng)的天
  • func (t Time) Location() *Location {} // 時間的時區(qū)
  • func (t Time) Zone() (name string, offset int) {} // 時間所在時區(qū)的規(guī)范名和想對UTC 時間偏移量
  • func (t Time) Unix() int64 {} // 時間轉(zhuǎn)為時間戳
  • func (t Time) UnixNano() int64 {} // 時間轉(zhuǎn)為時間戳(納秒)
// 返回時間的日期信息 year, month, day := t.Date() fmt.Println("'t.Date': ", year, month, day)// 星期 week := t.Weekday() fmt.Println("'t.Weekday': ", week)// 返回年,星期范圍編號 year, week_int := t.ISOWeek() fmt.Println("'t.ISOWeek': ", year, week_int)// 返回時間的時分秒 hour, min, sec := t.Clock() fmt.Println("'t.Clock': ", hour, min, sec)

時間比較與計算

  • func (t Time) IsZero() bool {} // 是否是零時時間
  • 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) Add(d Duration) Time {} // 返回t +d 的時間點
  • func (t Time) Sub(u Time) Duration {} // 返回 t-u
  • func (t Time) AddDate(years int, months int, days int) Time {} 返回增加了給出的年份、月份和天數(shù)的時間點Time
// 返回增加了給出的年份、月份和天數(shù)的時間點Time t_new := t.AddDate(0, 1, 1) fmt.Println("'t.AddDate': ", t_new)// 時間在u 之前 is_after := t.After(t_new) fmt.Println("'t.After': ", is_after)

時間序列化

  • func (t Time) MarshalBinary() ([]byte, error) {} // 時間序列化
  • func (t Time) UnmarshalBinary(data []byte) error {} // 反序列化
  • func (t Time) MarshalJSON() ([]byte, error) {} // 時間序列化
  • func (t Time) MarshalText() ([]byte, error) {} // 時間序列化
  • func (t Time) GobEncode() ([]byte, error) {} // 時間序列化
  • func (t Time) GobDecode() ([]byte, error) {} // 時間序列化
// 時間序列化 t_byte, err := t.MarshalJSON() fmt.Println("'t.MarshalJSON': ", t_byte, err)// 時間數(shù)據(jù)反序列化 var t_un time.Time err = t_un.UnmarshalJSON(t_byte) fmt.Println("'t_un.UnmarshalJSON': ", t_un, err)

time.Duration 方法

介紹 time.Duration 時間段接受者的方法:輸出和改變時間段單位。

  • func (d Duration) String() string // 格式化輸出 Duration
  • func (d Duration) Nanoseconds() int64 // 將時間段表示為納秒
  • func (d Duration) Seconds() float64 // 將時間段表示為秒
  • func (d Duration) Minutes() float64 // 將時間段表示為分鐘
  • func (d Duration) Hours() float64 // 將時間段表示為小時
// time.Duration 時間段 fmt.Println("time.Duration 時間段") d = time.Duration(10000000000000)fmt.Printf("'String: %v', 'Nanoseconds: %v', 'Seconds: %v', 'Minutes: %v', 'Hours: %v'\n", d.String(), d.Nanoseconds(), d.Seconds(), d.Minutes(), d.Hours()) // 'String: 2h46m40s', 'Nanoseconds: 10000000000000', 'Seconds: 10000', 'Minutes: 166.66666666666666', 'Hours: 2.7777777777777777'

time.Location 方法

time.Location 時區(qū)的導(dǎo)出函數(shù)

  • func (l *Location) String() string // 輸出時區(qū)名
  • func FixedZone(name string, offset int) *Location // FixedZone 使用給定的地點名name和時間偏移量offset(單位秒)創(chuàng)建并返回一個Location
  • func LoadLocation(name string) (*Location, error) // LoadLocation 使用給定的名字創(chuàng)建Location
var local *time.Location local, ok := time.LoadLocation("Asia/Shanghai") fmt.Printf("%v, %T, %v\n", local, local, ok)

其他方法

  • func Sleep(d Duration)// Sleep阻塞當前go程至少d代表的時間段。d<=0時,Sleep會立刻返回
d_second := time.Second time.Sleep(d_second)

總結(jié)

以上是生活随笔為你收集整理的golang中的time详情的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。