Conversion of time.Duration type microseconds value to milliseconds
Asked Answered
B

3

80

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.

Bolter answered 6/1, 2017 at 10:33 Comment(0)
F
177

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: 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
Forebrain answered 6/1, 2017 at 10:42 Comment(5)
I want to convert jitter to milliseconds which I am getting in microseconds. jitter is time.Duration type. And this is not working for it. I tried doing jitter = (stats.StdDevRtt/time.Nanosecond).Bolter
@Bolter It's the second half of my answer: to get it in milliseconds: ms := int64(d2 / time.Millisecond)Forebrain
when I am doing ms := int64(d2 / time.Millisecond), it is giving jitter =0 which is right as jitter I am getting is 61 microseconds which will be 0.061 milliseconds and when rounded off with int, it will give 0 as jitter value, but this is not what I want... I want jitter value in decimal places i.e. 0.061.... Is there any way to do so?Bolter
it worked.One more doubt I have. What it is printing now is "0.061". Can I get it like "0.061 ms" ?Bolter
@Bolter You can print it however you like: fmt.Printf("%.3f ms", ms).Forebrain
C
17

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

Commentator answered 28/10, 2019 at 11:12 Comment(1)
Remark : these methods truncate do not round. For example for 0.999μs we can see that time.Duration(999).Microseconds() returns 0.Commando
C
5

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.

Cantrip answered 6/1, 2017 at 11:20 Comment(2)
I tried doing jitterInMilliSeconds := (jitter / time.Microsecond) so what I am getting is jitter in nanoseconds now.... as you have written in your answer that while printing time.Duration type value, string method is called and it ensures that while printing , leading digit is non-zero. As jitter value I am getting is 64.6 microsecond so in miliseconds it will be 0.064 milliseconds which is less than zero. So is it the reason that I am getting it on nanoceconds when divided by time.Microsecond... Is there any way to do it?Bolter
@Bolter sorry, jitterInMilliSeconds variable should be latencyInMicroSeconds (already fixed), maybe this sample helps to figure it out: gist.github.com/sepehrmm/0c46f3b6cc417c9db21c15f767197c4aCantrip

© 2022 - 2024 — McMap. All rights reserved.