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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)