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

golang中time包之時間間隔格式化和秒、毫秒、納秒等時間戳格式輸出的方法實例

 更新時間:2022年08月09日 11:02:56   作者:玉梅小洋  
時間和日期是我們編程中經常會用到的,下面這篇文章主要給大家介紹了關于golang中time包之時間間隔格式化和秒、毫秒、納秒等時間戳格式輸出的方法實例,需要的朋友可以參考下

獲取當前時間的年、月、日、時、分、秒的方法如下:

	// 獲取當前時間
	now := time.Now()

	// 當前時間的年、月、日、小時、分鐘、秒和納秒都可以通過現(xiàn)有接口直接獲取
	year := now.Year()
	month := now.Month()
	day := now.Day()
	hour := now.Hour()
	minute := now.Minute()
	second := now.Second()
	nanosecond := now.Nanosecond()

	// 當前時間的微秒和毫秒是通過納秒計算生成
	microsecond := nanosecond / 1e3
	millisecond := nanosecond / 1e6

	fmt.Println(now.Format("2006-01-02 15:04:05.000000000"))
	fmt.Println(year, month, day, hour, minute, second, nanosecond, microsecond, millisecond)

運行結果如下:

# 當前時間格式輸出
2022-06-09 19:25:52.022598620
2022 June 9 19 25 52 22598620 22598 22

獲取從1970到現(xiàn)在經過的時間的方法如下:

	// 獲取從1970經過的時間,秒和納秒都可以通過現(xiàn)有接口直接獲取
	sec := now.Unix()     // 時間戳位數(shù)為10
	ms := now.UnixMilli() // 時間戳位數(shù)為13
	us := now.UnixMicro() // 時間戳位數(shù)為16
	ns := now.UnixNano()  // 時間戳位數(shù)為19
	fmt.Printf("sec:%v\n ms:%v\n us:%v\n ns:%v\n", sec, ms, us, ns)

運行結果如下:

# 1970經過的時間格式輸出
sec:1654773952
 ms:1654773952022
 us:1654773952022598
 ns:1654773952022598620

時間間隔格式化輸出方法

	// 時間間隔返回的是time.Duration,下面以1h1m1s1ms1us1ns的時間間隔舉例,測試各種格式的打印效果
	duration := 1*time.Hour + 1*time.Minute + 1*time.Second +
		1*time.Millisecond + 1*time.Microsecond + 1*time.Nanosecond
	// 直接使用%v打印,不轉換sec、ms或其他。
	fmt.Printf("duration:%v\n", duration)
	fmt.Printf("duration:%6v\n", duration)
	fmt.Printf("duration:%.6v\n", duration)
	fmt.Printf("duration:%.3v\n", duration)

	// duration支持Hours()、 Minutes()、Seconds() 和
	// Milliseconds()、Microseconds()、Nanoseconds()接口
	// 前三個接口返回類型為float64可以通過0.3f打印小數(shù)點后的數(shù),
	// 后三個為int64,是整數(shù),小數(shù)點后都是0
	// 下面列舉秒和毫秒的格式打印,其他時間單位可以參考秒和毫秒

	// 秒的打印格式%f可以打印小數(shù)點后9位,精確到納秒
	fmt.Printf("duration:%vsec\n", duration.Seconds())
	fmt.Printf("duration:%0.3fsec\n", duration.Seconds())
	fmt.Printf("duration:%0.6fsec\n", duration.Seconds())

	// 毫秒沒有小數(shù)點,都是整數(shù),轉換成float后,小數(shù)點后都是0
	fmt.Printf("duration:%vms\n", duration.Milliseconds())
	fmt.Printf("duration:%.3dms\n", duration.Milliseconds())
	fmt.Printf("duration:%.3fms\n", float64(duration.Milliseconds()))
}

行結果如下:

# 1h1m1s1ms1us1ns的時間間隔舉例格式輸出
# %v沒有單位轉換的時間輸出
duration:1h1m1.001001001s
duration:1h1m1.001001001s
duration:1h1m1.
duration:1h1

# 秒的格式輸出
duration:3661.001001001sec
duration:3661.001sec
duration:3661.001001sec

# 毫秒的格式輸出
duration:3661001ms
duration:3661001ms
duration:3661001.000ms

通過測試程序可以看到:

1.沒時間單位轉換的格式輸出,直接用%v能精確到ns,%.3V,只是對輸出的字符串進行了切割。此處建議直接用%v即可。

2.對于秒的格式輸出,%v精確到小數(shù)點9位,即納秒。當然可以根據%f的格式調整,例如%.3f精確到毫秒

3.對于毫秒的格式輸出,直接用%v或%d即可,轉換成float64沒有意義

一般在統(tǒng)計一個函數(shù)或一段程序運行了多長時間,一般建議使用第二種方式,轉換成秒的格式輸出,再根據精度調整%f的格式即可。

第一種方式有可能出現(xiàn)時間單位不統(tǒng)一,例如一個是分鐘,一個是秒。

上面例子完成的代碼如下:

package main

import (
	"fmt"
	"time"
)

func main() {
	// 獲取當前時間
	now := time.Now()

	// 當前時間的年、月、日、小時、分鐘、秒和納秒都可以通過現(xiàn)有接口直接獲取
	year := now.Year()
	month := now.Month()
	day := now.Day()
	hour := now.Hour()
	minute := now.Minute()
	second := now.Second()
	nanosecond := now.Nanosecond()

	// 當前時間的微秒和毫秒是通過納秒計算生成
	microsecond := nanosecond / 1e3
	millisecond := nanosecond / 1e6

	fmt.Println(now.Format("2006-01-02 15:04:05.000000000"))
	fmt.Println(year, month, day, hour, minute, second, nanosecond, microsecond, millisecond)

	// 獲取從1970經過的時間,秒和納秒都可以通過現(xiàn)有接口直接獲取
	sec := now.Unix()     // 時間戳位數(shù)為10
	ms := now.UnixMilli() // 時間戳位數(shù)為13
	us := now.UnixMicro() // 時間戳位數(shù)為16
	ns := now.UnixNano()  // 時間戳位數(shù)為19
	fmt.Printf("sec:%v\n ms:%v\n us:%v\n ns:%v\n", sec, ms, us, ns)


	// 時間間隔返回的是time.Duration,下面以1h1m1s1ms1us1ns的時間間隔舉例,測試各種格式的打印效果
	duration := 1*time.Hour + 1*time.Minute + 1*time.Second +
		1*time.Millisecond + 1*time.Microsecond + 1*time.Nanosecond
	// 直接使用%v打印,不轉換sec、ms或其他。
	fmt.Printf("duration:%v\n", duration)
	fmt.Printf("duration:%6v\n", duration)
	fmt.Printf("duration:%.6v\n", duration)
	fmt.Printf("duration:%.3v\n", duration)

	// duration支持Hours()、 Minutes()、Seconds() 和
	// Milliseconds()、Microseconds()、Nanoseconds()接口
	// 前三個接口返回類型為float64可以通過0.3f打印小數(shù)點后的數(shù),
	// 后三個為int64,是整數(shù),小數(shù)點后都是0
	// 下面列舉秒和毫秒的格式打印,其他時間單位可以參考秒和毫秒

	// 秒的打印格式%f可以打印小數(shù)點后9位,精確到納秒
	fmt.Printf("duration:%vsec\n", duration.Seconds())
	fmt.Printf("duration:%0.3fsec\n", duration.Seconds())
	fmt.Printf("duration:%0.6fsec\n", duration.Seconds())

	// 毫秒沒有小數(shù)點,都是整數(shù),轉換成float后,小數(shù)點后都是0
	fmt.Printf("duration:%vms\n", duration.Milliseconds())
	fmt.Printf("duration:%.3dms\n", duration.Milliseconds())
	fmt.Printf("duration:%.3fms\n", float64(duration.Milliseconds()))
}

下面是時間間隔的時間單位轉換的源碼:

// time.go

// Nanoseconds returns the duration as an integer nanosecond count.
func (d Duration) Nanoseconds() int64 { return int64(d) }

// Microseconds returns the duration as an integer microsecond count.
func (d Duration) Microseconds() int64 { return int64(d) / 1e3 }

// Milliseconds returns the duration as an integer millisecond count.
func (d Duration) Milliseconds() int64 { return int64(d) / 1e6 }

// These methods return float64 because the dominant
// use case is for printing a floating point number like 1.5s, and
// a truncation to integer would make them not useful in those cases.
// Splitting the integer and fraction ourselves guarantees that
// converting the returned float64 to an integer rounds the same
// way that a pure integer conversion would have, even in cases
// where, say, float64(d.Nanoseconds())/1e9 would have rounded
// differently.

// Seconds returns the duration as a floating point number of seconds.
func (d Duration) Seconds() float64 {
	sec := d / Second
	nsec := d % Second
	return float64(sec) + float64(nsec)/1e9
}

