-
Notifications
You must be signed in to change notification settings - Fork 0
/
timestamps.go
64 lines (53 loc) · 1.27 KB
/
timestamps.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
package main
import (
"sort"
"strings"
"time"
)
type timestamps []time.Time
// Len is the number of elements in the collection.
func (ts timestamps) Len() int { return len(ts) }
// Less reports whether the element with
// index i should sort before the element with index j.
func (ts timestamps) Less(i, j int) bool {
return ts[i].UnixNano() < ts[j].UnixNano()
}
// Swap swaps the elements with indexes i and j.
func (ts timestamps) Swap(i, j int) {
ts[i], ts[j] = ts[j], ts[i]
}
type eventSorter struct {
timeformat string
ts []time.Time
sorted timestamps
}
func newEventSorter(nReaders int, tf string) *eventSorter {
return &eventSorter{
timeformat: tf,
ts: make([]time.Time, nReaders),
sorted: make(timestamps, nReaders),
}
}
func (es *eventSorter) firstEventIndex(records []string) (int, error) {
for i, record := range records {
if record == "" {
es.ts[i] = time.Now()
continue
}
timePrefix := strings.TrimSpace(record[:len(es.timeformat)+1])
t, err := time.Parse(es.timeformat, timePrefix)
if err != nil {
return 0, err
}
es.ts[i] = t
}
copy(es.sorted, es.ts)
sort.Sort(es.sorted)
first := es.sorted[0]
for i, s := range es.ts {
if s == first {
return i, nil
}
}
panic("We have lost count of everything.")
}