Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep time location in zap.Time #425

Merged
merged 7 commits into from
May 12, 2017

Conversation

hnakamur
Copy link
Contributor

@hnakamur hnakamur commented May 4, 2017

The zap.Time() did convert the time value to integer using time.UnixNano().

zap/field.go

Line 178 in 7641ffe

return zapcore.Field{Key: key, Type: zapcore.TimeType, Integer: val.UnixNano()}

This means we lost the location information.
So EncodeEntry() for JSONEncoder and ISO8601TimeEncoder returned the time string with the local timezone, not the original location specified in the time value passed to zap.Time().

I add a test case to reproduce this problem at zapcore/json_encoder_entry_test.go.
Notice this test case fails only for environments whose timezone is not UTC.

This pull request fixes this problem with setting the time value to the Interface field of Field instead of Integer field.

To maintain backward compatibility, I modified the TimeType case in func (f Field) AddTo(enc ObjectEncoder) to use Interface field when it is set (not nil) and use Integer field otherwise.

Could you review this pull request?
Thanks!

@CLAassistant
Copy link

CLAassistant commented May 4, 2017

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@akshayjshah akshayjshah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting - I hadn't considered the need to preserve time zones. This PR can't be merged as-is, since it introduces a performance regression in zap.Time.

However, we may be able to solve this problem another way. Can you clarify for me exactly what you need here? Do you need to preserve the original time zone, or do you need to encode all times in a single (non-system) time zone?

// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch... :(

Thanks for adding the license.

field.go Outdated
@@ -175,7 +175,8 @@ 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()}
// Use Interface instead of Integer here to keep location in time.
return zapcore.Field{Key: key, Type: zapcore.TimeType, Interface: val}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this makes every call to Time allocate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made another try: 384c2f1
This time, we don't have extra allocate.

@hnakamur
Copy link
Contributor Author

hnakamur commented May 6, 2017

Thanks for your comments.
I need to preserve the original time zone.
I think times in UTC should be printed with timezone 'Z' and times in JST+9 should be printed with timezone '+0900'.
The current zap prints timezone '+0900' for times in UTC too.

Copy link
Contributor

@akshayjshah akshayjshah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great - thanks for the contribution!

@akshayjshah akshayjshah merged commit 909de71 into uber-go:master May 12, 2017
@hnakamur
Copy link
Contributor Author

Thanks for merging!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants