-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatefinder.go
58 lines (50 loc) · 1.36 KB
/
datefinder.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
package main
import (
"encoding/json"
"time"
"github.com/araddon/dateparse"
)
type datefinder func(string) (time.Time, bool)
var (
DATE_FINDER_ORDER = []datefinder{
dateFindZapJson,
dateFindRecursive,
}
)
// expensive search for a date somewhere in the string
func dateFindRecursive(datestr string) (time.Time, bool) {
if len(datestr) == 0 {
return time.Time{}, false
}
t, err := dateparse.ParseAny(datestr)
if err != nil {
return dateFindRecursive(datestr[1:])
}
return t, true
}
// go.uber.com/zap
// by default, NewProductionEncoderConfig will have the timestamp keyed by "ts"
// and NewDevelopmentEncoderConfig will use "T" as the TimeKey.
// Try to find either of those keys and decode the date from the value
func dateFindZapJson(datestr string) (time.Time, bool) {
var anyjson map[string]interface{}
if err := json.Unmarshal([]byte(datestr), &anyjson); err != nil {
return time.Time{}, false
}
for _, timeKey := range []string{"ts", "T"} {
if ts, ok := anyjson[timeKey]; ok {
return dateFindRecursive(ts.(string))
}
}
return time.Time{}, false
}
// Try to detect the date using several methods.
func findDate(datestr string) time.Time {
for _, fdr := range DATE_FINDER_ORDER {
if t, found := fdr(datestr); found {
return t
}
}
// no date found. This will sort low and print the line as quickly as possible.
return time.Time{}
}