forked from scouter-contrib/scouter-agent-golang
/
timeutil.go
31 lines (25 loc) · 870 Bytes
/
timeutil.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package util
import (
"fmt"
"time"
)
var secondsPerMin = int64(60)
var secondsPerHour = int64(60 * secondsPerMin)
var secondsPerDay = int64(24 * secondsPerHour)
//GetDuration returns long to string format
func GetDuration(seconds int64) string {
day := seconds / secondsPerDay
hour := (seconds - (day * secondsPerDay)) / secondsPerHour
min := (seconds - (day * secondsPerDay) - (hour * secondsPerHour)) / secondsPerMin
secs := (seconds - (day * secondsPerDay) - (hour * secondsPerHour) - (min * secondsPerMin))
return fmt.Sprintf("%dD %dH %dM %dS", day, hour, min, secs)
}
func MillisBetween(from time.Time, to time.Time) int32 {
return int32(to.Sub(from).Milliseconds())
}
func MillisToNow(from time.Time) int32 {
return int32(time.Now().Sub(from).Milliseconds())
}
func TimeToMillis(t time.Time) int64 {
return t.UnixNano() / int64(time.Millisecond)
}