时间已过去的天数、小时、分钟和秒的格式。

huangapple go评论84阅读模式
英文:

time since in days, hours, minutes, seconds format

问题

有没有一种方法可以格式化 time.Since() 的输出,以添加适用的天数,类似于:

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

当前的格式似乎只使用小时作为最大单位:

start := time.Unix(1411691219, 0)
diff := time.Since(start)
  
21132h9m41.714117301s

但我想使用天数而不是小时,以获得类似于:

880d12h9m41.7s

我目前正在使用以下的 TimeDiff 函数来生成所需的输出,但想知道是否有一种简单/更好/更本地的方法来实现这一点。

package main

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

func main() {
	start := time.Unix(1411691219, 0)
	diff := time.Since(start)
	fmt.Printf("diff = %s\n", diff)
	fmt.Printf("diff = %s\n", 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()
}
英文:

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\n", diff)
	fmt.Printf("diff = %s\n", 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()
}

答案1

得分: 0

一个天真的实现可能是:https://play.golang.org/p/VsVeIhoLGi

(注意对于天数持续时间常量的假设)。

英文:

A naïve implementation might be: https://play.golang.org/p/VsVeIhoLGi

(Note the assumption on the day duration constant).

答案2

得分: -1

回答你的具体问题:

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

输出结果:

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

对于Go 1.8及更早版本,这将给出绝对(UTC)时间的差异。它可能与墙上时钟时间的差异不匹配,因为墙上时钟时间可能受夏令时的影响。它还可能受到对系统墙(实时)时钟的调整的影响。

从Go 1.9开始,差异将使用单调时钟计算,该时钟不受系统墙时钟时间或夏令时的影响。

英文:

To answer your exact question:

package main

import (
	&quot;fmt&quot;
	&quot;time&quot;
)

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 &lt; 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(&quot;%dd%dh%dm%d.%ds&quot;, 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.

huangapple
  • 本文由 发表于 2017年2月22日 20:44:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/42391869.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定