-
Notifications
You must be signed in to change notification settings - Fork 7
/
example_test.go
90 lines (71 loc) · 2.35 KB
/
example_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
package spaniel_test
import (
"fmt"
"github.com/senseyeio/spaniel"
"sort"
"time"
)
type dur struct {
from time.Time
to time.Time
}
var times []dur
var input spaniel.Spans
func init() {
times = []dur{{from: time.Date(2018, 1, 30, 0, 0, 0, 0, time.UTC), to: time.Date(2018, 1, 30, 1, 0, 0, 0, time.UTC)},
{from: time.Date(2018, 1, 30, 0, 30, 0, 0, time.UTC), to: time.Date(2018, 1, 30, 1, 30, 0, 0, time.UTC)},
{from: time.Date(2018, 1, 30, 1, 31, 0, 0, time.UTC), to: time.Date(2018, 1, 30, 1, 35, 0, 0, time.UTC)},
{from: time.Date(2018, 1, 30, 1, 33, 0, 0, time.UTC), to: time.Date(2018, 1, 30, 1, 34, 0, 0, time.UTC)},
}
for t := range times {
input = append(input, spaniel.New(times[t].from, times[t].to))
}
}
func ExampleTimeSpan() {
start, _ := time.Parse("2006-01-02 15:04:05", "2018-01-01 00:00:00")
timespan := spaniel.New(start, time.Unix(1514768400, 0).UTC())
fmt.Printf("Start: %v (%v)\nEnd: %v (%v)\n", timespan.Start(), timespan.StartType(), timespan.End(), timespan.EndType())
// Output:
// Start: 2018-01-01 00:00:00 +0000 UTC (1)
// End: 2018-01-01 01:00:00 +0000 UTC (0)
}
func ExampleByStart() {
sort.Stable(spaniel.ByStart(input))
for i := range input {
fmt.Println(input[i].Start())
}
// Output:
// 2018-01-30 00:00:00 +0000 UTC
// 2018-01-30 00:30:00 +0000 UTC
// 2018-01-30 01:31:00 +0000 UTC
// 2018-01-30 01:33:00 +0000 UTC
}
func ExampleByEnd() {
sort.Stable(spaniel.ByEnd(input))
for i := range input {
fmt.Println(input[i].End())
}
// Output:
// 2018-01-30 01:00:00 +0000 UTC
// 2018-01-30 01:30:00 +0000 UTC
// 2018-01-30 01:34:00 +0000 UTC
// 2018-01-30 01:35:00 +0000 UTC
}
func ExampleSpans_Union() {
union := input.Union()
for u := range union {
fmt.Println(union[u].Start(), "->", union[u].End(), ": ", union[u].End().Sub(union[u].Start()))
}
// Output:
// 2018-01-30 00:00:00 +0000 UTC -> 2018-01-30 01:30:00 +0000 UTC : 1h30m0s
// 2018-01-30 01:31:00 +0000 UTC -> 2018-01-30 01:35:00 +0000 UTC : 4m0s
}
func ExampleSpans_Intersection() {
intersection := input.Intersection()
for i := range intersection {
fmt.Println(intersection[i].Start(), "->", intersection[i].End(), ": ", intersection[i].End().Sub(intersection[i].Start()))
}
// Output:
// 2018-01-30 00:30:00 +0000 UTC -> 2018-01-30 01:00:00 +0000 UTC : 30m0s
// 2018-01-30 01:33:00 +0000 UTC -> 2018-01-30 01:34:00 +0000 UTC : 1m0s
}