Conversion of time.Duration type microseconds valu

2020-01-29 05:21发布

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.

标签: go time
3条回答
戒情不戒烟
2楼-- · 2020-01-29 05:54

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
查看更多
不美不萌又怎样
3楼-- · 2020-01-29 05:55

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

查看更多
Rolldiameter
4楼-- · 2020-01-29 06:07

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

When you use print functions the String‍‍‍‍ ‍method of type time.Duration is invoked and it use 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 time package which you can use to convert the duration variable to your preferred unit of time, like this:

latencyInMicroSeconds := int64(jitter / time.Microsecond)

Pay attention that we converted it to a 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 nano second unit but now it's micro second which cause further problem in calculations if you're going to use time package functions.

查看更多
登录 后发表回答