最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Golang 日期/時間包的使用詳解

 更新時間:2019年03月12日 16:17:31   作者:big_cat  
這篇文章主要介紹了Golang 日期/時間包的使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

golang 的日期時間包:time 的使用方式。

  • time package 包含了 time.Time 時間對象 及 構建此時間對象的一些方法(time.Unix(), time.Parse())
  • golang 可精確到 nanosecond,故相應的函數(shù)返回值或參數(shù)都已納秒為單位,我們可以借助time.ParseDuration(durationString string)友好的生成納秒度量的時間跨度值
  • golang 的時間格式串Layout固定為 2006-01-02 15:04:05
  • golang 默認使用 Local 即本地時區(qū),可以通過 time.LoadLocation(zoneName string) (*Location, error)來設定時區(qū)

時區(qū)構建/格式化模式串

// 構建時區(qū)
var timeLocation *time.Location
timeLocation, _ = time.LoadLocation("")       //UTC
timeLocation, _ = time.LoadLocation("UTC")      //UTC
timeLocation, _ = time.LoadLocation("Local")     //Local
timeLocation, _ = time.LoadLocation("Asia/Shanghai") //使用時區(qū)碼

//golang的時間格式化pattern
var timeLayout = "2006-01-02 15:04:05"

當前時間對象

// 獲取當前時間對象
var timer time.Time
timer = time.Now()

// 為時間設定時區(qū) 可以通過 timer.Local()/timer.UTC() 快速設定時區(qū)
timer.In(timeLocation)

獲取秒級時間戳/納秒級時間戳

// 獲取當前秒級時間戳
var curTimestamp int64
curTimestamp = timer.Unix()
println("current timestamp:" + strconv.FormatInt(curTimestamp, 10))

// 獲取當前納秒及時間戳 1秒=1000毫秒=1000,000微妙=1000,000,000納秒
var curNanoTimestamp int64
curNanoTimestamp = timer.UnixNano()
println("current nano timestamp:" + strconv.FormatInt(curNanoTimestamp, 10))

獲取本地時間的時區(qū)/CST標準時間/自定義格式

// 獲取本地時間的時區(qū)/快速獲取時區(qū)時間/自定義格式
timeZone, _ := timer.Zone()
fmt.Printf("time zone: %s\n", timeZone)
fmt.Printf("time location: %s\n", timer.Location())
fmt.Printf("time in local zone: %s\n", timer.Local().String())
fmt.Printf("time in utc zone: %s\n", timer.UTC().String())
fmt.Printf("time: %s\n", timer.String())
fmt.Printf("time formatted: %s\n", timer.Format("2006-01-02 15:04:05"))

獲取當前的年/月/日 時:分:秒 納秒

// 獲取當前的年/月/日 時:分:秒 納秒
fmt.Printf("current year: %d\n", timer.Year())
fmt.Printf("current month: %d %s\n", timer.Month(), timer.Month()) //返回的Month對象
fmt.Printf("current day: %d\n", timer.Day())
fmt.Printf("current hour: %d\n", timer.Hour())
fmt.Printf("current minute: %d\n", timer.Minute())
fmt.Printf("current second: %d\n", timer.Second())
fmt.Printf("current nanosecond: %d\n", timer.Nanosecond())

獲取當前時間/日期

// 獲取當前時間/日期
curHour, curMinute, curSecond := timer.Clock()
fmt.Printf("current clock: %d:%02d:%02d\n", curHour, curMinute, curSecond)
curYear, curMonth, curDay := timer.Date()
fmt.Printf("current date: %d-%02d-%02d\n", curYear, curMonth, curDay)

編輯時間/求兩個日期的時間差

time.ParseDuration(durationString string)可以方便我們使用語義構建時間跨度值,數(shù)值單位為納秒,比如:
timeDuration, _ := time.ParseDuration("24h")
timeDuration, _ := time.ParseDuration("12m")
timeDuration, _ := time.ParseDuration("6s")
timeDuration, _ := time.ParseDuration("1ms")
timeDuration, _ := time.ParseDuration("1us")
timeDuration, _ := time.ParseDuration("1ns")


// 已當前時間為基增長年/月/日后的時間對象
timerAdded := timer.AddDate(1, 2, 3)
curYear, curMonth, curDay = timerAdded.Date()
fmt.Printf("current date: %d-%02d-%02d\n", curYear, curMonth, curDay)

// 以當前時間為基增長N納秒后的時間對象 比如增長了一天
timeDuration, _ := time.ParseDuration("24h")
timerAdded = timer.Add(timeDuration)
// 計算兩個時間的差值 返回的是納秒 按需求自行計算其他單位
// Duration is type of int64 and nanoseconds
timeDuration = timerAdded.Sub(timer)
fmt.Printf("days duration between %s~%s: %d\n",
  timerAdded.Format(timeLayout),
  timer.Format(timeLayout),
  timeDuration/1000/1000/1000/24/60/60)

