From 909de71e75bcfc2b4105a0b6f0109f3724747bbe Mon Sep 17 00:00:00 2001 From: Hiroaki Nakamura Date: Sat, 13 May 2017 05:49:59 +0900 Subject: [PATCH] Keep time location in zap.Time (#425) Preserve time zone information when using `zap.Time`. --- field.go | 2 +- field_test.go | 4 ++-- zapcore/field.go | 7 ++++++- zapcore/field_test.go | 2 +- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/field.go b/field.go index 9a8193266..c81a13d7f 100644 --- a/field.go +++ b/field.go @@ -175,7 +175,7 @@ func Stringer(key string, val fmt.Stringer) zapcore.Field { // Time constructs a zapcore.Field with the given key and value. The encoder // controls how the time is serialized. func Time(key string, val time.Time) zapcore.Field { - return zapcore.Field{Key: key, Type: zapcore.TimeType, Integer: val.UnixNano()} + return zapcore.Field{Key: key, Type: zapcore.TimeType, Integer: val.UnixNano(), Interface: val.Location()} } // Error is shorthand for the common idiom NamedError("error", err). diff --git a/field_test.go b/field_test.go index c932c319d..f19c4ae28 100644 --- a/field_test.go +++ b/field_test.go @@ -84,8 +84,8 @@ func TestFieldConstructors(t *testing.T) { {"Int16", zapcore.Field{Key: "k", Type: zapcore.Int16Type, Integer: 1}, Int16("k", 1)}, {"Int8", zapcore.Field{Key: "k", Type: zapcore.Int8Type, Integer: 1}, Int8("k", 1)}, {"String", zapcore.Field{Key: "k", Type: zapcore.StringType, String: "foo"}, String("k", "foo")}, - {"Time", zapcore.Field{Key: "k", Type: zapcore.TimeType, Integer: 0}, Time("k", time.Unix(0, 0))}, - {"Time", zapcore.Field{Key: "k", Type: zapcore.TimeType, Integer: 1000}, Time("k", time.Unix(0, 1000))}, + {"Time", zapcore.Field{Key: "k", Type: zapcore.TimeType, Integer: 0, Interface: time.UTC}, Time("k", time.Unix(0, 0).In(time.UTC))}, + {"Time", zapcore.Field{Key: "k", Type: zapcore.TimeType, Integer: 1000, Interface: time.UTC}, Time("k", time.Unix(0, 1000).In(time.UTC))}, {"Uint", zapcore.Field{Key: "k", Type: zapcore.Uint64Type, Integer: 1}, Uint("k", 1)}, {"Uint64", zapcore.Field{Key: "k", Type: zapcore.Uint64Type, Integer: 1}, Uint64("k", 1)}, {"Uint32", zapcore.Field{Key: "k", Type: zapcore.Uint32Type, Integer: 1}, Uint32("k", 1)}, diff --git a/zapcore/field.go b/zapcore/field.go index 063d7ba31..4f2e2eb6f 100644 --- a/zapcore/field.go +++ b/zapcore/field.go @@ -137,7 +137,12 @@ func (f Field) AddTo(enc ObjectEncoder) { case StringType: enc.AddString(f.Key, f.String) case TimeType: - enc.AddTime(f.Key, time.Unix(0, f.Integer)) + if f.Interface != nil { + enc.AddTime(f.Key, time.Unix(0, f.Integer).In(f.Interface.(*time.Location))) + } else { + // Fall back to UTC if location is nil. + enc.AddTime(f.Key, time.Unix(0, f.Integer)) + } case Uint64Type: enc.AddUint64(f.Key, uint64(f.Integer)) case Uint32Type: diff --git a/zapcore/field_test.go b/zapcore/field_test.go index 16a1ff5d0..41b7269d6 100644 --- a/zapcore/field_test.go +++ b/zapcore/field_test.go @@ -118,7 +118,7 @@ func TestFields(t *testing.T) { {t: Int16Type, i: 42, want: int16(42)}, {t: Int8Type, i: 42, want: int8(42)}, {t: StringType, s: "foo", want: "foo"}, - {t: TimeType, i: 1000, want: time.Unix(0, 1000)}, + {t: TimeType, i: 1000, iface: time.UTC, want: time.Unix(0, 1000).In(time.UTC)}, {t: Uint64Type, i: 42, want: uint64(42)}, {t: Uint32Type, i: 42, want: uint32(42)}, {t: Uint16Type, i: 42, want: uint16(42)},