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

sup*_*iya 34 time go

我正在使用Go-ping(https://github.com/sparrc/go-ping)golang库来进行无特权的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
Run Code Online (Sandbox Code Playgroud)

从运行它开始,我得到延迟(以毫秒为单位)和抖动(以微秒为单位).我想要两个相同的单位让我们说毫秒,所以当我做jitter = stats.StdDevRtt/1000jitter = jitter/1000(将微秒转换为毫秒)时,我得到的是以纳秒为单位的抖动:(.是否有任何方法可以获得相同的单位毫秒的延迟和抖动.

icz*_*cza 95

号码到 time.Duration

time.Duration是具有类型int64作为其基础类型,其存储在毫微秒的持续时间.

如果你知道这个值但你想要的不是纳秒,那么只需乘以你想要的单位,例如:

d := 100 * time.Microsecond
fmt.Println(d) // Output: 100µs
Run Code Online (Sandbox Code Playgroud)

上面的工作原因100是因为是一个无类型的常量,它可以自动转换为time.Duration具有int64底层类型的常量.

请注意,如果您将值作为类型化值,则必须使用显式类型转换:

value := 100 // value is of type int

d2 := time.Duration(value) * time.Millisecond
fmt.Println(d2) // Output: 100ms
Run Code Online (Sandbox Code Playgroud)

time.Duration 数字

所以time.Duration总是纳秒.例如,如果您需要它以毫秒为单位,那么您需要做的就是将time.Duration值除以毫秒中的纳秒数:

ms := int64(d2 / time.Millisecond)
fmt.Println("ms:", ms) // Output: ms: 100
Run Code Online (Sandbox Code Playgroud)

其他例子:

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
Run Code Online (Sandbox Code Playgroud)

试试Go Playground上的例子.

如果您的抖动(持续时间)小于您要将其转换为的单位,则需要使用浮点除法,否则将执行整数除法,从而切断小数部分.有关详细信息,请参阅:Golang Round至最近0.05.

将抖动和单位转换为float64分割前:

d := 61 * time.Microsecond
fmt.Println(d) // Output: 61µs

ms := float64(d) / float64(time.Millisecond)
fmt.Println("ms:", ms) // Output: ms: 0.061
Run Code Online (Sandbox Code Playgroud)

输出(在Go Playground上试试):

61µs
ms: 0.061
Run Code Online (Sandbox Code Playgroud)


小智 9

从 Go 1.13 开始,您可以使用新的 Duration 方法Microseconds并将Milliseconds持续时间作为它们各自命名的单位的整数计数返回。

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

  • 备注:这些方法截断不舍入。例如,对于 0.999μs,我们可以看到“time.Duration(999).Microseconds()”返回“0”。 (2认同)

sep*_*ehr 5

latency和变量的类型jittertime.Duration每个定义的其基本类型为 int64 并以纳秒表示。

\n

当您使用打印函数时,将调用String\xe2\x80\x8d\xe2\x80\x8d\xe2\x80\x8d\xe2\x80\x8d \xe2\x80\x8d 类型的方法,time.Duration并在打印时使用h, s, m, \xc2\xb5,n符号持续时间,这里是方法的文档String

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

包中有一些预定义的常量time,您可以使用它们将持续时间变量转换为您首选的时间单位,例如:

\n
latencyInMicroSeconds := int64(jitter / time.Microsecond)\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n