forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.go
59 lines (47 loc) · 1.19 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
package utils
import (
"time"
"github.com/Velocidex/json"
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
}
}
// TODO: Deprecate this one.
type TimeVal struct {
Sec int64 `json:"sec"`
Nsec int64 `json:"usec"`
}
func (self TimeVal) Time() time.Time {
if self.Nsec > 0 {
return time.Unix(0, self.Nsec)
}
return time.Unix(self.Sec, 0)
}
// 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()
case TimeVal:
return t.Time().UTC().MarshalJSON()
case *TimeVal:
return t.Time().UTC().MarshalJSON()
}
return nil, json.EncoderCallbackSkip
}
func init() {
vjson.RegisterCustomEncoder(time.Time{}, MarshalTimes)
vjson.RegisterCustomEncoder(&time.Time{}, MarshalTimes)
vjson.RegisterCustomEncoder(TimeVal{}, MarshalTimes)
vjson.RegisterCustomEncoder(&TimeVal{}, MarshalTimes)
}