-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.go
151 lines (138 loc) · 3.51 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
package date
import (
"errors"
"fmt"
"strings"
"time"
)
type Format string
const (
RFC2822 Format = "rfc2822"
ISO8601 Format = "iso8601"
Relative Format = "relative"
)
var now = time.Now
func SetNow(f func() time.Time) {
if f == nil {
panic("nil function")
}
now = f
}
const iso8601Layout = "2006-01-02 15:04:05 Z0700"
func FormatWithType(t time.Time, f Format) string {
switch f {
case RFC2822:
return FormatRFC2822(t)
case ISO8601:
return FormatISO8601(t)
case Relative:
return FormatRelative(t)
default:
return "format " + string(f) + " not supported"
}
}
// Format timestamps in a ISO 8601-like format (same as in Git command)
func FormatISO8601(t time.Time) string {
return t.Format(iso8601Layout)
}
// Format timestamps in RFC 2822 format, often found in email messages (same as in Git command)
func FormatRFC2822(t time.Time) string {
return t.Format(time.RFC1123Z)
}
func FormatRelative(t time.Time) string {
var (
timePassed = time.Since(t)
seconds = int(timePassed.Seconds())
minutes = int(timePassed.Minutes())
hours = int(timePassed.Hours() + 0.5)
)
switch {
case seconds < 1:
return "Less than a second ago"
case seconds == 1:
return "1 second ago"
case seconds < 60:
return fmt.Sprintf("%d seconds ago", seconds)
case minutes == 1:
return "About a minute ago"
case minutes < 60:
return fmt.Sprintf("%d minutes ago", minutes)
case hours == 1:
return "About an hour ago"
case hours < 48:
return fmt.Sprintf("%d hours ago", hours)
case hours < 24*7*2:
return fmt.Sprintf("%d days ago", hours/24)
case hours < 24*30*2:
return fmt.Sprintf("%d weeks ago", hours/24/7)
case hours < 24*365*2:
return fmt.Sprintf("%d months ago", hours/24/30)
default:
return fmt.Sprintf("%d years ago", int(timePassed.Hours())/24/365)
}
}
func ParseAbsolute(value string) (time.Time, error) {
t, err := time.Parse(time.RFC3339Nano, value)
if err == nil {
return t, nil
}
t, err = time.Parse(time.RFC1123Z, value)
if err == nil {
return t, nil
}
t, err = time.Parse(iso8601Layout, value)
if err == nil {
return t, nil
}
t, err = time.ParseInLocation("2006-01-02", value, time.Local)
if err == nil {
return t, nil
}
return time.Parse(time.UnixDate, value)
}
func Parse(value string) (time.Time, error) {
t, err := ParseAbsolute(value)
if err == nil {
return t, nil
}
var amount time.Duration
var unit string
_, err = fmt.Sscanf(value, "%d %s ago", &amount, &unit)
if err == nil {
unit = strings.ToLower(unit)
switch unit {
case "seconds", "second":
return now().Add(-time.Second * amount), nil
case "minutes", "minute":
return now().Add(-time.Minute * amount), nil
case "hours", "hour":
return now().Add(-time.Hour * amount), nil
case "days", "day":
return now().Add(-time.Hour * 24 * amount), nil
case "weeks", "week":
return now().Add(-time.Hour * 24 * 7 * amount), nil
case "months", "month":
return now().Add(-time.Hour * 30 * 24 * 7 * amount), nil
case "years", "year":
return now().Add(-time.Hour * 365 * 30 * 24 * 7 * amount), nil
}
}
if value == "now" {
return now(), nil
}
if value == "today" {
return midnight(now()), nil
}
if value == "yesterday" {
yesterday := now().AddDate(0, 0, -1)
return midnight(yesterday), nil
}
if value == "tomorrow" {
tomorrow := now().AddDate(0, 0, 1)
return midnight(tomorrow), nil
}
return time.Time{}, errors.New("not supported date format: " + value)
}
func midnight(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
}