1package json 2 3import ( 4 "strconv" 5 "time" 6) 7 8const ( 9 // Import from zerolog/global.go 10 timeFormatUnix = "" 11 timeFormatUnixMs = "UNIXMS" 12 timeFormatUnixMicro = "UNIXMICRO" 13) 14 15// AppendTime formats the input time with the given format 16// and appends the encoded string to the input byte slice. 17func (e Encoder) AppendTime(dst []byte, t time.Time, format string) []byte { 18 switch format { 19 case timeFormatUnix: 20 return e.AppendInt64(dst, t.Unix()) 21 case timeFormatUnixMs: 22 return e.AppendInt64(dst, t.UnixNano()/1000000) 23 case timeFormatUnixMicro: 24 return e.AppendInt64(dst, t.UnixNano()/1000) 25 } 26 return append(t.AppendFormat(append(dst, '"'), format), '"') 27} 28 29// AppendTimes converts the input times with the given format 30// and appends the encoded string list to the input byte slice. 31func (Encoder) AppendTimes(dst []byte, vals []time.Time, format string) []byte { 32 switch format { 33 case timeFormatUnix: 34 return appendUnixTimes(dst, vals) 35 case timeFormatUnixMs: 36 return appendUnixMsTimes(dst, vals) 37 } 38 if len(vals) == 0 { 39 return append(dst, '[', ']') 40 } 41 dst = append(dst, '[') 42 dst = append(vals[0].AppendFormat(append(dst, '"'), format), '"') 43 if len(vals) > 1 { 44 for _, t := range vals[1:] { 45 dst = append(t.AppendFormat(append(dst, ',', '"'), format), '"') 46 } 47 } 48 dst = append(dst, ']') 49 return dst 50} 51 52func appendUnixTimes(dst []byte, vals []time.Time) []byte { 53 if len(vals) == 0 { 54 return append(dst, '[', ']') 55 } 56 dst = append(dst, '[') 57 dst = strconv.AppendInt(dst, vals[0].Unix(), 10) 58 if len(vals) > 1 { 59 for _, t := range vals[1:] { 60 dst = strconv.AppendInt(append(dst, ','), t.Unix(), 10) 61 } 62 } 63 dst = append(dst, ']') 64 return dst 65} 66 67func appendUnixMsTimes(dst []byte, vals []time.Time) []byte { 68 if len(vals) == 0 { 69 return append(dst, '[', ']') 70 } 71 dst = append(dst, '[') 72 dst = strconv.AppendInt(dst, vals[0].UnixNano()/1000000, 10) 73 if len(vals) > 1 { 74 for _, t := range vals[1:] { 75 dst = strconv.AppendInt(append(dst, ','), t.UnixNano()/1000000, 10) 76 } 77 } 78 dst = append(dst, ']') 79 return dst 80} 81 82// AppendDuration formats the input duration with the given unit & format 83// and appends the encoded string to the input byte slice. 84func (e Encoder) AppendDuration(dst []byte, d time.Duration, unit time.Duration, useInt bool) []byte { 85 if useInt { 86 return strconv.AppendInt(dst, int64(d/unit), 10) 87 } 88 return e.AppendFloat64(dst, float64(d)/float64(unit)) 89} 90 91// AppendDurations formats the input durations with the given unit & format 92// and appends the encoded string list to the input byte slice. 93func (e Encoder) AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, useInt bool) []byte { 94 if len(vals) == 0 { 95 return append(dst, '[', ']') 96 } 97 dst = append(dst, '[') 98 dst = e.AppendDuration(dst, vals[0], unit, useInt) 99 if len(vals) > 1 { 100 for _, d := range vals[1:] { 101 dst = e.AppendDuration(append(dst, ','), d, unit, useInt) 102 } 103 } 104 dst = append(dst, ']') 105 return dst 106} 107