-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
202 lines (175 loc) · 4.66 KB
/
main.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Package human_duration provides human readable output of
// time.Duration
package human_duration
import (
"fmt"
"math"
"regexp"
"strings"
"time"
)
// Available precisions
const (
Second = "second"
Minute = "minute"
Hour = "hour"
Day = "day"
Week = "week"
Year = "year"
)
func precisionToDuration(precision string) time.Duration {
switch precision {
case Second:
return time.Second
case Minute:
return time.Minute
case Hour:
return time.Hour
case Day:
return time.Hour * 24
case Week:
return time.Hour * 24 * 7
case Year:
return time.Hour * 24 * 365
default:
return time.Nanosecond
}
}
// String converts duration to human readable format, according to precision.
func String(duration time.Duration, precision string) string {
return StringCeiling(duration, precision, "")
}
func StringCeiling(duration time.Duration, precision, ceiling string) string {
return StringCeilingPadded(duration, precision, ceiling, false)
}
func Ago(t time.Time) string {
ago := String(time.Now().Sub(t), "minute") + " ago"
return ago
}
func AboutAgo(t time.Time, precision string) string {
ago := String(time.Now().Sub(t), precision) + " ago"
return ago
}
type chunk struct {
singularName string
amount int64
}
func StringCeilingPadded(duration time.Duration, precision, ceiling string, padded bool) string {
years := int64(duration.Hours() / 24 / 365)
weeks := int64(math.Mod(float64(int64(duration.Hours()/24/7)), 52))
days := int64(math.Mod(float64(int64(duration.Hours()/24)), 365)) - (weeks * 7)
hours := int64(math.Mod(duration.Hours(), 24))
minutes := int64(math.Mod(duration.Minutes(), 60))
seconds := int64(math.Mod(duration.Seconds(), 60))
switch ceiling {
case Second:
seconds = int64(duration.Seconds())
minutes = 0
hours = 0
days = 0
years = 0
case Minute:
minutes = int64(duration.Minutes())
hours = 0
days = 0
years = 0
case Hour:
hours = int64(duration.Hours())
days = 0
years = 0
case Day:
days = int64(float64(int64(duration.Hours() / 24)))
years = 0
weeks = 0
case Week:
weeks = int64(float64(int64(duration.Hours() / 24 / 7)))
years = 0
}
chunks := []chunk{
{"year", years},
{"week", weeks},
{"day", days},
{"hour", hours},
{"minute", minutes},
{"second", seconds},
}
parts := []string{}
preciseEnough := false
isLeading := true
unpaddedNumberFormat := "%d"
paddedNumberFormat := "%02d"
hitSomething := false
var targetChunk chunk
for _, chunk := range chunks {
if preciseEnough {
continue
}
if chunk.amount != 0 {
hitSomething = true
}
if chunk.singularName == precision || chunk.singularName+"s" == precision {
targetChunk = chunk
preciseEnough = true
}
numberFormat := unpaddedNumberFormat
if chunk.amount > 0 && isLeading {
isLeading = false
} else if padded {
numberFormat = paddedNumberFormat
}
switch chunk.amount {
case 0:
continue
case 1:
parts = append(parts, fmt.Sprintf(numberFormat+" %s", chunk.amount, chunk.singularName))
default:
parts = append(parts, fmt.Sprintf(numberFormat+" %ss", chunk.amount, chunk.singularName))
}
}
if !hitSomething {
return "less than a " + targetChunk.singularName
}
return strings.Join(parts, " ")
}
// ShortString converts duration to a shortened human readable format, according to precision.
func ShortString(duration time.Duration, precision string) string {
str := String(duration, precision)
str = shorten(str)
return str
}
func shorten(str string) string {
str = strings.Replace(str, " ", "", -1)
str = strings.Replace(str, "years", "y", 1)
str = strings.Replace(str, "year", "y", 1)
str = strings.Replace(str, "days", "d", 1)
str = strings.Replace(str, "day", "d", 1)
str = strings.Replace(str, "weeks", "w", 1)
str = strings.Replace(str, "week", "w", 1)
str = strings.Replace(str, "hours", "h", 1)
str = strings.Replace(str, "hour", "h", 1)
str = strings.Replace(str, "minutes", "m", 1)
str = strings.Replace(str, "minute", "m", 1)
str = strings.Replace(str, "seconds", "s", 1)
str = strings.Replace(str, "second", "s", 1)
return str
}
var trailingColon = regexp.MustCompile(`:$`)
// Timestamp converts duration to a common timestamp format, often used for videos.
func Timestamp(interval time.Duration, precision string) string {
if precisionToDuration(precision) > time.Hour {
precision = "hours"
}
str := StringCeilingPadded(interval, precision, "hour", true)
if str == "less than a "+precision {
return "0:00"
}
str = shorten(str)
str = strings.Replace(str, "h", ":", 1)
str = strings.Replace(str, "m", ":", 1)
str = strings.Replace(str, "s", "", 1)
str = trailingColon.ReplaceAllString(str, "")
if !strings.Contains(str, ":") {
str = "0:" + str
}
return str
}