forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.go
63 lines (48 loc) · 1.22 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
package utils
import (
"time"
"github.com/Velocidex/json"
errors "github.com/pkg/errors"
vjson "www.velocidex.com/golang/velociraptor/json"
"www.velocidex.com/golang/vfilter"
)
func IsTime(a vfilter.Any) (time.Time, bool) {
switch t := a.(type) {
case time.Time:
return t, true
default:
return time.Unix(0, 0), false
}
}
// Take care of marshaling all timestamps in UTC
func MarshalTimes(v interface{}, opts *json.EncOpts) ([]byte, error) {
switch t := v.(type) {
case time.Time:
// Marshal the time in the desired timezone.
return t.UTC().MarshalJSON()
case *time.Time:
return t.UTC().MarshalJSON()
}
return nil, json.EncoderCallbackSkip
}
func init() {
vjson.RegisterCustomEncoder(time.Time{}, MarshalTimes)
vjson.RegisterCustomEncoder(&time.Time{}, MarshalTimes)
}
func AnyToTime(v interface{}) (time.Time, error) {
switch t := v.(type) {
case time.Time:
return t.UTC(), nil
case *time.Time:
return t.UTC(), nil
case int64:
return time.Unix(t, 0).UTC(), nil
case int:
return time.Unix(int64(t), 0).UTC(), nil
case float64:
return time.Unix(int64(t), 0).UTC(), nil
case uint64:
return time.Unix(int64(t), 0).UTC(), nil
}
return time.Time{}, errors.New("Can not convert to time")
}