Skip to content

Commit

Permalink
add millisecond duration encoder
Browse files Browse the repository at this point in the history
make ms encoder compatible for go version <=1.13.0 & add testcase for ms encoder

change name of ms encoder
  • Loading branch information
caibirdme committed Jan 9, 2020
1 parent 33e58d4 commit f7f9679
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
8 changes: 8 additions & 0 deletions zapcore/encoder.go
Expand Up @@ -168,6 +168,12 @@ func NanosDurationEncoder(d time.Duration, enc PrimitiveArrayEncoder) {
enc.AppendInt64(int64(d))
}

// MillisDurationEncoder serializes a time.Duration to an integer number of
// milliseconds elapsed.
func MillisDurationEncoder(d time.Duration, enc PrimitiveArrayEncoder) {
enc.AppendInt64(d.Nanoseconds() / 1e6)
}

// StringDurationEncoder serializes a time.Duration using its built-in String
// method.
func StringDurationEncoder(d time.Duration, enc PrimitiveArrayEncoder) {
Expand All @@ -183,6 +189,8 @@ func (e *DurationEncoder) UnmarshalText(text []byte) error {
*e = StringDurationEncoder
case "nanos":
*e = NanosDurationEncoder
case "ms":
*e = MillisDurationEncoder
default:
*e = SecondsDurationEncoder
}
Expand Down
1 change: 1 addition & 0 deletions zapcore/encoder_test.go
Expand Up @@ -566,6 +566,7 @@ func TestDurationEncoders(t *testing.T) {
}{
{"string", "1.0000005s"},
{"nanos", int64(1000000500)},
{"ms", int64(1000)},
{"", 1.0000005},
{"something-random", 1.0000005},
}
Expand Down

0 comments on commit f7f9679

Please sign in to comment.