diff --git a/console.go b/console.go index 2c28754..15bd063 100644 --- a/console.go +++ b/console.go @@ -299,7 +299,12 @@ func consoleDefaultFormatTimestamp(timeFormat string, noColor bool) Formatter { if err != nil { t = tt.String() } else { - ts := time.Unix(i, 0) + var sec, nsec int64 = i, 0 + if TimeFieldFormat == TimeFormatUnixMs { + nsec = int64(time.Duration(i) * time.Millisecond) + sec = 0 + } + ts := time.Unix(sec, nsec).UTC() t = ts.Format(timeFormat) } } diff --git a/console_test.go b/console_test.go index 2cc335a..4567e9e 100644 --- a/console_test.go +++ b/console_test.go @@ -129,14 +129,36 @@ func TestConsoleWriter(t *testing.T) { zerolog.TimeFieldFormat = zerolog.TimeFormatUnix buf := &bytes.Buffer{} - w := zerolog.ConsoleWriter{Out: buf, NoColor: true} + w := zerolog.ConsoleWriter{Out: buf, TimeFormat: time.StampMilli, NoColor: true} - _, err := w.Write([]byte(`{"time": 0, "level": "debug", "message": "Foobar", "foo": "bar"}`)) + _, err := w.Write([]byte(`{"time": 1234, "level": "debug", "message": "Foobar", "foo": "bar"}`)) if err != nil { t.Errorf("Unexpected error when writing output: %s", err) } - expectedOutput := "4:00PM DBG Foobar foo=bar\n" + expectedOutput := "Jan 1 00:20:34.000 DBG Foobar foo=bar\n" + actualOutput := buf.String() + if actualOutput != expectedOutput { + t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput) + } + }) + + t.Run("Unix timestamp ms input format", func(t *testing.T) { + of := zerolog.TimeFieldFormat + defer func() { + zerolog.TimeFieldFormat = of + }() + zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs + + buf := &bytes.Buffer{} + w := zerolog.ConsoleWriter{Out: buf, TimeFormat: time.StampMilli, NoColor: true} + + _, err := w.Write([]byte(`{"time": 1234567, "level": "debug", "message": "Foobar", "foo": "bar"}`)) + if err != nil { + t.Errorf("Unexpected error when writing output: %s", err) + } + + expectedOutput := "Jan 1 00:20:34.567 DBG Foobar foo=bar\n" actualOutput := buf.String() if actualOutput != expectedOutput { t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)