Skip to content

Conversation

@DaveCTurner
Copy link
Contributor

java.time has the functionality needed to deal with timezones with varying offsets correctly, but it also has a bunch of methods that silently let you forget about the hard cases, which raises the risk that we'll quietly do the wrong thing at some point in the future.

This change adds (what I think to be) the trappy methods to the list of forbidden methods to try and help stop this from happening.

There was only one use of these methods in the codebase so far: IngestDocument#deepCopy() used ZonedDateTime.of() which may alter the offset of the given time in cases where the offset is ambiguous.

@DaveCTurner DaveCTurner added the :Data Management/Ingest Node Execution or management of Ingest Pipelines including GeoIP label Feb 1, 2018
} else if (value instanceof ZonedDateTime) {
ZonedDateTime zonedDateTime = (ZonedDateTime) value;
return ZonedDateTime.of(zonedDateTime.toLocalDate(), zonedDateTime.toLocalTime(), zonedDateTime.getZone());
return ZonedDateTime.ofInstant(zonedDateTime.toLocalDateTime(), zonedDateTime.getOffset(), zonedDateTime.getZone());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

TBH I'm wondering whether this could just use zonedDateTime directly, since it is supposed to have value semantics. Put differently, why not also deep-copy the LocalDateTime and ZoneId and so on?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure I understand. does it matter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does but I should have added a test to show this. It only very rarely matters, which is why it's trappy. I added a test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, ok, I forgot I'd made that original comment, but I think I was answering a different question in my previous reply.

The question is why not simply this:

else if (value instanceof ZonedDateTime) {
    return value;

I thought this would be ok because ZonedDateTime is immutable and we shouldn't be looking at reference equality on it. If that's not fine, because we do want the contents of the copy to share no references with the original for some reason, then what we do here doesn't do that since it makes a shallow copy of the ZonedDateTime, because it copies the LocalDateTime instance within.

Copy link
Contributor

Choose a reason for hiding this comment

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

++ on that

Copy link
Contributor

@talevy talevy left a comment

Choose a reason for hiding this comment

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

LGTM.

thanks for looking at this, I'm not entirely sure about the repercussions of your code comment, but the current changes look reasonable enough?

Copy link
Contributor

@spinscale spinscale left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for doing that! Just returning the ZonedDateTime looks good to me as well, but I might be missing why it was added in the first place (didnt check history)

@DaveCTurner
Copy link
Contributor Author

Ok, there was already a case for value types like String, Integer, ..., so I just added ZonedDateTime to that list.

@DaveCTurner DaveCTurner merged commit ab8f5ea into elastic:master Feb 2, 2018
@DaveCTurner DaveCTurner deleted the 2018-02-01-java-time-forbid-trappy-methods branch February 2, 2018 18:24
DaveCTurner added a commit that referenced this pull request Feb 2, 2018
ava.time has the functionality needed to deal with timezones with varying 
offsets correctly, but it also has a bunch of methods that silently let you
forget about the hard cases, which raises the risk that we'll quietly do the
wrong thing at some point in the future.

This change adds the trappy methods to the list of forbidden methods to try and
help stop this from happening.

It also fixes the only use of these methods in the codebase so far:
IngestDocument#deepCopy() used ZonedDateTime.of() which may alter the offset of
the given time in cases where the offset is ambiguous.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Data Management/Ingest Node Execution or management of Ingest Pipelines including GeoIP >enhancement v6.3.0 v7.0.0-beta1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants