-
-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathextremum.go
More file actions
executable file
·168 lines (142 loc) · 3.71 KB
/
extremum.go
File metadata and controls
executable file
·168 lines (142 loc) · 3.71 KB
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
package carbon
import (
"time"
)
const (
minDuration Duration = -1 << 63
maxDuration Duration = 1<<63 - 1
)
// ZeroValue returns the zero value of Carbon instance.
func ZeroValue() *Carbon {
return MinValue()
}
// EpochValue returns the unix epoch value of Carbon instance.
func EpochValue() *Carbon {
return NewCarbon(time.Date(EpochYear, time.January, MinDay, MinHour, MinMinute, MinSecond, MinNanosecond, time.UTC))
}
// MaxValue returns the maximum value of Carbon instance.
func MaxValue() *Carbon {
return NewCarbon(time.Date(MaxYear, time.December, MaxDay, MaxHour, MaxMinute, MaxSecond, MaxNanosecond, time.UTC))
}
// MinValue returns the minimum value of Carbon instance.
func MinValue() *Carbon {
return NewCarbon(time.Date(MinYear, time.January, MinDay, MinHour, MinMinute, MinSecond, MinNanosecond, time.UTC))
}
// MaxDuration returns the maximum value of duration instance.
func MaxDuration() Duration {
return maxDuration
}
// MinDuration returns the minimum value of duration instance.
func MinDuration() Duration {
return minDuration
}
// Max returns the maximum Carbon instance from some given Carbon instances.
func Max(c1 *Carbon, c2 ...*Carbon) (c *Carbon) {
// If first carbon is invalid, return it immediately
if c1.IsInvalid() {
return c1
}
c = c1
// If no additional arguments, return the first one
if len(c2) == 0 {
return
}
// Check all additional arguments
for _, carbon := range c2 {
// If any carbon is invalid, return it immediately
if carbon.IsInvalid() {
return carbon
}
// Update maximum if current carbon is greater or equal
if carbon.Gte(c) {
c = carbon
}
}
return
}
// Min returns the minimum Carbon instance from some given Carbon instances.
func Min(c1 *Carbon, c2 ...*Carbon) (c *Carbon) {
// If first carbon is invalid, return it immediately
if c1.IsInvalid() {
return c1
}
c = c1
// If no additional arguments, return the first one
if len(c2) == 0 {
return
}
// Check all additional arguments
for _, carbon := range c2 {
// If any carbon is invalid, return it immediately
if carbon.IsInvalid() {
return carbon
}
// Update minimum if current carbon is less or equal
if carbon.Lte(c) {
c = carbon
}
}
return
}
// Closest returns the closest Carbon instance from some given Carbon instances.
func (c *Carbon) Closest(c1 *Carbon, c2 ...*Carbon) *Carbon {
// Validate the base carbon instance
if c.IsInvalid() {
return c
}
// Validate the first comparison instance
if c1.IsInvalid() {
return c1
}
// If no additional arguments, return the first one
if len(c2) == 0 {
return c1
}
// Find the closest among all instances
closest := c1
minDiff := c.DiffAbsInSeconds(closest)
// Check all additional arguments
for _, arg := range c2 {
// Validate each argument
if arg.IsInvalid() {
return arg
}
// Calculate difference and update if closer
if diff := c.DiffAbsInSeconds(arg); diff < minDiff {
minDiff = diff
closest = arg
}
}
return closest
}
// Farthest returns the farthest Carbon instance from some given Carbon instances.
func (c *Carbon) Farthest(c1 *Carbon, c2 ...*Carbon) *Carbon {
// Validate the base carbon instance
if c.IsInvalid() {
return c
}
// Validate the first comparison instance
if c1.IsInvalid() {
return c1
}
// If no additional arguments, return the first one
if len(c2) == 0 {
return c1
}
// Find the farthest among all instances
farthest := c1
maxDiff := c.DiffAbsInSeconds(farthest)
// Check all additional arguments
for _, arg := range c2 {
// Validate each argument
if arg.IsInvalid() {
return arg
}
// Calculate difference and update if farther
if diff := c.DiffAbsInSeconds(arg); diff > maxDiff {
maxDiff = diff
farthest = arg
}
}
return farthest
}