From 596175c125cd3f3923ffe4a99e2fa29ae3d2a3c5 Mon Sep 17 00:00:00 2001 From: Thomas <9749173+uhthomas@users.noreply.github.com> Date: Sun, 18 Aug 2019 22:13:32 +0100 Subject: [PATCH 1/2] Add RFC3339, RFC3339Nano and update docs to include 'nano' --- zapcore/encoder.go | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/zapcore/encoder.go b/zapcore/encoder.go index 9cb0f5807..8d0fc72ce 100644 --- a/zapcore/encoder.go +++ b/zapcore/encoder.go @@ -118,11 +118,30 @@ func ISO8601TimeEncoder(t time.Time, enc PrimitiveArrayEncoder) { enc.AppendString(t.Format("2006-01-02T15:04:05.000Z0700")) } -// UnmarshalText unmarshals text to a TimeEncoder. "iso8601" and "ISO8601" are -// unmarshaled to ISO8601TimeEncoder, "millis" is unmarshaled to -// EpochMillisTimeEncoder, and anything else is unmarshaled to EpochTimeEncoder. +// RFC3339TimeEncoder serializes a time.Time to an RFC3339-formatted string. +func RFC3339TimeEncoder(t time.Time, enc PrimitiveArrayEncoder) { + enc.AppendString(t.Format(time.RFC3339)) +} + +// RFC3339NanoTimeEncoder serializes a time.Time to an RFC3339-formatted string +// with nanosecond precision. +func RFC3339NanoTimeEncoder(t time.Time, enc PrimitiveArrayEncoder) { + enc.AppendString(t.Format(time.RFC3339Nano)) +} + +// UnmarshalText unmarshals text to a TimeEncoder. +// "rfc3339nano" and "RFC3339Nano" are unmarshaled to RFC3339NanoTimeEncoder. +// "rfc3339" and "RFC3339" are unmarshaled to RFC3339TimeEncoder. +// "iso8601" and "ISO8601" are unmarshaled to ISO8601TimeEncoder. +// "millis" is unmarshaled to EpochMillisTimeEncoder. +// "nanos" is unmarshaled to EpochNanosEncoder. +// Anything else is unmarshaled to EpochTimeEncoder. func (e *TimeEncoder) UnmarshalText(text []byte) error { switch string(text) { + case "rfc3339nano", "RFC3339Nano": + *e = RFC3339NanoTimeEncoder + case "rfc3339", "RFC3339": + *e = RFC3339TimeEncoder case "iso8601", "ISO8601": *e = ISO8601TimeEncoder case "millis": From b39d4614cfbc0a9548a8775cd11fcf93be807a26 Mon Sep 17 00:00:00 2001 From: Thomas <9749173+uhthomas@users.noreply.github.com> Date: Tue, 20 Aug 2019 21:03:13 +0100 Subject: [PATCH 2/2] add tests for rfc3339 and rfc3339nano --- zapcore/encoder_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/zapcore/encoder_test.go b/zapcore/encoder_test.go index b7d31a161..646b58a3f 100644 --- a/zapcore/encoder_test.go +++ b/zapcore/encoder_test.go @@ -540,6 +540,10 @@ func TestTimeEncoders(t *testing.T) { {"nanos", int64(100050005000)}, {"", 100.050005}, {"something-random", 100.050005}, + {"rfc3339", "1970-01-01T00:01:40Z"}, + {"RFC3339", "1970-01-01T00:01:40Z"}, + {"rfc3339nano", "1970-01-01T00:01:40.050005Z"}, + {"RFC3339Nano", "1970-01-01T00:01:40.050005Z"}, } for _, tt := range tests {