Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add millisecond duration encoder #773

Merged
merged 1 commit into from Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
enc.AppendInt64(d.Nanoseconds() / 1e6)
enc.AppendInt64(d.Milliseconds())

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the d.Milliseconds() is a new API only for go1.13+

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, we can migrate to it once we drop support for 1.12. This is good for now, thanks.

}

// 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