-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.go
147 lines (125 loc) · 3.49 KB
/
math.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
package hdur
import (
"math"
"time"
)
// Equal returns true if the durations are equal
func (d Duration) Equal(other Duration) bool {
// Normalize both durations before comparison
d.normalize()
other.normalize()
return d.Years == other.Years &&
d.Months == other.Months &&
d.Days == other.Days &&
d.Hours == other.Hours &&
d.Minutes == other.Minutes &&
d.Seconds == other.Seconds &&
d.Nanos == other.Nanos
}
// Less returns true if d is less than other
func (d Duration) Less(other Duration) bool {
// Compare using a fixed point in time
t := time.Now()
return d.Add(t).Before(other.Add(t))
}
// LessOrEqual returns true if d is less than or equal to other
func (d Duration) LessOrEqual(other Duration) bool {
return d.Less(other) || d.Equal(other)
}
// Greater returns true if d is greater than other
func (d Duration) Greater(other Duration) bool {
return !d.LessOrEqual(other)
}
// GreaterOrEqual returns true if d is greater than or equal to other
func (d Duration) GreaterOrEqual(other Duration) bool {
return !d.Less(other)
}
// Abs returns the absolute value of the duration
func (d Duration) Abs() Duration {
if d.Less(Duration{}) {
return d.Mul(-1)
}
return d
}
// Mul returns the duration multiplied by the given factor
func (d Duration) Mul(factor float64) Duration {
if factor == 0 {
return Duration{}
}
// For negative factors, multiply by the absolute value and then negate
if factor < 0 {
factor = -factor
result := Duration{
Years: -int(float64(d.Years) * factor),
Months: -int(float64(d.Months) * factor),
Days: -int(float64(d.Days) * factor),
Hours: -int(float64(d.Hours) * factor),
Minutes: -int(float64(d.Minutes) * factor),
Seconds: -int(float64(d.Seconds) * factor),
Nanos: -int(float64(d.Nanos) * factor),
}
result.normalize()
return result
}
result := Duration{
Years: int(float64(d.Years) * factor),
Months: int(float64(d.Months) * factor),
Days: int(float64(d.Days) * factor),
Hours: int(float64(d.Hours) * factor),
Minutes: int(float64(d.Minutes) * factor),
Seconds: int(float64(d.Seconds) * factor),
Nanos: int(float64(d.Nanos) * factor),
}
result.normalize()
return result
}
// Div returns the duration divided by the given divisor
func (d Duration) Div(divisor float64) Duration {
if divisor == 0 {
panic("division by zero")
}
return d.Mul(1 / divisor)
}
// Round rounds the duration to the nearest multiple of the given duration
func (d Duration) Round(multiple Duration) Duration {
if multiple.IsZero() {
return d
}
// Convert to nanoseconds for comparison
t := time.Now()
dNanos := d.Add(t).Sub(t)
mNanos := multiple.Add(t).Sub(t)
if mNanos == 0 {
return d
}
rounded := time.Duration(math.Round(float64(dNanos)/float64(mNanos))) * time.Duration(mNanos)
result := Duration{
Seconds: int(rounded.Seconds()),
Nanos: int(rounded.Nanoseconds() % 1000000000),
}
result.normalize()
return result
}
// Truncate truncates the duration to a multiple of the given duration
func (d Duration) Truncate(multiple Duration) Duration {
if multiple.IsZero() {
return d
}
// Convert to nanoseconds for comparison
t := time.Now()
dNanos := d.Add(t).Sub(t)
mNanos := multiple.Add(t).Sub(t)
if mNanos == 0 {
return d
}
truncated := time.Duration(dNanos/mNanos) * time.Duration(mNanos)
result := Duration{
Seconds: int(truncated.Seconds()),
Nanos: int(truncated.Nanoseconds() % 1000000000),
}
result.normalize()
return result
}
func abs(x int) int {
return int(math.Abs(float64(x)))
}