-
Notifications
You must be signed in to change notification settings - Fork 1
/
time.go
71 lines (62 loc) · 1.65 KB
/
time.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
package gofn
import "time"
// MinTime finds the minimum time in the list.
// NOTE: if zero time is in the list, the result will be zero.
func MinTime(t1 time.Time, s ...time.Time) time.Time {
min := t1
for i := range s {
if s[i].Before(min) {
min = s[i]
}
}
return min
}
// MaxTime finds the maximum time in the list
func MaxTime(t1 time.Time, s ...time.Time) time.Time {
max := t1
for i := range s {
if s[i].After(max) {
max = s[i]
}
}
return max
}
// MinMaxTime gets the minimum and maximum time values in the list
func MinMaxTime(t1 time.Time, s ...time.Time) (time.Time, time.Time) {
minTime := t1
maxTime := t1
for i := range s {
if s[i].Before(minTime) {
minTime = s[i]
} else if s[i].After(maxTime) {
maxTime = s[i]
}
}
return minTime, maxTime
}
// ExecDuration measures time duration of running a function
func ExecDuration(fn func()) time.Duration {
start := time.Now()
fn()
return time.Since(start)
}
// ExecDuration1 measures time duration of running a function
func ExecDuration1[T any](fn func() T) (T, time.Duration) {
start := time.Now()
val := fn()
return val, time.Since(start)
}
// ExecDuration2 measures time duration of running a function
func ExecDuration2[T1, T2 any](fn func() (T1, T2)) (T1, T2, time.Duration) {
start := time.Now()
val1, val2 := fn()
return val1, val2, time.Since(start)
}
// ExecDuration3 measures time duration of running a function
func ExecDuration3[T1, T2, T3 any](fn func() (T1, T2, T3)) (T1, T2, T3, time.Duration) {
start := time.Now()
val1, val2, val3 := fn()
return val1, val2, val3, time.Since(start)
}
// ExecDelay is an alias of time.AfterFunc
var ExecDelay = time.AfterFunc