-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimediff.go
202 lines (171 loc) · 5.44 KB
/
timediff.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 timediff
import (
"errors"
"fmt"
"math"
"slices"
"time"
)
type DateDiff struct {
Years int
Months int
Weeks int
Days int
Hours int
Minutes int
Seconds int
Milliseconds int
Nanoseconds int
}
type DateTime struct {
time.Time
}
func (t DateTime) Diff(endDate time.Time, running bool, units []string) (DateDiff, error) {
if endDate.Before(t.Time) {
return DateDiff{}, errors.New("End date before start date")
}
runningDiff := DateDiff{}
if slices.Contains(units, "years") {
diff, _ := t.DiffYears(endDate)
runningDiff.Years = diff.Years
if running {
endDate = endDate.AddDate(-diff.Years, 0, 0)
}
}
if slices.Contains(units, "months") {
diff, _ := t.DiffMonths(endDate)
runningDiff.Months = diff.Months
if running {
endDate = endDate.AddDate(0, -diff.Months, 0)
}
}
if slices.Contains(units, "weeks") {
diff, _ := t.DiffWeeks(endDate)
runningDiff.Weeks = diff.Weeks
if running {
endDate = endDate.AddDate(0, 0, -(diff.Weeks * 7))
}
}
if slices.Contains(units, "days") {
diff, _ := t.DiffDays(endDate)
runningDiff.Days = diff.Days
if running {
endDate = endDate.AddDate(0, 0, -diff.Days)
}
}
if slices.Contains(units, "hours") {
diff, _ := t.DiffHours(endDate)
runningDiff.Hours = diff.Hours
if running {
duration, _ := time.ParseDuration(fmt.Sprintf("-%dh%dns", diff.Hours, diff.Nanoseconds))
endDate = endDate.Add(duration)
}
}
if slices.Contains(units, "minutes") {
diff, _ := t.DiffMinutes(endDate)
runningDiff.Minutes = diff.Minutes
if running {
duration, _ := time.ParseDuration(fmt.Sprintf("-%dm%dns", diff.Minutes, diff.Nanoseconds))
endDate = endDate.Add(duration)
}
}
if slices.Contains(units, "seconds") {
diff, _ := t.DiffSeconds(endDate)
runningDiff.Seconds = diff.Seconds
if running {
duration, _ := time.ParseDuration(fmt.Sprintf("-%ds%dns", diff.Seconds, diff.Nanoseconds))
endDate = endDate.Add(duration)
}
}
if slices.Contains(units, "milliseconds") {
diff, _ := t.DiffMilliseconds(endDate)
runningDiff.Milliseconds = diff.Milliseconds
if running {
duration, _ := time.ParseDuration(fmt.Sprintf("-%dms%dns", diff.Milliseconds, diff.Nanoseconds))
endDate = endDate.Add(duration)
}
}
if slices.Contains(units, "nanoseconds") {
diff := int(endDate.Sub(t.Time))
runningDiff.Nanoseconds = diff
}
return runningDiff, nil
}
func (t DateTime) DiffYears(endDate time.Time) (DateDiff, error) {
if endDate.Before(t.Time) {
return DateDiff{}, errors.New("End date before start date")
}
yearDiff := endDate.Year() - t.Year()
remainder := endDate.AddDate(-yearDiff, 0, 0).Sub(t.Time)
if remainder < 0 {
yearDiff--
remainder = endDate.AddDate(-yearDiff, 0, 0).Sub(t.Time)
}
return DateDiff{Years: yearDiff, Nanoseconds: int(remainder)}, nil
}
func (t DateTime) DiffMonths(endDate time.Time) (DateDiff, error) {
if endDate.Before(t.Time) {
return DateDiff{}, errors.New("End date before start date")
}
monthDiff := (endDate.Year()-t.Year())*12 + int(endDate.Month()) - int(t.Month())
remainder := endDate.AddDate(0, -monthDiff, 0).Sub(t.Time)
if remainder < 0 {
monthDiff--
remainder = endDate.AddDate(0, -monthDiff, 0).Sub(t.Time)
}
return DateDiff{Months: monthDiff, Nanoseconds: int(remainder)}, nil
}
func (t DateTime) DiffWeeks(endDate time.Time) (DateDiff, error) {
if endDate.Before(t.Time) {
return DateDiff{}, errors.New("End date before start date")
}
dateSub := endDate.Sub(t.Time)
remainder := int(dateSub) - int(dateSub.Nanoseconds())
weekDiff := int(math.Floor(dateSub.Hours())) / 24 / 7
return DateDiff{Weeks: weekDiff, Nanoseconds: remainder}, nil
}
func (t DateTime) DiffDays(endDate time.Time) (DateDiff, error) {
if endDate.Before(t.Time) {
return DateDiff{}, errors.New("End date before start date")
}
dateSub := endDate.Sub(t.Time)
remainder := int(dateSub) - int(dateSub.Nanoseconds())
dayDiff := int(math.Floor(dateSub.Hours())) / 24
return DateDiff{Days: dayDiff, Nanoseconds: remainder}, nil
}
func (t DateTime) DiffHours(endDate time.Time) (DateDiff, error) {
if endDate.Before(t.Time) {
return DateDiff{}, errors.New("End date before start date")
}
dateSub := endDate.Sub(t.Time)
remainder := int(dateSub) - int(dateSub.Nanoseconds())
hourDiff := int(math.Floor(dateSub.Hours()))
return DateDiff{Hours: hourDiff, Nanoseconds: remainder}, nil
}
func (t DateTime) DiffMinutes(endDate time.Time) (DateDiff, error) {
if endDate.Before(t.Time) {
return DateDiff{}, errors.New("End date before start date")
}
dateSub := endDate.Sub(t.Time)
remainder := int(dateSub) - int(dateSub.Nanoseconds())
minuteDiff := int(math.Floor(dateSub.Minutes()))
return DateDiff{Minutes: minuteDiff, Nanoseconds: remainder}, nil
}
func (t DateTime) DiffSeconds(endDate time.Time) (DateDiff, error) {
if endDate.Before(t.Time) {
return DateDiff{}, errors.New("End date before start date")
}
dateSub := endDate.Sub(t.Time)
remainder := int(dateSub) - int(dateSub.Nanoseconds())
secondDiff := int(math.Floor(dateSub.Seconds()))
return DateDiff{Seconds: secondDiff, Nanoseconds: remainder}, nil
}
func (t DateTime) DiffMilliseconds(endDate time.Time) (DateDiff, error) {
if endDate.Before(t.Time) {
return DateDiff{}, errors.New("End date before start date")
}
dateSub := endDate.Sub(t.Time)
remainder := int(dateSub) - int(dateSub.Nanoseconds())
millisecondDiff := int(dateSub.Milliseconds())
return DateDiff{Milliseconds: millisecondDiff, Nanoseconds: remainder}, nil
}