将 time.Duration 类型的微秒值转换为毫秒 [英] Conversion of time.Duration type microseconds value to milliseconds

查看:33
本文介绍了将 time.Duration 类型的微秒值转换为毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 go-ping ( https://github.com/sparrc/go-ping ) 用于非特权 ICMP ping 的 golang 库.

timeout := time.Second*1000间隔:= time.Second计数:= 5主机 := p.ipAddrpinger, cmdErr := ping.NewPinger(host)pinger.Count = 计数pinger.Interval = 间隔pinger.Timeout = 超时pinger.SetPrivileged(false)pinger.Run()统计数据:= pinger.Statistics()delay = stats.AvgRtt//stats.AvgRtt 是 time.Duration 类型jitter = stats.StdDevRtt//stats.StdDevRtt 是 time.Duration 类型

通过运行这个,我得到了毫秒级的延迟和微秒级的抖动.我想要相同的单位让我们说毫秒所以当我做 jitter = stats.StdDevRtt/1000jitter = jitter/1000 (将微秒转换为毫秒)时,什么我得到的是以纳秒为单位的抖动 :(.有什么方法可以使延迟和抖动获得相同的单位毫秒.

解决方案

Number to time.Duration

time.Duration 是具有 的类型int64 作为其底层类型,以纳秒为单位存储持续时间.

如果您知道该值但您想要的不是纳秒,只需乘以您想要的单位,例如:

d := 100 * time.Microsecondfmt.Println(d)//输出:100µs

上述有效是因为 100 是一个无类型的 constant,并且它可以自动转换为具有 int64 底层类型的 time.Duration.

请注意,如果您将该值作为类型值,则必须使用显式类型转换:

value := 100//值为 int 类型d2 := time.Duration(value) * time.Millisecondfmt.Println(d2)//输出:100ms

time.Duration 到数字

所以 time.Duration 总是纳秒.例如,如果您需要以毫秒为单位,您需要做的就是将 time.Duration 值除以以毫秒为单位的纳秒数:

ms := int64(d2/time.Millisecond)fmt.Println("ms:", ms)//输出:ms: 100

其他示例:

fmt.Println("ns:", int64(d2/time.Nanosecond))//ns: 100000000fmt.Println("µs:", int64(d2/time.Microsecond))//µs: 100000fmt.Println("ms:", int64(d2/time.Millisecond))//ms: 100

试试 Go Playground 上的示例.

如果您的抖动(持续时间)小于您希望将其转换为的单位,则需要使用浮点除法,否则将执行整数除法,从而切断小数部分.详情请参见:Golang Round to Nearest 0.05.>

分割前将抖动和单位都转换为float64:

d := 61 * time.Microsecondfmt.Println(d)//输出:61µsms := float64(d)/float64(time.Millisecond)fmt.Println("ms:", ms)//输出:ms: 0.061

输出(在 Go Playground 上试试):

61µs毫秒:0.061

I am using go-ping ( https://github.com/sparrc/go-ping )library of golang for unprivileged ICMP ping.

timeout := time.Second*1000
interval := time.Second
count := 5
host := p.ipAddr
pinger, cmdErr := ping.NewPinger(host)

pinger.Count = count
pinger.Interval = interval
pinger.Timeout = timeout
pinger.SetPrivileged(false)
pinger.Run()
stats := pinger.Statistics()

latency = stats.AvgRtt  // stats.AvgRtt is time.Duration type
jitter = stats.StdDevRtt// stats.StdDevRtt is time.Duration type

From running this, I am getting latency in milliseconds and jitter in microseconds. I want same unit for both let's say millisecond so when I am doing jitter = stats.StdDevRtt/1000 or jitter = jitter/1000 (to convert microseconds to milliseconds), what I am getting is jitter in nanoseconds :(. Is there any way to get same unit milliseconds for both latency and jitter.

解决方案

Number to time.Duration

time.Duration is a type having int64 as its underlying type, which stores the duration in nanoseconds.

If you know the value but you want other than nanoseconds, simply multiply the unit you want, e.g.:

d := 100 * time.Microsecond
fmt.Println(d) // Output: 100µs

The above works because 100 is an untyped constant, and it can be converted automatically to time.Duration which has int64 underlying type.

Note that if you have the value as a typed value, you have to use explicit type conversion:

value := 100 // value is of type int

d2 := time.Duration(value) * time.Millisecond
fmt.Println(d2) // Output: 100ms

time.Duration to number

So time.Duration is always the nanoseconds. If you need it in milliseconds for example, all you need to do is divide the time.Duration value with the number of nanoseconds in a millisecond:

ms := int64(d2 / time.Millisecond)
fmt.Println("ms:", ms) // Output: ms: 100

Other examples:

fmt.Println("ns:", int64(d2/time.Nanosecond))  // ns: 100000000
fmt.Println("µs:", int64(d2/time.Microsecond)) // µs: 100000
fmt.Println("ms:", int64(d2/time.Millisecond)) // ms: 100

Try the examples on the Go Playground.

If your jitter (duration) is less than the unit you whish to convert it to, you need to use floating point division, else an integer division will be performed which cuts off the fraction part. For details see: Golang Round to Nearest 0.05.

Convert both the jitter and unit to float64 before dividing:

d := 61 * time.Microsecond
fmt.Println(d) // Output: 61µs

ms := float64(d) / float64(time.Millisecond)
fmt.Println("ms:", ms) // Output: ms: 0.061

Output (try it on the Go Playground):

61µs
ms: 0.061

这篇关于将 time.Duration 类型的微秒值转换为毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