Skip to content

Commit

Permalink
Add RFC3339, RFC3339Nano and update docs to include 'nano' (#736)
Browse files Browse the repository at this point in the history
  • Loading branch information
uhthomas authored and prashantv committed Aug 21, 2019
1 parent 9a9fa7d commit 4252145
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
25 changes: 22 additions & 3 deletions zapcore/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
4 changes: 4 additions & 0 deletions zapcore/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4252145

Please sign in to comment.