将time.Duration类型的微秒值转换为毫秒。

huangapple go评论83阅读模式
英文:

Conversion of time.Duration type microseconds value to milliseconds

问题

我正在使用Go语言的go-ping库(https://github.com/sparrc/go-ping)进行非特权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是time.Duration类型
jitter = stats.StdDevRtt  // stats.StdDevRtt是time.Duration类型

运行这段代码后,我得到的延迟单位是毫秒,而抖动单位是微秒。我希望它们都使用相同的单位,比如毫秒,所以当我执行jitter = stats.StdDevRtt/1000jitter = jitter/1000(将微秒转换为毫秒)时,我得到的抖动单位是纳秒。是否有办法让延迟和抖动都使用相同的单位,比如毫秒?

英文:

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.

答案1

得分: 172

数字转换为time.Duration

time.Duration 是一个类型,其底层类型int64,用于以纳秒为单位存储持续时间。

如果你知道数值,但想要使用除纳秒以外的单位,只需将你想要的单位乘以数值,例如:

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

上述代码之所以有效,是因为 100 是一个无类型的常量,它可以自动转换为具有 int64 底层类型的 time.Duration

请注意,如果你的数值是一个有类型的值,你需要使用显式的类型转换

value := 100 // value 是 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

Go Playground 上尝试这些示例。

如果你的抖动(持续时间)小于你希望转换的单位,你需要使用浮点除法,否则将执行整数除法,会截断小数部分。有关详细信息,请参阅:https://stackoverflow.com/questions/39544571/golang-round-to-nearest-0-05/39544897#39544897。

在进行除法之前,将抖动和单位都转换为 float64

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
英文:

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 wish 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: https://stackoverflow.com/questions/39544571/golang-round-to-nearest-0-05/39544897#39544897.

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

答案2

得分: 16

从Go 1.13版本开始,你可以使用新的Duration方法MicrosecondsMilliseconds,它们将以整数形式返回相应单位的持续时间。

https://golang.org/doc/go1.13#time

英文:

As of Go 1.13, you can use new Duration methods Microseconds and Milliseconds which return the duration as an integer count of their respectively named units.

https://golang.org/doc/go1.13#time

答案3

得分: 5

latencyjitter变量的类型是time.Duration,根据定义,它的基本类型是int64,并以纳秒为单位表示。

当你使用打印函数时,会调用time.Duration类型的String方法,并在打印持续时间时使用hsmμn的表示法。下面是String方法的文档:

// String返回一个表示持续时间的字符串,格式为"72h3m0.5s"。
// 前导零单位将被省略。作为特例,小于一秒的持续时间格式使用较小的单位(毫秒、微秒或纳秒),
// 以确保前导数字非零。零持续时间格式为0s。

time包中有一些预定义的常量,你可以使用这些常量将持续时间变量转换为你喜欢的时间单位,例如:

latencyInMicroSeconds := int64(jitter / time.Microsecond)

请注意,我们将其转换为int类型,因为如果不这样做,它仍然是time.Duration类型,而该类型的值被认为是以纳秒为单位的,但现在它是微秒,这会在使用time包函数进行计算时导致进一步的问题。

英文:

The type of latency and jitter variables is time.Duration which per definition its base type is int64 and is expressed in nanoseconds.

When you use print functions the String‍‍‍‍ ‍method of type time.Duration is invoked and it uses h, s, m, µ, n notations when printing the duration, here is the documentation for String method:

// String returns a string representing the duration in the form "72h3m0.5s".
// Leading zero units are omitted. As a special case, durations less than one
// second format use a smaller unit (milli-, micro-, or nanoseconds) to ensure
// that the leading digit is non-zero. The zero duration formats as 0s.

There are some pre-defined constants in the time package which you can use to convert the duration variable to your preferred unit of time, e.g.:

latencyInMicroSeconds := int64(jitter / time.Microsecond)

Pay attention that we converted it to an int type because if you won't it would be still in time.Duration type and the value of that type is considered to be in nanosecond unit but now it's microsecond which causes a further problem in calculations if you're going to use time package functions.

huangapple
  • 本文由 发表于 2017年1月6日 18:33:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/41503758.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定