Skip to content

Commit

Permalink
_minTime should use math.MinInt64, and test for it
Browse files Browse the repository at this point in the history
Co-Authored-By: Abhinav Gupta <abg@uber.com>
  • Loading branch information
prashantv and abhinav committed Mar 26, 2020
1 parent e48bc3e commit 1ffdd52
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion field.go
Expand Up @@ -33,7 +33,7 @@ import (
type Field = zapcore.Field

var (
_minTimeInt64 = time.Unix(0, 0)
_minTimeInt64 = time.Unix(0, math.MinInt64)
_maxTimeInt64 = time.Unix(0, math.MaxInt64)
)

Expand Down
1 change: 1 addition & 0 deletions field_test.go
Expand Up @@ -108,6 +108,7 @@ func TestFieldConstructors(t *testing.T) {
{"String", Field{Key: "k", Type: zapcore.StringType, String: "foo"}, String("k", "foo")},
{"Time", Field{Key: "k", Type: zapcore.TimeType, Integer: 0, Interface: time.UTC}, Time("k", time.Unix(0, 0).In(time.UTC))},
{"Time", Field{Key: "k", Type: zapcore.TimeType, Integer: 1000, Interface: time.UTC}, Time("k", time.Unix(0, 1000).In(time.UTC))},
{"Time", Field{Key: "k", Type: zapcore.TimeType, Integer: math.MintInt64, Interface: time.UTC}, Time("k", time.Unix(0, math.MinInt64).In(time.UTC))},
{"Time", Field{Key: "k", Type: zapcore.TimeType, Integer: math.MaxInt64, Interface: time.UTC}, Time("k", time.Unix(0, math.MaxInt64).In(time.UTC))},
{"Time", Field{Key: "k", Type: zapcore.TimeFullType, Interface: time.Time{}}, Time("k", time.Time{})},
{"Time", Field{Key: "k", Type: zapcore.TimeFullType, Interface: time.Unix(math.MaxInt64, 0)}, Time("k", time.Unix(math.MaxInt64, 0))},
Expand Down
20 changes: 14 additions & 6 deletions zapcore/field_test.go
Expand Up @@ -165,9 +165,13 @@ func TestFields(t *testing.T) {
}

func TestEquals(t *testing.T) {
timeOutOfRange := time.Unix(0, math.MaxInt64).Add(time.Nanosecond)
timeOutOfRangeNano := time.Unix(0, timeOutOfRange.UnixNano())
require.NotEqual(t, timeOutOfRange, timeOutOfRangeNano, "should be different as value is out of UnixNano range")
// Values outside the UnixNano range were encoded incorrectly (#737, #803).
timeOutOfRangeHigh := time.Unix(0, math.MaxInt64).Add(time.Nanosecond)
timeOutOfRangeLow := time.Unix(0, math.MinInt64).Add(-time.Nanosecond)
timeOutOfRangeHighNano := time.Unix(0, timeOutOfRangeHigh.UnixNano())
timeOutOfRangeLowNano := time.Unix(0, timeOutOfRangeLow.UnixNano())
require.NotEqual(t, timeOutOfRangeHigh, timeOutOfRangeHighNano, "should be different as value is > UnixNano range")
require.NotEqual(t, timeOutOfRangeHigh, timeOutOfRangeHighNano, "should be different as value is < UnixNano range")

tests := []struct {
a, b Field
Expand Down Expand Up @@ -204,9 +208,13 @@ func TestEquals(t *testing.T) {
want: false,
},
{
// Values outside the UnixNano range were encoded incorrectly (#737, #803).
a: zap.Time("k", timeOutOfRange),
b: zap.Time("k", timeOutOfRangeNano),
a: zap.Time("k", timeOutOfRangeLow),
b: zap.Time("k", timeOutOfRangeLowNano),
want: false,
},
{
a: zap.Time("k", timeOutOfRangeHigh),
b: zap.Time("k", timeOutOfRangeHighNano),
want: false,
},
{
Expand Down

0 comments on commit 1ffdd52

Please sign in to comment.