-
Notifications
You must be signed in to change notification settings - Fork 67
/
time.go
56 lines (45 loc) · 1.02 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
package zed
import (
"time"
"github.com/brimdata/zed/pkg/nano"
"github.com/brimdata/zed/zcode"
)
type TypeOfTime struct{}
func NewTime(ts nano.Ts) Value {
return Value{TypeTime, EncodeTime(ts)}
}
func EncodeTime(t nano.Ts) zcode.Bytes {
var b [8]byte
n := zcode.EncodeCountedVarint(b[:], int64(t))
return b[:n]
}
func AppendTime(bytes zcode.Bytes, t nano.Ts) zcode.Bytes {
return AppendInt(bytes, int64(t))
}
func DecodeTime(zv zcode.Bytes) (nano.Ts, error) {
if zv == nil {
return 0, nil
}
return nano.Ts(zcode.DecodeCountedVarint(zv)), nil
}
func (t *TypeOfTime) ID() int {
return IDTime
}
func (t *TypeOfTime) String() string {
return "time"
}
func (t *TypeOfTime) Marshal(zv zcode.Bytes) (interface{}, error) {
ts, err := DecodeTime(zv)
if err != nil {
return nil, err
}
return ts.Time().Format(time.RFC3339Nano), nil
}
func (t *TypeOfTime) Format(zv zcode.Bytes) string {
ts, err := DecodeTime(zv)
if err != nil {
return badZng(err, t, zv)
}
b := ts.Time().Format(time.RFC3339Nano)
return string(b)
}