dougong9987 2017-01-06 10:33
浏览 1932
已采纳

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

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.

  • 写回答

2条回答 默认 最新

  • doudun3040 2017-01-06 10:42
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器