当前位置:网站首页>golang的time包:时间间隔格式化和秒、毫秒、纳秒等时间戳格式输出的方法
golang的time包:时间间隔格式化和秒、毫秒、纳秒等时间戳格式输出的方法
2022-08-02 05:01:00 【m0_67394006】
获取当前时间的年、月、日、时、分、秒的方法如下:
// 获取当前时间
now := time.Now()
// 当前时间的年、月、日、小时、分钟、秒和纳秒都可以通过现有接口直接获取
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到现在经过的时间的方法如下:
// 获取从1970经过的时间,秒和纳秒都可以通过现有接口直接获取
sec := now.Unix() // 时间戳位数为10
ms := now.UnixMilli() // 时间戳位数为13
us := now.UnixMicro() // 时间戳位数为16
ns := now.UnixNano() // 时间戳位数为19
fmt.Printf("sec:%v
ms:%v
us:%v
ns:%v
", 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
", duration)
fmt.Printf("duration:%6v
", duration)
fmt.Printf("duration:%.6v
", duration)
fmt.Printf("duration:%.3v
", duration)
// duration支持Hours()、 Minutes()、Seconds() 和
// Milliseconds()、Microseconds()、Nanoseconds()接口
// 前三个接口返回类型为float64可以通过0.3f打印小数点后的数,
// 后三个为int64,是整数,小数点后都是0
// 下面列举秒和毫秒的格式打印,其他时间单位可以参考秒和毫秒
// 秒的打印格式%f可以打印小数点后9位,精确到纳秒
fmt.Printf("duration:%vsec
", duration.Seconds())
fmt.Printf("duration:%0.3fsec
", duration.Seconds())
fmt.Printf("duration:%0.6fsec
", duration.Seconds())
// 毫秒没有小数点,都是整数,转换成float后,小数点后都是0
fmt.Printf("duration:%vms
", duration.Milliseconds())
fmt.Printf("duration:%.3dms
", duration.Milliseconds())
fmt.Printf("duration:%.3fms
", 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精确到小数点9位,即纳秒。当然可以根据%f的格式调整,例如%.3f精确到毫秒
3.对于毫秒的格式输出,直接用%v或%d即可,转换成float64没有意义
一般在统计一个函数或一段程序运行了多长时间,一般建议使用第二种方式,转换成秒的格式输出,再根据精度调整%f的格式即可。
第一种方式有可能出现时间单位不统一,例如一个是分钟,一个是秒。
上面例子完成的代码如下:
package main
import (
"fmt"
"time"
)
func main() {
// 获取当前时间
now := time.Now()
// 当前时间的年、月、日、小时、分钟、秒和纳秒都可以通过现有接口直接获取
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经过的时间,秒和纳秒都可以通过现有接口直接获取
sec := now.Unix() // 时间戳位数为10
ms := now.UnixMilli() // 时间戳位数为13
us := now.UnixMicro() // 时间戳位数为16
ns := now.UnixNano() // 时间戳位数为19
fmt.Printf("sec:%v
ms:%v
us:%v
ns:%v
", 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
", duration)
fmt.Printf("duration:%6v
", duration)
fmt.Printf("duration:%.6v
", duration)
fmt.Printf("duration:%.3v
", duration)
// duration支持Hours()、 Minutes()、Seconds() 和
// Milliseconds()、Microseconds()、Nanoseconds()接口
// 前三个接口返回类型为float64可以通过0.3f打印小数点后的数,
// 后三个为int64,是整数,小数点后都是0
// 下面列举秒和毫秒的格式打印,其他时间单位可以参考秒和毫秒
// 秒的打印格式%f可以打印小数点后9位,精确到纳秒
fmt.Printf("duration:%vsec
", duration.Seconds())
fmt.Printf("duration:%0.3fsec
", duration.Seconds())
fmt.Printf("duration:%0.6fsec
", duration.Seconds())
// 毫秒没有小数点,都是整数,转换成float后,小数点后都是0
fmt.Printf("duration:%vms
", duration.Milliseconds())
fmt.Printf("duration:%.3dms
", duration.Milliseconds())
fmt.Printf("duration:%.3fms
", 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)
}
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
猜你喜欢

MySQL 用户授权

Android Studio 实现登录注册-源代码 (连接MySql数据库)

MySQL 8.0.29 decompressed version installation tutorial (valid for personal testing)

navicat无法连接mysql超详细处理方法

H5接入支付流程-微信支付&支付宝支付

MySQL 8.0.28 version installation and configuration method graphic tutorial

MobaXsterm如何使用

Does Conway's Law Matter for System Architecture?

UE4 创建开始游戏界面UI

Android studio connects to MySQL and completes simple login and registration functions
随机推荐
12个MySQL慢查询的原因分析
力扣练习——42 二叉树的层次遍历 II
UE4 局域网联机案例
MySQL 5.7详细下载安装配置教程
面试测试工程师一般会问什么?测试主管告诉你
c语言:查漏补缺(三)
使用pycharm debug 深度学习代码
H5接入支付流程-微信支付&支付宝支付
大屏UI设计-看这一篇就够了
MySQL(7)
选择黑盒测试用例设计方法的综合策略方案总结
2022河南萌新联赛第(四)场:郑州轻工业大学 A - ZZULI
2022年7月学习计划完成情况
MES如何做好生产过程监控,本文给出了详细解答
MySQL 8.0.29 decompressed version installation tutorial (valid for personal testing)
MySQL 8.0.29 设置和修改默认密码
力扣 2127. 参加会议的最多员工数 拓扑剪枝与2360补充
CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!)
从DES走到AES(现代密码的传奇之路)
Liquidated damages are too high"