2018-03-28 18:49:41 +00:00
|
|
|
package cbor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func appendIntegerTimestamp(dst []byte, t time.Time) []byte {
|
|
|
|
major := majorTypeTags
|
|
|
|
minor := additionalTypeTimestamp
|
2021-12-21 12:07:54 +00:00
|
|
|
dst = append(dst, major|minor)
|
2018-03-28 18:49:41 +00:00
|
|
|
secs := t.Unix()
|
|
|
|
var val uint64
|
|
|
|
if secs < 0 {
|
|
|
|
major = majorTypeNegativeInt
|
|
|
|
val = uint64(-secs - 1)
|
|
|
|
} else {
|
|
|
|
major = majorTypeUnsignedInt
|
|
|
|
val = uint64(secs)
|
|
|
|
}
|
2021-12-21 12:07:54 +00:00
|
|
|
dst = appendCborTypePrefix(dst, major, val)
|
2018-03-28 18:49:41 +00:00
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
2018-05-10 22:01:41 +00:00
|
|
|
func (e Encoder) appendFloatTimestamp(dst []byte, t time.Time) []byte {
|
2018-03-28 18:49:41 +00:00
|
|
|
major := majorTypeTags
|
|
|
|
minor := additionalTypeTimestamp
|
2021-12-21 12:07:54 +00:00
|
|
|
dst = append(dst, major|minor)
|
2018-03-28 18:49:41 +00:00
|
|
|
secs := t.Unix()
|
|
|
|
nanos := t.Nanosecond()
|
|
|
|
var val float64
|
2021-12-21 12:07:54 +00:00
|
|
|
val = float64(secs)*1.0 + float64(nanos)*1e-9
|
2018-05-10 22:01:41 +00:00
|
|
|
return e.AppendFloat64(dst, val)
|
2018-03-28 18:49:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AppendTime encodes and adds a timestamp to the dst byte array.
|
2018-05-10 22:01:41 +00:00
|
|
|
func (e Encoder) AppendTime(dst []byte, t time.Time, unused string) []byte {
|
2018-03-28 18:49:41 +00:00
|
|
|
utc := t.UTC()
|
|
|
|
if utc.Nanosecond() == 0 {
|
|
|
|
return appendIntegerTimestamp(dst, utc)
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
return e.appendFloatTimestamp(dst, utc)
|
2018-03-28 18:49:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AppendTimes encodes and adds an array of timestamps to the dst byte array.
|
2018-05-10 22:01:41 +00:00
|
|
|
func (e Encoder) AppendTimes(dst []byte, vals []time.Time, unused string) []byte {
|
2018-03-28 18:49:41 +00:00
|
|
|
major := majorTypeArray
|
|
|
|
l := len(vals)
|
|
|
|
if l == 0 {
|
2018-05-10 22:01:41 +00:00
|
|
|
return e.AppendArrayEnd(e.AppendArrayStart(dst))
|
2018-03-28 18:49:41 +00:00
|
|
|
}
|
|
|
|
if l <= additionalMax {
|
|
|
|
lb := byte(l)
|
2021-12-21 12:07:54 +00:00
|
|
|
dst = append(dst, major|lb)
|
2018-03-28 18:49:41 +00:00
|
|
|
} else {
|
|
|
|
dst = appendCborTypePrefix(dst, major, uint64(l))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range vals {
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = e.AppendTime(dst, t, unused)
|
2018-03-28 18:49:41 +00:00
|
|
|
}
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppendDuration encodes and adds a duration to the dst byte array.
|
|
|
|
// useInt field indicates whether to store the duration as seconds (integer) or
|
|
|
|
// as seconds+nanoseconds (float).
|
2018-05-10 22:01:41 +00:00
|
|
|
func (e Encoder) AppendDuration(dst []byte, d time.Duration, unit time.Duration, useInt bool) []byte {
|
2018-03-28 18:49:41 +00:00
|
|
|
if useInt {
|
2018-05-10 22:01:41 +00:00
|
|
|
return e.AppendInt64(dst, int64(d/unit))
|
2018-03-28 18:49:41 +00:00
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
return e.AppendFloat64(dst, float64(d)/float64(unit))
|
2018-03-28 18:49:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AppendDurations encodes and adds an array of durations to the dst byte array.
|
|
|
|
// useInt field indicates whether to store the duration as seconds (integer) or
|
|
|
|
// as seconds+nanoseconds (float).
|
2018-05-10 22:01:41 +00:00
|
|
|
func (e Encoder) AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, useInt bool) []byte {
|
2018-03-28 18:49:41 +00:00
|
|
|
major := majorTypeArray
|
|
|
|
l := len(vals)
|
|
|
|
if l == 0 {
|
2018-05-10 22:01:41 +00:00
|
|
|
return e.AppendArrayEnd(e.AppendArrayStart(dst))
|
2018-03-28 18:49:41 +00:00
|
|
|
}
|
|
|
|
if l <= additionalMax {
|
|
|
|
lb := byte(l)
|
2021-12-21 12:07:54 +00:00
|
|
|
dst = append(dst, major|lb)
|
2018-03-28 18:49:41 +00:00
|
|
|
} else {
|
|
|
|
dst = appendCborTypePrefix(dst, major, uint64(l))
|
|
|
|
}
|
|
|
|
for _, d := range vals {
|
2018-05-10 22:01:41 +00:00
|
|
|
dst = e.AppendDuration(dst, d, unit, useInt)
|
2018-03-28 18:49:41 +00:00
|
|
|
}
|
|
|
|
return dst
|
|
|
|
}
|