// Minutes returns the duration as a floating point number of minutes.
func (d Duration) Minutes() float64 {
	min := d / Minute
	nsec := d % Minute
	return float64(min) + float64(nsec)/(60*1e9)
}

// Hours returns the duration as a floating point number of hours.
func (d Duration) Hours() float64 {
	hour := d / Hour
	nsec := d % Hour
	return float64(hour) + float64(nsec)/(60*60*1e9)
}

補充:如果想格式化為12小時方式,需指定PM。

func formatDemo() {
    now := time.Now()
    // 格式化的模板為Go的出生時間2006年1月2號15點04分 Mon Jan
    // 24小時制
    fmt.Println(now.Format("2006-01-02 15:04:05.000 Mon Jan"))
    // 12小時制
    fmt.Println(now.Format("2006-01-02 03:04:05.000 PM Mon Jan"))
    fmt.Println(now.Format("2006/01/02 15:04"))
    fmt.Println(now.Format("15:04 2006/01/02"))
    fmt.Println(now.Format("2006/01/02"))
}

總結

到此這篇關于golang中time包之時間間隔格式化和秒、毫秒、納秒等時間戳格式輸出的文章就介紹到這了,更多相關golang time包時間間隔格式化內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • golang?metrics各個指標含義講解說明

    golang?metrics各個指標含義講解說明

    這篇文章主要為大家介紹了golang?metrics各個指標含義講解說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05
  • 一文帶你了解Golang中interface的設計與實現(xiàn)

    一文帶你了解Golang中interface的設計與實現(xiàn)

    本文就來詳細說說為什么說?接口本質是一種自定義類型,以及這種自定義類型是如何構建起?go?的?interface?系統(tǒng)的,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-01-01
  • Golang依賴注入工具digo的使用詳解

    Golang依賴注入工具digo的使用詳解

    這篇文章主要為大家詳細介紹了Golang中依賴注入工具digo的使用,文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-06-06
  • Go語言指針用法詳解

    Go語言指針用法詳解

    Go指針和C指針在許多方面非常相似,但其中也有一些不同。本文詳細講解了Go語言指針的用法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • golang grpc 負載均衡的方法

    golang grpc 負載均衡的方法

    這篇文章主要介紹了golang grpc 負載均衡的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • Go每日一庫之zap日志庫的安裝使用指南

    Go每日一庫之zap日志庫的安裝使用指南

    這篇文章主要為大家介紹了Go每日一庫之zap安裝使用示例學習,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05
  • 深入了解Golang的map增量擴容

    深入了解Golang的map增量擴容

    這篇文章主要介紹了深入了解Golang的map增量擴容,擴容的主要目的是為了縮短map容器的響應時間。增量擴容的本質其實就是將總的擴容時間分攤到了每一次hash操作上,更多相關內容需要的小伙伴可以參考一下
    2022-06-06
  • 深入解析Go語言中HTTP請求處理的底層實現(xiàn)

    深入解析Go語言中HTTP請求處理的底層實現(xiàn)

    本文將詳細介紹?Go?語言中?HTTP?請求處理的底層機制,包括工作流程、創(chuàng)建?Listen?Socket?監(jiān)聽端口、接收客戶端請求并建立連接以及處理客戶端請求并返回響應等,需要的朋友可以參考下
    2023-05-05
  • Go Java算法之二叉樹的所有路徑示例詳解

    Go Java算法之二叉樹的所有路徑示例詳解

    這篇文章主要為大家介紹了Go Java算法之二叉樹的所有路徑示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • Go語言實現(xiàn)Snowflake雪花算法

    Go語言實現(xiàn)Snowflake雪花算法

    雪花算法產生的背景當然是twitter高并發(fā)環(huán)境下對唯一ID生成的需求,得益于twitter內部牛的技術,雪花算法能夠流傳于至今并且被廣泛使用,本文就詳細的介紹一下,感興趣的可以了解一下
    2021-06-06

最新評論

赤壁市| 牟定县| 屏东市| 太白县| 宝山区| 龙泉市| 文登市| 霍林郭勒市| 青岛市| 临沂市| 珲春市| 松原市| 寻乌县| 河曲县| 巨鹿县| 龙江县| 东源县| 仁寿县| 嵊州市| 德安县| 武清区| 永春县| 合阳县| 延津县| 曲沃县| 太湖县| 博白县| 霍山县| 彭水| 日照市| 大化| 宜兰市| 磴口县| 高淳县| 彭山县| 巫溪县| 冷水江市| 漳州市| 玛多县| 绥德县| 新民市|