golang中time包之時(shí)間間隔格式化和秒、毫秒、納秒等時(shí)間戳格式輸出的方法實(shí)例
獲取當(dāng)前時(shí)間的年、月、日、時(shí)、分、秒的方法如下:
// 獲取當(dāng)前時(shí)間 now := time.Now() // 當(dāng)前時(shí)間的年、月、日、小時(shí)、分鐘、秒和納秒都可以通過現(xiàn)有接口直接獲取 year := now.Year() month := now.Month() day := now.Day() hour := now.Hour() minute := now.Minute() second := now.Second() nanosecond := now.Nanosecond() // 當(dāng)前時(shí)間的微秒和毫秒是通過納秒計(jì)算生成 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)
運(yùn)行結(jié)果如下:
# 當(dāng)前時(shí)間格式輸出
2022-06-09 19:25:52.022598620
2022 June 9 19 25 52 22598620 22598 22
獲取從1970到現(xiàn)在經(jīng)過的時(shí)間的方法如下:
// 獲取從1970經(jīng)過的時(shí)間,秒和納秒都可以通過現(xiàn)有接口直接獲取 sec := now.Unix() // 時(shí)間戳位數(shù)為10 ms := now.UnixMilli() // 時(shí)間戳位數(shù)為13 us := now.UnixMicro() // 時(shí)間戳位數(shù)為16 ns := now.UnixNano() // 時(shí)間戳位數(shù)為19 fmt.Printf("sec:%v\n ms:%v\n us:%v\n ns:%v\n", sec, ms, us, ns)
運(yùn)行結(jié)果如下:
# 1970經(jīng)過的時(shí)間格式輸出
sec:1654773952
ms:1654773952022
us:1654773952022598
ns:1654773952022598620
時(shí)間間隔格式化輸出方法:
// 時(shí)間間隔返回的是time.Duration,下面以1h1m1s1ms1us1ns的時(shí)間間隔舉例,測試各種格式的打印效果 duration := 1*time.Hour + 1*time.Minute + 1*time.Second + 1*time.Millisecond + 1*time.Microsecond + 1*time.Nanosecond // 直接使用%v打印,不轉(zhuǎn)換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()接口 // 前三個(gè)接口返回類型為float64可以通過0.3f打印小數(shù)點(diǎn)后的數(shù), // 后三個(gè)為int64,是整數(shù),小數(shù)點(diǎn)后都是0 // 下面列舉秒和毫秒的格式打印,其他時(shí)間單位可以參考秒和毫秒 // 秒的打印格式%f可以打印小數(shù)點(diǎn)后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ù)點(diǎn),都是整數(shù),轉(zhuǎn)換成float后,小數(shù)點(diǎn)后都是0 fmt.Printf("duration:%vms\n", duration.Milliseconds()) fmt.Printf("duration:%.3dms\n", duration.Milliseconds()) fmt.Printf("duration:%.3fms\n", float64(duration.Milliseconds())) }
行結(jié)果如下:
# 1h1m1s1ms1us1ns的時(shí)間間隔舉例格式輸出
# %v沒有單位轉(zhuǎn)換的時(shí)間輸出
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.沒時(shí)間單位轉(zhuǎn)換的格式輸出,直接用%v能精確到ns,%.3V,只是對輸出的字符串進(jìn)行了切割。此處建議直接用%v即可。
2.對于秒的格式輸出,%v精確到小數(shù)點(diǎn)9位,即納秒。當(dāng)然可以根據(jù)%f的格式調(diào)整,例如%.3f精確到毫秒
3.對于毫秒的格式輸出,直接用%v或%d即可,轉(zhuǎn)換成float64沒有意義
一般在統(tǒng)計(jì)一個(gè)函數(shù)或一段程序運(yùn)行了多長時(shí)間,一般建議使用第二種方式,轉(zhuǎn)換成秒的格式輸出,再根據(jù)精度調(diào)整%f的格式即可。
第一種方式有可能出現(xiàn)時(shí)間單位不統(tǒng)一,例如一個(gè)是分鐘,一個(gè)是秒。
上面例子完成的代碼如下:
package main import ( "fmt" "time" ) func main() { // 獲取當(dāng)前時(shí)間 now := time.Now() // 當(dāng)前時(shí)間的年、月、日、小時(shí)、分鐘、秒和納秒都可以通過現(xiàn)有接口直接獲取 year := now.Year() month := now.Month() day := now.Day() hour := now.Hour() minute := now.Minute() second := now.Second() nanosecond := now.Nanosecond() // 當(dāng)前時(shí)間的微秒和毫秒是通過納秒計(jì)算生成 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經(jīng)過的時(shí)間,秒和納秒都可以通過現(xiàn)有接口直接獲取 sec := now.Unix() // 時(shí)間戳位數(shù)為10 ms := now.UnixMilli() // 時(shí)間戳位數(shù)為13 us := now.UnixMicro() // 時(shí)間戳位數(shù)為16 ns := now.UnixNano() // 時(shí)間戳位數(shù)為19 fmt.Printf("sec:%v\n ms:%v\n us:%v\n ns:%v\n", sec, ms, us, ns) // 時(shí)間間隔返回的是time.Duration,下面以1h1m1s1ms1us1ns的時(shí)間間隔舉例,測試各種格式的打印效果 duration := 1*time.Hour + 1*time.Minute + 1*time.Second + 1*time.Millisecond + 1*time.Microsecond + 1*time.Nanosecond // 直接使用%v打印,不轉(zhuǎn)換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()接口 // 前三個(gè)接口返回類型為float64可以通過0.3f打印小數(shù)點(diǎn)后的數(shù), // 后三個(gè)為int64,是整數(shù),小數(shù)點(diǎn)后都是0 // 下面列舉秒和毫秒的格式打印,其他時(shí)間單位可以參考秒和毫秒 // 秒的打印格式%f可以打印小數(shù)點(diǎn)后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ù)點(diǎn),都是整數(shù),轉(zhuǎn)換成float后,小數(shù)點(diǎn)后都是0 fmt.Printf("duration:%vms\n", duration.Milliseconds()) fmt.Printf("duration:%.3dms\n", duration.Milliseconds()) fmt.Printf("duration:%.3fms\n", float64(duration.Milliseconds())) }
下面是時(shí)間間隔的時(shí)間單位轉(zhuǎn)換的源碼:
// 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) }
補(bǔ)充:如果想格式化為12小時(shí)方式,需指定PM
。
func formatDemo() { now := time.Now() // 格式化的模板為Go的出生時(shí)間2006年1月2號15點(diǎn)04分 Mon Jan // 24小時(shí)制 fmt.Println(now.Format("2006-01-02 15:04:05.000 Mon Jan")) // 12小時(shí)制 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")) }
總結(jié)
到此這篇關(guān)于golang中time包之時(shí)間間隔格式化和秒、毫秒、納秒等時(shí)間戳格式輸出的文章就介紹到這了,更多相關(guān)golang time包時(shí)間間隔格式化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Go語言中時(shí)間time相關(guān)處理方法詳解
- Go Time庫中時(shí)間和日期相關(guān)的操作方法整理
- go?time.Sleep睡眠指定時(shí)間實(shí)例詳解(小時(shí)級到納秒級)
- 解決golang時(shí)間字符串轉(zhuǎn)time.Time的坑
- golang time包做時(shí)間轉(zhuǎn)換操作
- golang的time包:秒、毫秒、納秒時(shí)間戳輸出方式
- golang 使用time包獲取時(shí)間戳與日期格式化操作
- django 中使用DateTime常用的時(shí)間查詢方式
- Go語言時(shí)間管理利器之深入解析time模塊的實(shí)戰(zhàn)技巧
相關(guān)文章
Go語言實(shí)現(xiàn)超時(shí)的三種方法實(shí)例
超時(shí)在一些業(yè)務(wù)場景里非常普遍,下面這篇文章主要給大家介紹了關(guān)于Go語言實(shí)現(xiàn)超時(shí)的三種方法,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Go語言具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-07-07GoLang?socket網(wǎng)絡(luò)編程傳輸數(shù)據(jù)包時(shí)進(jìn)行長度校驗(yàn)的方法
在GoLang?socket網(wǎng)絡(luò)編程中,為了確保數(shù)據(jù)交互的穩(wěn)定性和安全性,通常會通過傳輸數(shù)據(jù)的長度進(jìn)行校驗(yàn),發(fā)送端首先發(fā)送數(shù)據(jù)長度,然后發(fā)送數(shù)據(jù)本體,接收端則根據(jù)接收到的數(shù)據(jù)長度和數(shù)據(jù)本體進(jìn)行比較,以此來確認(rèn)數(shù)據(jù)是否傳輸成功2024-11-11利用go語言實(shí)現(xiàn)查找二叉樹中的最大寬度
這篇文章主要介紹了利用go語言實(shí)現(xiàn)查找二叉樹中的最大寬度,文章圍繞主題展開詳細(xì)介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05Go語言網(wǎng)站使用異步編程和Goroutine提高Web的性能
作為一門現(xiàn)代化編程語言,Go語言提供了強(qiáng)大的異步編程能力,使得程序員可以以更高效的方式處理并發(fā)任務(wù),在Go語言中,使用Goroutine在單個(gè)進(jìn)程中實(shí)現(xiàn)多任務(wù)并行處理,以及如何使用協(xié)程池來進(jìn)一步提高Web服務(wù)器的處理能力,2024-01-01golang值類型轉(zhuǎn)換成[]uint8類型的操作
這篇文章主要介紹了golang值類型轉(zhuǎn)換成[]uint8類型的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-05-05Go語言中TCP/IP網(wǎng)絡(luò)編程的深入講解
這篇文章主要給大家介紹了關(guān)于Go語言中TCP/IP網(wǎng)絡(luò)編程的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05Golang實(shí)現(xiàn)加權(quán)輪詢負(fù)載均衡算法
加權(quán)輪詢負(fù)載均衡算法是一種常見的負(fù)載均衡策略,本文主要介紹了Golang實(shí)現(xiàn)加權(quán)輪詢負(fù)載均衡算法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-08-08