Skip to content

Commit

Permalink
Keep time location in zap.Time (#425)
Browse files Browse the repository at this point in the history
Preserve time zone information when using `zap.Time`.
  • Loading branch information
hnakamur authored and akshayjshah committed May 12, 2017
1 parent 6240cf5 commit 909de71
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion field.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
4 changes: 2 additions & 2 deletions field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
Expand Down
7 changes: 6 additions & 1 deletion zapcore/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion zapcore/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
Expand Down

0 comments on commit 909de71

Please sign in to comment.