-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.go
145 lines (116 loc) · 2.5 KB
/
time.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
package frain
import (
"errors"
"fmt"
"time"
)
var (
// ErrFutureTime is an error that is thrown when the reference time is greater than
// the supposedly future time to check
ErrFutureTime = errors.New("reference time argument is in the future")
)
// Clock basically stores the essential time.Time information excluding milliseconds
type Clock struct {
Second int
Minute int
Hour int
Day int
Month int
Year int
}
// TimeAgo takes a time.Time construct from the past and calculates the difference between it (t) and
// the reference time (ref) and then appending 'ago' at the end
func TimeAgo(ref, t time.Time) (string, error) {
if ref.After(t) {
return "", ErrFutureTime
}
elapsed := TimeDiff(t, ref)
unit := "years"
if elapsed.Year == 0 {
unit = "months"
if elapsed.Month == 0 {
unit = "days"
if elapsed.Day == 0 {
unit = "hours"
if elapsed.Hour == 0 {
unit = "minutes"
if elapsed.Minute == 0 {
unit = "seconds"
if elapsed.Second == 1 {
unit = "second"
}
return fmt.Sprintf("%d %s ago", elapsed.Second, unit), nil
}
if elapsed.Minute == 1 {
unit = "minute"
}
return fmt.Sprintf("%d %s ago", elapsed.Minute, unit), nil
}
if elapsed.Hour == 1 {
unit = "hour"
}
return fmt.Sprintf("%d %s ago", elapsed.Hour, unit), nil
}
if elapsed.Day == 1 {
unit = "day"
}
return fmt.Sprintf("%d %s ago", elapsed.Day, unit), nil
}
if elapsed.Month == 1 {
unit = "month"
}
return fmt.Sprintf("%d %s ago", elapsed.Month, unit), nil
}
if elapsed.Year == 1 {
unit = "year"
}
return fmt.Sprintf("%d %s ago", elapsed.Year, unit), nil
}
// TimeDiff finds the time elapsed between two time.Time types
func TimeDiff(t1, t2 time.Time) Clock {
// set same location
if t1.Location() != t2.Location() {
t1 = t1.In(t2.Location())
}
if t2.After(t1) {
t1, t2 = t2, t1
}
h1, m1, s1 := t1.Clock()
h2, m2, s2 := t2.Clock()
y1, M1, d1 := t1.Date()
y2, M2, d2 := t2.Date()
seconds := s1 - s2
minutes := m1 - m2
hours := h1 - h2
days := d1 - d2
months := int(M1 - M2)
years := y1 - y2
if seconds < 0 {
seconds += 60
minutes--
}
if minutes < 0 {
minutes += 60
hours--
}
if hours < 0 {
hours += 24
days--
}
if days < 0 {
days += time.Date(y1, M1, 0, 0, 0, 0, 0, time.UTC).Day()
months--
}
if months < 0 {
months += 12
years--
}
return Clock{
Second: seconds,
Minute: minutes,
Hour: hours,
Day: days,
Month: months,
Year: years,
}
}