Conversion of time.Duration type microseconds value to milliseconds
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.
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
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