-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.go
58 lines (48 loc) · 1.39 KB
/
util.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"fmt"
"strings"
"time"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
var (
printer = message.NewPrinter(language.English)
)
// TimeDifference will return the FormattedTime of time.Unix().Now() - lastBeat
func TimeDifference(lastBeat int64, t time.Time) string {
st := t.Sub(time.Unix(lastBeat, 0))
return FormattedTime(int64(st.Seconds()))
}
// FormattedTime will turn seconds into a pretty time representation
func FormattedTime(secondsIn int64) string {
hours := secondsIn / 3600
minutes := (secondsIn / 60) - (60 * hours)
seconds := secondsIn % 60
units := make([]string, 0)
if hours != 0 {
units = append(units, joinIntAndStr(hours, "hour"))
}
if minutes != 0 {
units = append(units, joinIntAndStr(minutes, "minute"))
}
if seconds != 0 || (hours == 0 && minutes == 0) {
units = append(units, joinIntAndStr(seconds, "second"))
}
return strings.Join(units, ", ")
}
// FormattedNum will insert commas as necessary in large numbers
func FormattedNum(num int64) string {
return printer.Sprintf("%d", num)
}
// FormattedUTCData will return the Unix time as a spreadsheet data string
func FormattedUTCData(unix int64) string {
return time.Unix(unix, 0).In(time.UTC).Format("2006/01/02 15:04")
}
func joinIntAndStr(int int64, str string) string {
plural := "s"
if int == 1 {
plural = ""
}
return fmt.Sprintf("%s %s%s", FormattedNum(int), str, plural)
}