Conversion of time.Duration type microseconds value to milliseconds

GoTime

Go Problem Overview


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 Solutions


Solution 1 - Go

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

Solution 2 - Go

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

Solution 3 - Go

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsupriyaView Question on Stackoverflow
Solution 1 - GoiczaView Answer on Stackoverflow
Solution 2 - GoXesinaView Answer on Stackoverflow
Solution 3 - GosepehrView Answer on Stackoverflow