2021-09-20 09:28:00 +00:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Import from zerolog/global.go
|
2024-08-23 23:11:20 +00:00
|
|
|
timeFormatUnix = ""
|
|
|
|
timeFormatUnixMs = "UNIXMS"
|
2021-09-20 09:28:00 +00:00
|
|
|
timeFormatUnixMicro = "UNIXMICRO"
|
2024-08-23 23:11:20 +00:00
|
|
|
timeFormatUnixNano = "UNIXNANO"
|
2021-09-20 09:28:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// AppendTime formats the input time with the given format
|
|
|
|
// and appends the encoded string to the input byte slice.
|
|
|
|
func (e Encoder) AppendTime(dst []byte, t time.Time, format string) []byte {
|
|
|
|
switch format {
|
|
|
|
case timeFormatUnix:
|
|
|
|
return e.AppendInt64(dst, t.Unix())
|
|
|
|
case timeFormatUnixMs:
|
|
|
|
return e.AppendInt64(dst, t.UnixNano()/1000000)
|
|
|
|
case timeFormatUnixMicro:
|
|
|
|
return e.AppendInt64(dst, t.UnixNano()/1000)
|
2024-08-23 23:11:20 +00:00
|
|
|
case timeFormatUnixNano:
|
|
|
|
return e.AppendInt64(dst, t.UnixNano())
|
2021-09-20 09:28:00 +00:00
|
|
|
}
|
|
|
|
return append(t.AppendFormat(append(dst, '"'), format), '"')
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppendTimes converts the input times with the given format
|
|
|
|
// and appends the encoded string list to the input byte slice.
|
|
|
|
func (Encoder) AppendTimes(dst []byte, vals []time.Time, format string) []byte {
|
|
|
|
switch format {
|
|
|
|
case timeFormatUnix:
|
|
|
|
return appendUnixTimes(dst, vals)
|
|
|
|
case timeFormatUnixMs:
|
2024-08-23 23:11:20 +00:00
|
|
|
return appendUnixNanoTimes(dst, vals, 1000000)
|
|
|
|
case timeFormatUnixMicro:
|
|
|
|
return appendUnixNanoTimes(dst, vals, 1000)
|
|
|
|
case timeFormatUnixNano:
|
|
|
|
return appendUnixNanoTimes(dst, vals, 1)
|
2021-09-20 09:28:00 +00:00
|
|
|
}
|
|
|
|
if len(vals) == 0 {
|
|
|
|
return append(dst, '[', ']')
|
|
|
|
}
|
|
|
|
dst = append(dst, '[')
|
|
|
|
dst = append(vals[0].AppendFormat(append(dst, '"'), format), '"')
|
|
|
|
if len(vals) > 1 {
|
|
|
|
for _, t := range vals[1:] {
|
|
|
|
dst = append(t.AppendFormat(append(dst, ',', '"'), format), '"')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dst = append(dst, ']')
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendUnixTimes(dst []byte, vals []time.Time) []byte {
|
|
|
|
if len(vals) == 0 {
|
|
|
|
return append(dst, '[', ']')
|
|
|
|
}
|
|
|
|
dst = append(dst, '[')
|
|
|
|
dst = strconv.AppendInt(dst, vals[0].Unix(), 10)
|
|
|
|
if len(vals) > 1 {
|
|
|
|
for _, t := range vals[1:] {
|
|
|
|
dst = strconv.AppendInt(append(dst, ','), t.Unix(), 10)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dst = append(dst, ']')
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
2024-08-23 23:11:20 +00:00
|
|
|
func appendUnixNanoTimes(dst []byte, vals []time.Time, div int64) []byte {
|
2021-09-20 09:28:00 +00:00
|
|
|
if len(vals) == 0 {
|
|
|
|
return append(dst, '[', ']')
|
|
|
|
}
|
|
|
|
dst = append(dst, '[')
|
2024-08-23 23:11:20 +00:00
|
|
|
dst = strconv.AppendInt(dst, vals[0].UnixNano()/div, 10)
|
2021-09-20 09:28:00 +00:00
|
|
|
if len(vals) > 1 {
|
|
|
|
for _, t := range vals[1:] {
|
2024-08-23 23:11:20 +00:00
|
|
|
dst = strconv.AppendInt(append(dst, ','), t.UnixNano()/div, 10)
|
2021-09-20 09:28:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
dst = append(dst, ']')
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppendDuration formats the input duration with the given unit & format
|
|
|
|
// and appends the encoded string to the input byte slice.
|
2024-08-23 23:11:20 +00:00
|
|
|
func (e Encoder) AppendDuration(dst []byte, d time.Duration, unit time.Duration, useInt bool, precision int) []byte {
|
2021-09-20 09:28:00 +00:00
|
|
|
if useInt {
|
|
|
|
return strconv.AppendInt(dst, int64(d/unit), 10)
|
|
|
|
}
|
2024-08-23 23:11:20 +00:00
|
|
|
return e.AppendFloat64(dst, float64(d)/float64(unit), precision)
|
2021-09-20 09:28:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AppendDurations formats the input durations with the given unit & format
|
|
|
|
// and appends the encoded string list to the input byte slice.
|
2024-08-23 23:11:20 +00:00
|
|
|
func (e Encoder) AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, useInt bool, precision int) []byte {
|
2021-09-20 09:28:00 +00:00
|
|
|
if len(vals) == 0 {
|
|
|
|
return append(dst, '[', ']')
|
|
|
|
}
|
|
|
|
dst = append(dst, '[')
|
2024-08-23 23:11:20 +00:00
|
|
|
dst = e.AppendDuration(dst, vals[0], unit, useInt, precision)
|
2021-09-20 09:28:00 +00:00
|
|
|
if len(vals) > 1 {
|
|
|
|
for _, d := range vals[1:] {
|
2024-08-23 23:11:20 +00:00
|
|
|
dst = e.AppendDuration(append(dst, ','), d, unit, useInt, precision)
|
2021-09-20 09:28:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
dst = append(dst, ']')
|
|
|
|
return dst
|
|
|
|
}
|