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()
}
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.
A naïve implementation might be: https://play.golang.org/p/VsVeIhoLGi
(Note the assumption on the day duration constant).