This repository has been archived by the owner on Nov 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_test.go
134 lines (105 loc) · 3.28 KB
/
event_test.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
package event
import (
"fmt"
"strings"
"time"
)
func ExampleBetween() {
before := time.Now()
A := time.Now()
t := time.Now()
B := time.Now()
after := time.Now()
fmt.Println(Between(t, A, B)) // true
fmt.Println(Between(t, before, A)) // false
fmt.Println(Between(t, B, after)) // false
fmt.Println(Between(t, before, after)) // true
fmt.Println(Between(t, before, B)) // true
fmt.Println(Between(t, A, after)) // true
fmt.Println(Between(t, after, before)) // false
fmt.Println(Between(t, t, B)) // true (from inclusive, to exclusive)
fmt.Println(Between(t, A, t)) // false (from inclusive, to exclusive)
// Output:
// true
// false
// false
// true
// true
// true
// false
// true
// false
}
func ExampleBetween_clock() {
now := time.Now()
in2secToday := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second()+2, now.Nanosecond(), now.Location())
in2secNextYear := time.Date(now.Year()+1, now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second()+2, now.Nanosecond(), now.Location())
start := now
//fmt.Println(in2secToday)
//fmt.Println(in2secNextYear)
in4sec := now.Add(4 * time.Second)
fmt.Println(Between(in2secNextYear, start, in4sec))
fmt.Println(Between(in2secToday, start, in4sec))
fmt.Println(Between(in2secNextYear, start, in4sec))
fmt.Println(Between(in2secToday, start, in4sec))
// Output:
// false
// true
// false
// true
}
// dFmt formats a duration nicely
func dFmt(d time.Duration) string {
s := fmt.Sprintf("%6s", d)
if strings.Contains(s, ".") {
pos := strings.Index(s, ".")
if strings.Contains(s[pos:], "s") {
s = s[:pos] + "s"
}
}
if strings.HasSuffix(s, "m0s") {
s = s[:len(s)-2]
}
if strings.HasSuffix(s, "h0m") {
s = s[:len(s)-2]
}
return strings.TrimSpace(s)
}
func ExampleBetween_clockDay() {
now := time.Now()
noon := time.Date(now.Year(), now.Month(), now.Day(), 12, 0, 0, 0, now.Location())
two := time.Date(now.Year(), now.Month(), now.Day(), 14, 0, 0, 0, now.Location())
h1 := time.Hour * 1
fmt.Println("one hour:", dFmt(h1))
e := &Event{from: noon, upTo: two, cooldown: h1, clockOnly: true}
fmt.Println("two hours:", dFmt(e.Duration()))
// Output:
// one hour: 1h
// two hours: 1h59m59s
}
func ExampleBetween_clockDayBackwards() {
now := time.Now()
noon := time.Date(now.Year(), now.Month(), now.Day(), 14, 0, 0, 0, now.Location())
two := time.Date(now.Year(), now.Month(), now.Day(), 12, 0, 0, 0, now.Location())
h1 := time.Hour * 1
fmt.Println("one hour:", dFmt(h1))
e := &Event{from: noon, upTo: two, cooldown: h1, clockOnly: true}
fmt.Println("twenty two hours:", dFmt(e.Duration()))
// Output:
// one hour: 1h
// twenty two hours: 21h59m59s
}
func ExampleBetween_clockMidnight() {
now := time.Now()
// One hour before midnight, the date does not matter
beforeMidnight := time.Date(now.Year(), now.Month(), now.Day(), 23, 0, 0, 0, now.Location())
// One hour after midnight, the date does not matter
afterMidnight := time.Date(now.Year(), now.Month(), now.Day(), 1, 0, 0, 0, now.Location())
h1 := time.Hour * 1
fmt.Println("one hour:", dFmt(h1))
e := &Event{from: beforeMidnight, upTo: afterMidnight, cooldown: h1, clockOnly: true}
fmt.Println("two hours:", dFmt(e.Duration()))
// Output:
// one hour: 1h
// two hours: 1h59m59s
}