fastpastebin/vendor/github.com/rs/zerolog/internal/json/time.go

104 lines
2.8 KiB
Go
Raw Normal View History

2018-04-30 18:42:17 +05:00
package json
import (
"strconv"
"time"
)
2019-10-13 13:55:38 +05:00
const (
// Import from zerolog/global.go
timeFormatUnix = ""
timeFormatUnixMs = "UNIXMS"
)
2018-04-30 18:42:17 +05:00
// AppendTime formats the input time with the given format
// and appends the encoded string to the input byte slice.
2019-03-07 07:56:50 +05:00
func (e Encoder) AppendTime(dst []byte, t time.Time, format string) []byte {
2019-10-13 13:55:38 +05:00
switch format {
case timeFormatUnix:
2019-03-07 07:56:50 +05:00
return e.AppendInt64(dst, t.Unix())
2019-10-13 13:55:38 +05:00
case timeFormatUnixMs:
return e.AppendInt64(dst, t.UnixNano()/1000000)
2018-04-30 18:42:17 +05: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.
2019-03-07 07:56:50 +05:00
func (Encoder) AppendTimes(dst []byte, vals []time.Time, format string) []byte {
2019-10-13 13:55:38 +05:00
switch format {
case timeFormatUnix:
2018-04-30 18:42:17 +05:00
return appendUnixTimes(dst, vals)
2019-10-13 13:55:38 +05:00
case timeFormatUnixMs:
return appendUnixMsTimes(dst, vals)
2018-04-30 18:42:17 +05: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:] {
2019-03-07 07:56:50 +05:00
dst = strconv.AppendInt(append(dst, ','), t.Unix(), 10)
2018-04-30 18:42:17 +05:00
}
}
dst = append(dst, ']')
return dst
}
2019-10-13 13:55:38 +05:00
func appendUnixMsTimes(dst []byte, vals []time.Time) []byte {
if len(vals) == 0 {
return append(dst, '[', ']')
}
dst = append(dst, '[')
dst = strconv.AppendInt(dst, vals[0].UnixNano()/1000000, 10)
if len(vals) > 1 {
for _, t := range vals[1:] {
dst = strconv.AppendInt(append(dst, ','), t.UnixNano()/1000000, 10)
}
}
dst = append(dst, ']')
return dst
}
2018-04-30 18:42:17 +05:00
// AppendDuration formats the input duration with the given unit & format
// and appends the encoded string to the input byte slice.
2019-03-07 07:56:50 +05:00
func (e Encoder) AppendDuration(dst []byte, d time.Duration, unit time.Duration, useInt bool) []byte {
2018-04-30 18:42:17 +05:00
if useInt {
return strconv.AppendInt(dst, int64(d/unit), 10)
}
2019-03-07 07:56:50 +05:00
return e.AppendFloat64(dst, float64(d)/float64(unit))
2018-04-30 18:42:17 +05:00
}
// AppendDurations formats the input durations with the given unit & format
// and appends the encoded string list to the input byte slice.
2019-03-07 07:56:50 +05:00
func (e Encoder) AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, useInt bool) []byte {
2018-04-30 18:42:17 +05:00
if len(vals) == 0 {
return append(dst, '[', ']')
}
dst = append(dst, '[')
2019-03-07 07:56:50 +05:00
dst = e.AppendDuration(dst, vals[0], unit, useInt)
2018-04-30 18:42:17 +05:00
if len(vals) > 1 {
for _, d := range vals[1:] {
2019-03-07 07:56:50 +05:00
dst = e.AppendDuration(append(dst, ','), d, unit, useInt)
2018-04-30 18:42:17 +05:00
}
}
dst = append(dst, ']')
return dst
}