doujingke4981 2017-02-22 12:44
浏览 208
已采纳

时间,以天,小时,分钟,秒的格式表示

Is there a way to format the output of time.Since() to add days when aplicable, something like:

[days]d[hours]h[minutes]m[seconds]s

The current format seems to use only hours as the maximum unit:

start := time.Unix(1411691219, 0)
diff := time.Since(start)

21132h9m41.714117301s

But I would like to use days instead of hours, in order to obtain something like:

880d12h9m41.7s

I am currently using the following TimeDiff function to produce the desired output, but wondering if there is an easy/better/native way of achieving this.

package main

import (
    "bytes"
    "fmt"
    "math"
    "time"
)

func main() {
    start := time.Unix(1411691219, 0)
    diff := time.Since(start)
    fmt.Printf("diff = %s
", diff)
    fmt.Printf("diff = %s
", TimeDiff(start))
}

func TimeDiff(t time.Time) string {
    diff := time.Since(t)
    days := diff / (24 * time.Hour)
    hours := diff % (24 * time.Hour)
    minutes := hours % time.Hour
    seconds := math.Mod(minutes.Seconds(), 60)
    var buffer bytes.Buffer
    if days > 0 {
        buffer.WriteString(fmt.Sprintf("%dd", days))
    }
    if hours/time.Hour > 0 {
        buffer.WriteString(fmt.Sprintf("%dh", hours/time.Hour))
    }
    if minutes/time.Minute > 0 {
        buffer.WriteString(fmt.Sprintf("%dm", minutes/time.Minute))
    }
    if seconds > 0 {
        buffer.WriteString(fmt.Sprintf("%.1fs", seconds))
    }
    return buffer.String()
}
  • 写回答

2条回答 默认 最新

  • dpq59734 2017-02-22 16:52
    关注

    To answer your exact question:

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func FormatSince(t time.Time) string {
        const (
            Decisecond = 100 * time.Millisecond
            Day        = 24 * time.Hour
        )
        ts := time.Since(t)
        sign := time.Duration(1)
        if ts < 0 {
            sign = -1
            ts = -ts
        }
        ts += +Decisecond / 2
        d := sign * (ts / Day)
        ts = ts % Day
        h := ts / time.Hour
        ts = ts % time.Hour
        m := ts / time.Minute
        ts = ts % time.Minute
        s := ts / time.Second
        ts = ts % time.Second
        f := ts / Decisecond
        return fmt.Sprintf("%dd%dh%dm%d.%ds", d, h, m, s, f)
    }
    

    Output:

    diff = 21136h26m58.574146433s
    diff = 880d16h26m58.6s
    

    For Go 1.8 and earlier, this gives you the difference in absolute (UTC) time. It may not match the difference in wall clock time which can be affected by daylight saving time. It may also be affected by adjustments to the system wall (real-time) clock.

    From Go 1.9 onward, the difference will be computed using a monotonic clock which is not affected by changes to the system wall clock time or daylight savings time.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了