diff --git a/field.go b/field.go index 7d65f33e6..dd558fc23 100644 --- a/field.go +++ b/field.go @@ -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) ) diff --git a/field_test.go b/field_test.go index 73901a37d..c0bdcd261 100644 --- a/field_test.go +++ b/field_test.go @@ -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))}, diff --git a/zapcore/field_test.go b/zapcore/field_test.go index 0ff876654..95444a8a4 100644 --- a/zapcore/field_test.go +++ b/zapcore/field_test.go @@ -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 @@ -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, }, {