-
Notifications
You must be signed in to change notification settings - Fork 203
/
date.go
359 lines (331 loc) · 9.75 KB
/
date.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package templates
import (
"bytes"
"regexp"
"strconv"
"strings"
"time"
)
var marchMayChecker = regexp.MustCompile("M([^a]|$)")
func generateTimeAgo(date *time.Time) []byte {
timeAgo := time.Now().Sub(*date)
if timeAgo.Minutes() < 1 {
return []byte("a few seconds ago")
}
if timeAgo.Minutes() < 2 {
return []byte("a minute ago")
}
if timeAgo.Minutes() < 60 {
var buffer bytes.Buffer
buffer.WriteString(strconv.Itoa(int(timeAgo.Minutes())))
buffer.WriteString(" minutes ago")
return buffer.Bytes()
}
if timeAgo.Hours() < 2 {
return []byte("an hour ago")
}
if timeAgo.Hours() < 24 {
var buffer bytes.Buffer
buffer.WriteString(strconv.Itoa(int(timeAgo.Hours())))
buffer.WriteString(" hours ago")
return buffer.Bytes()
}
if timeAgo.Hours() < 48 {
return []byte("a day ago")
}
days := int(timeAgo.Hours() / 24)
if days < 25 {
var buffer bytes.Buffer
buffer.WriteString(strconv.Itoa(days))
buffer.WriteString(" days ago")
return buffer.Bytes()
}
if days < 45 {
return []byte("a month ago")
}
if days < 345 {
months := days / 30
if months < 2 {
months = 2
}
var buffer bytes.Buffer
buffer.WriteString(strconv.Itoa(months))
buffer.WriteString(" months ago")
return buffer.Bytes()
}
if days < 548 {
return []byte("a year ago")
}
years := days / 365
if years < 2 {
years = 2
}
var buffer bytes.Buffer
buffer.WriteString(strconv.Itoa(years))
buffer.WriteString(" years ago")
return buffer.Bytes()
}
func formatDate(format string, date *time.Time) []byte {
// Do these first (so they don't accidentally replace something the others insert)
if strings.Contains(format, "h") {
format = strings.Replace(format, "h", replaceh(date), -1)
}
format = strings.Replace(format, "s", strconv.Itoa(date.Second()), -1)
// Year, month, and day
if strings.Contains(format, "Do") {
format = strings.Replace(format, "Do", replaceDo(date), -1)
}
format = strings.Replace(format, "YYYY", strconv.Itoa(date.Year()), -1)
if date.Year() > 99 {
format = strings.Replace(format, "YY", strconv.Itoa(date.Year())[2:], -1)
}
format = strings.Replace(format, "Q", strconv.Itoa(((int(date.Month())-1)/3)+1), -1)
if strings.Contains(format, "DDDD") {
format = strings.Replace(format, "DDDD", replaceDDDD(date), -1)
}
if strings.Contains(format, "DDD") {
format = strings.Replace(format, "DDD", replaceDDD(date), -1)
}
if strings.Contains(format, "DD") {
format = strings.Replace(format, "DD", replaceDD(date), -1)
}
format = strings.Replace(format, "X", strconv.FormatInt(date.Unix(), 10), -1)
// Unix ms ('x') is not used by ghost. Excluding it for now.
// format = strings.Replace(format, "x", strconv.FormatInt((date.UnixNano()/1000000), 10), -1)
// Locale formats. Not supported yet
format = strings.Replace(format, "gggg", strconv.Itoa(date.Year()), -1)
if date.Year() > 99 {
format = strings.Replace(format, "gg", strconv.Itoa(date.Year())[2:], -1)
}
if strings.Contains(format, "ww") {
format = strings.Replace(format, "ww", replaceww(date), -1)
}
if strings.Contains(format, "w") {
format = strings.Replace(format, "w", replacew(date), -1)
}
format = strings.Replace(format, "e", strconv.Itoa(int(date.Weekday())), -1)
// ISO week date formats. Not supported yet - https://en.wikipedia.org/wiki/ISO_week_date
format = strings.Replace(format, "GGGG", strconv.Itoa(date.Year()), -1)
if date.Year() > 99 {
format = strings.Replace(format, "GG", strconv.Itoa(date.Year())[2:], -1)
}
if strings.Contains(format, "WW") {
format = strings.Replace(format, "WW", replaceww(date), -1)
}
if strings.Contains(format, "W") {
format = strings.Replace(format, "W", replacew(date), -1)
}
format = strings.Replace(format, "E", strconv.Itoa(int(date.Weekday())), -1)
// Hour, minute, second, millisecond, and offset
if strings.Contains(format, "HH") {
format = strings.Replace(format, "HH", replaceHH(date), -1)
}
format = strings.Replace(format, "H", strconv.Itoa(date.Hour()), -1)
if strings.Contains(format, "hh") {
format = strings.Replace(format, "hh", replacehh(date), -1)
}
if strings.Contains(format, "a") {
format = strings.Replace(format, "a", replacea(date), -1)
}
if strings.Contains(format, "A") {
format = strings.Replace(format, "A", replaceA(date), -1)
}
if strings.Contains(format, "mm") {
format = strings.Replace(format, "mm", replacemm(date), -1)
}
format = strings.Replace(format, "m", strconv.Itoa(date.Minute()), -1)
if strings.Contains(format, "ss") {
format = strings.Replace(format, "ss", replacess(date), -1)
}
format = strings.Replace(format, "SSS", strconv.Itoa(date.Nanosecond()/1000000), -1)
format = strings.Replace(format, "SS", strconv.Itoa(date.Nanosecond()/10000000), -1)
format = strings.Replace(format, "S", strconv.Itoa(date.Nanosecond()/100000000), -1)
if strings.Contains(format, "ZZ") {
format = strings.Replace(format, "ZZ", replaceZZ(date), -1)
}
if strings.Contains(format, "Z") {
format = strings.Replace(format, "Z", replaceZ(date), -1)
}
// Not documented for moment.js, but seems to be used by ghost themes
if strings.Contains(format, "dddd") {
format = strings.Replace(format, "dddd", date.Weekday().String(), -1)
}
// This needs to be last so that month strings don't interfere with the other replace functions
format = strings.Replace(format, "MMMM", date.Month().String(), -1)
if len(date.Month().String()) > 2 {
format = strings.Replace(format, "MMM", date.Month().String()[:3], -1)
}
if strings.Contains(format, "MM") {
format = strings.Replace(format, "MM", replaceMM(date), -1)
}
// Replace M - make sure the Ms in March and May don't get replaced. TODO: Regex could be improved, only recognizes 'M's that are not followed by 'a's.
format = marchMayChecker.ReplaceAllString(format, strconv.Itoa(int(date.Month())))
return []byte(format)
}
func replaceMM(date *time.Time) string {
var buffer bytes.Buffer
month := int(date.Month())
if month < 10 {
buffer.WriteString("0")
buffer.WriteString(strconv.Itoa(month))
} else {
buffer.WriteString(strconv.Itoa(month))
}
return buffer.String()
}
func replaceDDDD(date *time.Time) string {
var buffer bytes.Buffer
startOfYear := time.Date((date.Year() - 1), time.December, 31, 0, 0, 0, 0, time.UTC)
days := int(date.Sub(startOfYear) / (24 * time.Hour))
if days < 10 {
buffer.WriteString("00")
} else if days < 100 {
buffer.WriteString("0")
}
buffer.WriteString(strconv.Itoa(days))
return buffer.String()
}
func replaceDDD(date *time.Time) string {
startOfYear := time.Date((date.Year() - 1), time.December, 31, 0, 0, 0, 0, time.UTC)
return strconv.Itoa(int(date.Sub(startOfYear) / (24 * time.Hour)))
}
func replaceDD(date *time.Time) string {
var buffer bytes.Buffer
if date.Day() < 10 {
buffer.WriteString("0")
buffer.WriteString(strconv.Itoa(date.Day()))
} else {
buffer.WriteString(strconv.Itoa(date.Day()))
}
return buffer.String()
}
func replaceDo(date *time.Time) string {
var buffer bytes.Buffer
buffer.WriteString(strconv.Itoa(date.Day()))
if date.Day() == 1 || date.Day() == 21 || date.Day() == 31 {
buffer.WriteString("st")
} else if date.Day() == 2 || date.Day() == 22 {
buffer.WriteString("nd")
} else if date.Day() == 3 || date.Day() == 23 {
buffer.WriteString("rd")
} else {
buffer.WriteString("th")
}
return buffer.String()
}
func replaceww(date *time.Time) string {
var buffer bytes.Buffer
startOfYear := time.Date((date.Year() - 1), time.December, 25, 0, 0, 0, 0, time.UTC)
weeks := int(date.Sub(startOfYear) / (24 * time.Hour * 7))
if weeks < 10 {
buffer.WriteString("0")
}
buffer.WriteString(strconv.Itoa(weeks))
return buffer.String()
}
func replacew(date *time.Time) string {
startOfYear := time.Date((date.Year() - 1), time.December, 25, 0, 0, 0, 0, time.UTC)
return strconv.Itoa(int(date.Sub(startOfYear) / (24 * time.Hour * 7)))
}
func replaceHH(date *time.Time) string {
var buffer bytes.Buffer
hour := date.Hour()
if hour < 10 {
buffer.WriteString("0")
}
buffer.WriteString(strconv.Itoa(hour))
return buffer.String()
}
func replacehh(date *time.Time) string {
var buffer bytes.Buffer
hour := date.Hour()
if hour == 0 {
hour = 12
} else if hour > 12 {
hour = hour - 12
}
if hour < 10 {
buffer.WriteString("0")
}
buffer.WriteString(strconv.Itoa(hour))
return buffer.String()
}
func replaceh(date *time.Time) string {
var buffer bytes.Buffer
hour := date.Hour()
if hour == 0 {
hour = 12
} else if hour > 12 {
hour = hour - 12
}
buffer.WriteString(strconv.Itoa(hour))
return buffer.String()
}
func replacea(date *time.Time) string {
if date.Hour() < 12 {
return "am"
}
return "pm"
}
func replaceA(date *time.Time) string {
if date.Hour() < 12 {
return "AM"
}
return "PM"
}
func replacemm(date *time.Time) string {
var buffer bytes.Buffer
minute := date.Minute()
if minute < 10 {
buffer.WriteString("0")
}
buffer.WriteString(strconv.Itoa(minute))
return buffer.String()
}
func replacess(date *time.Time) string {
var buffer bytes.Buffer
second := date.Second()
if second < 10 {
buffer.WriteString("0")
}
buffer.WriteString(strconv.Itoa(second))
return buffer.String()
}
func replaceZZ(date *time.Time) string {
var buffer bytes.Buffer
_, offset := date.In(time.Local).Zone()
offset = offset / 3600
if offset > 0 {
buffer.WriteString("+")
} else {
buffer.WriteString("-")
}
if offset < 0 {
offset = -offset
}
if offset < 10 {
buffer.WriteString("0")
}
buffer.WriteString(strconv.Itoa(offset))
buffer.WriteString("00")
return buffer.String()
}
func replaceZ(date *time.Time) string {
var buffer bytes.Buffer
_, offset := date.In(time.Local).Zone()
offset = offset / 3600
if offset > 0 {
buffer.WriteString("+")
} else {
buffer.WriteString("-")
}
if offset < 0 {
offset = -offset
}
if offset < 10 {
buffer.WriteString("0")
}
buffer.WriteString(strconv.Itoa(offset))
buffer.WriteString(":00")
return buffer.String()
}