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

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条)

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格