当前位置:网站首页>golang's time package: methods for time interval formatting and output of timestamp formats such as seconds, milliseconds, and nanoseconds
golang's time package: methods for time interval formatting and output of timestamp formats such as seconds, milliseconds, and nanoseconds
2022-08-02 06:12:00 【m0_67394006】
获取当前时间的年、月、日、时、分、The method of seconds is as follows:
// 获取当前时间
now := time.Now()
// 当前时间的年、月、日、小时、分钟、Both seconds and nanoseconds can be obtained directly through the existing interface
year := now.Year()
month := now.Month()
day := now.Day()
hour := now.Hour()
minute := now.Minute()
second := now.Second()
nanosecond := now.Nanosecond()
// The microseconds and milliseconds of the current time are generated by nanosecond calculations
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
获取从1970The method of the elapsed time up to now is as follows:
// 获取从1970经过的时间,Both seconds and nanoseconds can be obtained directly through the existing interface
sec := now.Unix() // Timestamp bits are 10
ms := now.UnixMilli() // Timestamp bits are 13
us := now.UnixMicro() // Timestamp bits are 16
ns := now.UnixNano() // Timestamp bits are 19
fmt.Printf("sec:%v
ms:%v
us:%v
ns:%v
", sec, ms, us, ns)
运行结果如下:
# 1970Elapsed time format output
sec:1654773952
ms:1654773952022
us:1654773952022598
ns:1654773952022598620
Time interval formatted output method:
// The time interval returns yestime.Duration,下面以1h1m1s1ms1us1nsexample of time intervals,Test the printing effect of various formats
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
", duration)
fmt.Printf("duration:%6v
", duration)
fmt.Printf("duration:%.6v
", duration)
fmt.Printf("duration:%.3v
", duration)
// duration支持Hours()、 Minutes()、Seconds() 和
// Milliseconds()、Microseconds()、Nanoseconds()接口
// The first three interface return types are float64可以通过0.3fPrint the number after the decimal point,
// 后三个为int64,是整数,小数点后都是0
// The following lists the format printing of seconds and milliseconds,Other time units can refer to seconds and milliseconds
// Print format for seconds%fAfter the decimal point can be printed9位,精确到纳秒
fmt.Printf("duration:%vsec
", duration.Seconds())
fmt.Printf("duration:%0.3fsec
", duration.Seconds())
fmt.Printf("duration:%0.6fsec
", duration.Seconds())
// Milliseconds have no decimal point,都是整数,转换成float后,小数点后都是0
fmt.Printf("duration:%vms
", duration.Milliseconds())
fmt.Printf("duration:%.3dms
", duration.Milliseconds())
fmt.Printf("duration:%.3fms
", float64(duration.Milliseconds()))
}
行结果如下:
# 1h1m1s1ms1us1nsThe time interval example format output
# %vTime output without unit conversion
duration:1h1m1.001001001s
duration:1h1m1.001001001s
duration:1h1m1.
duration:1h1
# 秒的格式输出
duration:3661.001001001sec
duration:3661.001sec
duration:3661.001001sec
# Millisecond format output
duration:3661001ms
duration:3661001ms
duration:3661001.000ms
It can be seen through the test program:
1.Format output without time unit conversion,直接用%v能精确到ns,%.3V,Just cut the output string.Direct use is recommended here%v即可.
2.For second format output,%v精确到小数点9位,即纳秒.当然可以根据%fformat adjustment,例如%.3f精确到毫秒
3.For millisecond format output,直接用%v或%d即可,转换成float64没有意义
It is generally used to count how long a function or a program has been running,一般建议使用第二种方式,Convert output in seconds format,Then adjust according to the accuracy%f的格式即可.
In the first way, the time unit may not be uniform,For example one is minutes,一个是秒.
The completed code for the above example is as follows:
package main
import (
"fmt"
"time"
)
func main() {
// 获取当前时间
now := time.Now()
// 当前时间的年、月、日、小时、分钟、Both seconds and nanoseconds can be obtained directly through the existing interface
year := now.Year()
month := now.Month()
day := now.Day()
hour := now.Hour()
minute := now.Minute()
second := now.Second()
nanosecond := now.Nanosecond()
// The microseconds and milliseconds of the current time are generated by nanosecond calculations
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经过的时间,Both seconds and nanoseconds can be obtained directly through the existing interface
sec := now.Unix() // Timestamp bits are 10
ms := now.UnixMilli() // Timestamp bits are 13
us := now.UnixMicro() // Timestamp bits are 16
ns := now.UnixNano() // Timestamp bits are 19
fmt.Printf("sec:%v
ms:%v
us:%v
ns:%v
", sec, ms, us, ns)
// The time interval returns yestime.Duration,下面以1h1m1s1ms1us1nsexample of time intervals,Test the printing effect of various formats
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
", duration)
fmt.Printf("duration:%6v
", duration)
fmt.Printf("duration:%.6v
", duration)
fmt.Printf("duration:%.3v
", duration)
// duration支持Hours()、 Minutes()、Seconds() 和
// Milliseconds()、Microseconds()、Nanoseconds()接口
// The first three interface return types are float64可以通过0.3fPrint the number after the decimal point,
// 后三个为int64,是整数,小数点后都是0
// The following lists the format printing of seconds and milliseconds,Other time units can refer to seconds and milliseconds
// Print format for seconds%fAfter the decimal point can be printed9位,精确到纳秒
fmt.Printf("duration:%vsec
", duration.Seconds())
fmt.Printf("duration:%0.3fsec
", duration.Seconds())
fmt.Printf("duration:%0.6fsec
", duration.Seconds())
// Milliseconds have no decimal point,都是整数,转换成float后,小数点后都是0
fmt.Printf("duration:%vms
", duration.Milliseconds())
fmt.Printf("duration:%.3dms
", duration.Milliseconds())
fmt.Printf("duration:%.3fms
", float64(duration.Milliseconds()))
}
Below is the source code for time unit conversion for time intervals:
// 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)
}
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在.深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小.自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前.因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担.添加下方名片,即可获取全套学习资料哦
边栏推荐
猜你喜欢
apisix-入门使用篇
Android Studio 实现登录注册-源代码 (连接MySql数据库)
Towhee 每周模型
CAN光端机解决泰和安TX3016C消防主机长距离联网问题 实现CAN与光纤之间的双向数据智能转换
Introduction and use of apifox (1).
MySQL 8.0.29 解压版安装教程(亲测有效)
100 latest software testing interview questions in 2022, summary of common interview questions and answers
navicat新建数据库
mysql安装教程【安装版】
力扣 2127. 参加会议的最多员工数 拓扑剪枝与2360补充
随机推荐
Does Conway's Law Matter for System Architecture?
2022河南萌新联赛第(四)场:郑州轻工业大学 C - 最大公因数
MySQL如何创建用户
Detailed explanation of the software testing process (mind map) of the first-tier manufacturers
golang的time包:时间间隔格式化和秒、毫秒、纳秒等时间戳格式输出的方法
浏览器的onload事件
Towhee 每周模型
系统(层次)聚类
[QNX Hypervisor 2.2用户手册]9.17 tolerance
软件测试分析流程及输出项包括哪些内容?
LeetCode刷题系列 -- 787. K 站中转内最便宜的航班
AMQP协议详解
MySQL 5.7 upgrade to 8.0 detailed process
[网鼎杯 2020 青龙组]singal
07-传统的生产者消费者问题、防止虚假唤醒
Android studio连接MySQL并完成简单的登录注册功能
MySQL 用户授权
go语言中的goroutine(协程)
Js数据类型转化之数组的join方法
18年程序员生涯,读了200多本编程书,挑出一些精华分享给大家