-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdifference.go
170 lines (147 loc) · 5.15 KB
/
difference.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
package carbon
import (
"time"
)
// Get the difference in nanoseconds using timestamps.
func (c *Carbon) DiffInRealNanoseconds(v interface{}, absolute bool) int64 {
diff := c.resolveCarbon(v).UnixNano() - c.UnixNano()
diff = absValueIf(diff, absolute)
return diff
}
// Get the difference in microseconds using timestamps.
func (c *Carbon) DiffInRealMicroseconds(v interface{}, absolute bool) int64 {
diff := c.DiffInRealNanoseconds(v, absolute)
return diff / int64(time.Microsecond)
}
// Get the difference in milliseconds rounded down using timestamps.
func (c *Carbon) DiffInRealMilliseconds(v interface{}, absolute bool) int64 {
diff := c.DiffInRealNanoseconds(v, absolute)
return diff / int64(time.Millisecond)
}
// Get the difference in seconds using timestamps.
func (c *Carbon) DiffInRealSeconds(v interface{}, absolute bool) int64 {
diff := c.resolveCarbon(v).Timestamp() - c.Timestamp()
diff = absValueIf(diff, absolute)
return diff
}
// Get the difference in nanoseconds.
func (c *Carbon) DiffInNanoseconds(v interface{}, absolute bool) int64 {
diff := int64(c.resolveCarbon(v).Sub(c.Time))
diff = absValueIf(diff, absolute)
return diff
}
// Get the difference in microseconds.
func (c *Carbon) DiffInMicroseconds(v interface{}, absolute bool) int64 {
return c.DiffInNanoseconds(v, absolute) / int64(time.Microsecond)
}
// Get the difference in milliseconds rounded down.
func (c *Carbon) DiffInMilliseconds(v interface{}, absolute bool) int64 {
return c.DiffInNanoseconds(v, absolute) / int64(time.Millisecond)
}
// Get the difference in seconds rounded down.
func (c *Carbon) DiffInSeconds(v interface{}, absolute bool) int64 {
return c.DiffInNanoseconds(v, absolute) / int64(time.Second)
}
// Get the difference in minutes rounded down.
func (c *Carbon) DiffInMinutes(v interface{}, absolute bool) int64 {
return c.DiffInSeconds(v, absolute) / SecondsPerMinute
}
// Get the difference in minutes rounded down using timestamps.
func (c *Carbon) DiffInRealMinutes(v interface{}, absolute bool) int64 {
return c.DiffInRealSeconds(v, absolute) / SecondsPerMinute
}
// Get the difference in hours rounded down.
func (c *Carbon) DiffInHours(v interface{}, absolute bool) int64 {
return c.DiffInMinutes(v, absolute) / MinutesPerHour
}
// Get the difference in hours rounded down using timestamps.
func (c *Carbon) DiffInRealHours(v interface{}, absolute bool) int64 {
return c.DiffInRealMinutes(v, absolute) / MinutesPerHour
}
// Get the difference in days rounded down.
func (c *Carbon) DiffInDays(v interface{}, absolute bool) int64 {
return c.DiffInHours(v, absolute) / HoursPerDay
}
// Get the difference in weeks rounded down.
func (c *Carbon) DiffInWeeks(v interface{}, absolute bool) int64 {
return c.DiffInDays(v, absolute) / DaysPerWeek
}
// Get the difference in years
func (c *Carbon) DiffInYears(v interface{}, absolute bool) int64 {
resolve := c.resolveCarbon(v)
if c.Year() == resolve.Year() {
return 0
}
start := NewCarbon(c.Time)
end := NewCarbon(resolve.Time)
swap := false
if end.UnixNano() < start.UnixNano() {
start, end = end, start
swap = true
}
diff := int64(end.Year() - start.Year() - 1)
start.SetYears(end.Year())
if start.UnixNano() <= end.UnixNano() {
diff++
}
if swap {
diff = -diff
}
diff = absValueIf(diff, absolute)
return diff
}
// Get the difference in months rounded down.
// TODO: Add more tests
func (c *Carbon) DiffInMonths(v interface{}, absolute bool) int64 {
resolve := c.resolveCarbon(v)
originalCopy := c.Copy()
resolveCopy := resolve.Copy()
diffMonths := int64(0)
if resolveCopy.Year() == originalCopy.Year() && resolveCopy.Month() != originalCopy.Month() {
diffMonths = int64(resolveCopy.Month() - originalCopy.Month())
if diffMonths > 0 {
if originalCopy.AddMonths(int(diffMonths)).GreaterThan(resolveCopy) {
diffMonths--
}
} else if diffMonths < 0 {
if resolveCopy.SubMonths(int(diffMonths)).GreaterThan(originalCopy) {
diffMonths++
}
}
}
if resolveCopy.Year() < originalCopy.Year() {
diffMonths = int64(MonthsPerYear - resolveCopy.Month() + originalCopy.Month() - 1)
if originalCopy.hasRemainingOneMonthHour(resolveCopy) {
diffMonths++
}
if resolve.LessThan(c) {
diffMonths = -diffMonths
}
}
if resolveCopy.Year() > originalCopy.Year() {
diffMonths = int64(MonthsPerYear - originalCopy.Month() + resolveCopy.Month() - 1)
if resolveCopy.hasRemainingOneMonthHour(originalCopy) {
diffMonths++
}
if resolve.LessThan(c) {
diffMonths = -diffMonths
}
}
diffMonths = diffMonths % MonthsPerYear
diffMonths = absValueIf(int64(diffMonths), absolute)
return c.DiffInYears(v, absolute) * MonthsPerYear + diffMonths
}
// Get the difference in quarters rounded down.
func (c *Carbon) DiffInQuarters(v interface{}, absolute bool) int64 {
return c.DiffInMonths(v, absolute) / MonthsPerQuarter
}
func (c *Carbon) hasRemainingOneMonthHour(resolve *Carbon) bool {
totalHour := int64(c.DaysInMonth() * HoursPerDay)
diff := c.Copy().StartOfMonth().DiffInHours(c, false)
remainHour := totalHour - diff
spendHour := resolve.Copy().StartOfMonth().DiffInHours(resolve, false)
if c.Month() == resolve.Month() {
return diff == spendHour
}
return remainHour + spendHour >= totalHour
}