Skip to content

Commit

Permalink
Keep time location in Inteface field in Field
Browse files Browse the repository at this point in the history
  • Loading branch information
hnakamur committed May 6, 2017
1 parent e3dfbef commit 384c2f1
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 6 deletions.
3 changes: 1 addition & 2 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +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 {
// Use Interface instead of Integer here to keep location in time.
return zapcore.Field{Key: key, Type: zapcore.TimeType, Interface: val}
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, Interface: time.Unix(0, 0)}, Time("k", time.Unix(0, 0))},
{"Time", zapcore.Field{Key: "k", Type: zapcore.TimeType, Interface: time.Unix(0, 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
2 changes: 1 addition & 1 deletion zapcore/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (f Field) AddTo(enc ObjectEncoder) {
enc.AddString(f.Key, f.String)
case TimeType:
if f.Interface != nil {
enc.AddTime(f.Key, f.Interface.(time.Time))
enc.AddTime(f.Key, time.Unix(0, f.Integer).In(f.Interface.(*time.Location)))
} else {
// For backward compatibility, we use f.Integer when f.Interface is nil.
enc.AddTime(f.Key, time.Unix(0, f.Integer))
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 384c2f1

Please sign in to comment.