使用 時間字符串 / Unix Timestamp 構建時間對象

// 使用時間串獲取時間對象
timer, _ = time.Parse(timeLayout, "2018-08-08 08:08:08")
// 使用時間串獲取時間對象 并設定時區(qū)
timer, _ = time.ParseInLocation(timeLayout, "2018-08-08 08:08:08", timeLocation)
// 使用Unix時間戳構建時間對象
timer = time.Unix(1552368806, 0) //2019-03-12 13:33:26的Unix時間戳
fmt.Println(timer.Format(timeLayout))

獲取當前時間是本年第幾天 本周第幾天

注意周日 的 Weekday編號為 0

// 獲取當前時間是本年第幾天 本周第幾天
fmt.Printf("year day: %d, week day: %d\n", timer.YearDay(), timer.Weekday())

使用表征字符串轉(zhuǎn)換時間跨度

// 使用表征字符串轉(zhuǎn)換時間跨度
timeDuration, _ = time.ParseDuration("300s")
fmt.Printf("nanosecond: %d\n", timeDuration)
timeDuration, _ = time.ParseDuration("300us")
fmt.Printf("nanosecond: %d\n", timeDuration)

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Golang 類型轉(zhuǎn)換的實現(xiàn)(斷言、強制、顯式類型)

    Golang 類型轉(zhuǎn)換的實現(xiàn)(斷言、強制、顯式類型)

    將一個值從一種類型轉(zhuǎn)換到另一種類型,便發(fā)生了類型轉(zhuǎn)換,在go可以分為斷言、強制、顯式類型轉(zhuǎn)換,本文就詳細的介紹一下這就幾種轉(zhuǎn)換方式,具有一定的參考價值,感興趣的可以了解一下
    2023-09-09
  • goLand Delve版本太老的問題及解決

    goLand Delve版本太老的問題及解決

    這篇文章主要介紹了goLand Delve版本太老的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Golang 串口通信的實現(xiàn)示例

    Golang 串口通信的實現(xiàn)示例

    串口通信是一種常見的硬件通信方式,用于在計算機和外部設備之間傳輸數(shù)據(jù),本文主要介紹了Golang 串口通信的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下
    2024-03-03
  • GoFrame框架gset交差并補集使用實例

    GoFrame框架gset交差并補集使用實例

    這篇文章主要為大家介紹了GoFrame框架gset交差并補集使用實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • Go基本數(shù)據(jù)類型與string類型互轉(zhuǎn)

    Go基本數(shù)據(jù)類型與string類型互轉(zhuǎn)

    本文主要介紹了Go基本數(shù)據(jù)類型與string類型互轉(zhuǎn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-03-03
  • Golang 如何解析和生成json

    Golang 如何解析和生成json

    這篇文章主要介紹了Golang 如何解析和生成json,幫助大家更好的理解和學習golang,感興趣的朋友可以了解下
    2020-09-09
  • Go語言實現(xiàn)UDP版聊天小工具的示例詳解

    Go語言實現(xiàn)UDP版聊天小工具的示例詳解

    這篇文章主要為大家詳細介紹了如何利用Go語言實現(xiàn)聊天小工具(UDP版),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-03-03
  • go責任鏈行為型設計模式Chain?Of?Responsibility

    go責任鏈行為型設計模式Chain?Of?Responsibility

    這篇文章主要為大家介紹了go行為型設計模式之責任鏈Chain?Of?Responsibility使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-12-12
  • golang打包成帶圖標的exe可執(zhí)行文件

    golang打包成帶圖標的exe可執(zhí)行文件

    這篇文章主要給大家介紹了關于golang打包成帶圖標的exe可執(zhí)行文件的相關資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2023-06-06
  • 多階段構建優(yōu)化Go?程序Docker鏡像

    多階段構建優(yōu)化Go?程序Docker鏡像

    這篇文章主要為大家介紹了多階段構建優(yōu)化Go?程序Docker鏡像,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08

最新評論

清河县| 平山县| 荆门市| 西宁市| 达拉特旗| 同心县| 乌兰县| 民丰县| 冀州市| 荆州市| 密山市| 怀来县| 宜春市| 萝北县| 龙山县| 吉木萨尔县| 体育| 英德市| 文安县| 新泰市| 大港区| 昭觉县| 石泉县| 新巴尔虎右旗| 鹤壁市| 错那县| 墨江| 诸暨市| 柘荣县| 长沙市| 章丘市| 上虞市| 武平县| 北海市| 清徐县| 泽普县| 宜章县| 沁水县| 波密县| 缙云县| 峨山|