-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructors.go
More file actions
166 lines (142 loc) · 3.59 KB
/
constructors.go
File metadata and controls
166 lines (142 loc) · 3.59 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
package hdur
import "time"
// Nanoseconds creates a Duration from a number of nanoseconds
func Nanoseconds(n float64) Duration {
d := Duration{}
total := int(n)
d.Nanos = total
d.normalize()
return d
}
// Microseconds creates a Duration from a number of microseconds
func Microseconds(µs float64) Duration {
return Nanoseconds(µs * 1000)
}
// Milliseconds creates a Duration from a number of milliseconds
func Milliseconds(ms float64) Duration {
return Nanoseconds(ms * 1000000)
}
// Seconds creates a Duration from a number of seconds
func Seconds(s float64) Duration {
d := Duration{}
total := int(s)
d.Seconds = total
d.normalize()
return d
}
// Minutes creates a Duration from a number of minutes
func Minutes(m float64) Duration {
d := Duration{}
total := int(m)
d.Minutes = total
d.normalize()
return d
}
// Hours creates a Duration from a number of hours
func Hours(h float64) Duration {
d := Duration{}
total := int(h)
d.Hours = total
d.normalize()
return d
}
// Days creates a Duration from a number of days
func Days(days float64) Duration {
d := Duration{}
total := int(days)
isNegative := days < 0
if isNegative {
total = -total
}
// Convert days to months when appropriate
if total >= 30 {
d.Months = total / 30
total = total % 30
}
d.Days = total
if isNegative {
d.Months = -d.Months
d.Days = -d.Days
}
d.normalize()
return d
}
// Weeks creates a Duration from a number of weeks
func Weeks(weeks float64) Duration {
return Days(weeks * 7)
}
// Months creates a Duration from a number of months
func Months(months float64) Duration {
d := Duration{}
total := int(months)
d.Months = total
d.normalize()
return d
}
// Years creates a Duration from a number of years
func Years(years float64) Duration {
return Months(years * 12)
}
// FromStandard converts a time.Duration to our Duration type
func FromStandard(d time.Duration) Duration {
return Duration{
Seconds: int(d.Seconds()),
Nanos: int(d.Nanoseconds() % 1000000000),
}
}
// InHours returns the duration as a floating-point number of hours
func (d Duration) InHours() float64 {
t := time.Now()
return d.Add(t).Sub(t).Hours()
}
// InMinutes returns the duration as a floating-point number of minutes
func (d Duration) InMinutes() float64 {
t := time.Now()
return d.Add(t).Sub(t).Minutes()
}
// InSeconds returns the duration as a floating-point number of seconds
func (d Duration) InSeconds() float64 {
t := time.Now()
return d.Add(t).Sub(t).Seconds()
}
// InNanoseconds returns the duration as an integer number of nanoseconds
func (d Duration) InNanoseconds() int64 {
t := time.Now()
return d.Add(t).Sub(t).Nanoseconds()
}
// InMonths returns the approximate number of months in the duration
func (d Duration) InMonths() float64 {
return float64(d.Years*12+d.Months) + float64(d.Days)/30.44
}
// InYears returns the approximate number of years in the duration
func (d Duration) InYears() float64 {
return float64(d.Years) + float64(d.Months)/12.0 + float64(d.Days)/365.25
}
// Common durations
var (
Nanosecond = Nanoseconds(1)
Microsecond = Microseconds(1)
Millisecond = Milliseconds(1)
Second = Seconds(1)
Minute = Minutes(1)
Hour = Hours(1)
Day = Days(1)
Week = Weeks(1)
Month = Months(1)
Year = Years(1)
Seconds30 = Seconds(30)
Minutes5 = Minutes(5)
Minutes30 = Minutes(30)
Hours2 = Hours(2)
Hours12 = Hours(12)
Hours24 = Hours(24)
Days7 = Days(7)
Days30 = Days(30)
Days90 = Days(90)
Days180 = Days(180)
Days365 = Days(365)
Months6 = Months(6)
Years2 = Years(2)
Years5 = Years(5)
Years10 = Years(10)
)