将time.Duration类型的微秒值转换为毫秒 [英] Conversion of time.Duration type microseconds value to milliseconds
问题描述
我正在使用go-ping( https://github.com/sparrc/go-ping)库的非特权ICMP ping。
timeout:= time.Second * 1000
interval:= time.Second
count:= 5
host:= p.ipAddr $ b $ pinger,cmdErr:= ping.NewPinger(host)
pinger.Count = count
pinger.Interval =间隔
pinger.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
通过运行这个函数,我得到了以毫秒为单位的延迟和以微秒为单位的抖动。我需要同样的单位让我们说毫秒,所以当我正在做 jitter = stats.StdDevRtt / 1000
或 jitter = jitter / 1000
(将微秒转换为毫秒),我得到的是以纳秒为单位的抖动:(有没有什么办法可以让延迟和抖动达到相同的单位毫秒数。
数字为 time.Duration
time.Duration
是一个类型<
如果你知道价值,但你想要的不是纳秒,只需乘以你想要的单位,例如:
d:= 100 * time.Microsecond
fmt.Println(d)//输出:100μs
上述工作是因为 100
是一个非类型化的常量,它可以自动转换为 time.Duration
which具有 int64
基础类型。
请注意,如果您将该值作为类型值,则必须使用显式类型转换:
值:= 100 //值类型为int
d2:= time.Duration(value)* time.Millisecond
fmt.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:100000000
fmt.Println(μs:,int64(d2 / time.Microsecond) )//μs:100000
fmt.Println(ms:,int64(d2 / time.Millisecond))// ms:100
试试去游乐场的例子。
如果您的抖动(持续时间)小于您希望将其转换为的单位,则需要使用浮点除法,否则将执行整数除法小数部分。有关详细信息,请参阅: Golang Round最接近0.05 。
在分割之前将抖动和单位转换为 float64
< pre $ d:= 61 * time.Microsecond
fmt.Println(d)//输出:61μs
ms:= float64(d)/ float64(time.Millisecond)
fmt.Println(ms:,ms)//输出:ms:0.061
输出(在 Go Playground 上试用):
61μs
ms: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屋!