Skip to content

Commit

Permalink
Add millisecond duration encoder (#773)
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
  • Loading branch information
caibirdme authored and prashantv committed Jan 9, 2020
1 parent 66bdb5b commit 73a5f64
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 73a5f64

Please sign in to comment.