-
-
Notifications
You must be signed in to change notification settings - Fork 254
/
Copy pathextremum.go
executable file
·92 lines (82 loc) · 2.17 KB
/
extremum.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
package carbon
import "time"
const (
minDuration time.Duration = -1 << 63
maxDuration time.Duration = 1<<63 - 1
)
// MaxValue returns a Carbon instance for the greatest supported date.
// 返回 Carbon 的最大值
func MaxValue() *Carbon {
return NewCarbon(time.Date(9999, time.December, 31, 23, 59, 59, 999999999, time.UTC))
}
// MinValue returns a Carbon instance for the lowest supported date.
// 返回 Carbon 的最小值
func MinValue() *Carbon {
return NewCarbon(time.Date(-9998, time.January, 1, 0, 0, 0, 0, time.UTC))
}
// MaxDuration returns the maximum duration value.
// 返回 Duration 的最大值
func MaxDuration() time.Duration {
return maxDuration
}
// MinDuration returns the minimum duration value.
// 返回 Duration 的最小值
func MinDuration() time.Duration {
return minDuration
}
// Max returns the maximum Carbon instance from the given Carbon instance (second-precision).
// 返回最大的 Carbon 实例
func Max(c1 *Carbon, c2 ...*Carbon) (c *Carbon) {
c = c1
if len(c2) == 0 {
return
}
for i := range c2 {
if c.IsInvalid() || c2[i].IsInvalid() {
return nil
}
if c2[i].Gte(c) {
c = c2[i]
}
}
return
}
// Min returns the minimum Carbon instance from the given Carbon instance (second-precision).
// 返回最小的 Carbon 实例
func Min(c1 *Carbon, c2 ...*Carbon) (c *Carbon) {
c = c1
if len(c2) == 0 {
return
}
for i := range c2 {
if c.IsInvalid() || c2[i].IsInvalid() {
return nil
}
if c2[i].Lte(c) {
c = c2[i]
}
}
return
}
// Closest returns the closest Carbon instance from the given Carbon instance.
// 返回离给定 carbon 实例最近的 Carbon 实例
func (c *Carbon) Closest(c1 *Carbon, c2 *Carbon) *Carbon {
if c.IsInvalid() || c1.IsInvalid() || c2.IsInvalid() {
return nil
}
if c.DiffAbsInSeconds(c1) < c.DiffAbsInSeconds(c2) {
return c1
}
return c2
}
// Farthest returns the farthest Carbon instance from the given Carbon instance.
// 返回离给定 carbon 实例最远的 Carbon 实例
func (c *Carbon) Farthest(c1 *Carbon, c2 *Carbon) *Carbon {
if c.IsInvalid() || c1.IsInvalid() || c2.IsInvalid() {
return nil
}
if c.DiffAbsInSeconds(c1) > c.DiffAbsInSeconds(c2) {
return c1
}
return c2
}