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

Assorted ZoneOffset improvements #38

Merged
merged 2 commits into from
Apr 11, 2022

Conversation

rickie
Copy link
Member

@rickie rickie commented Feb 7, 2022

  • StaticImport: Add ZoneOffset.UTC as candidate.
  • TimeTemplates: Prefer Instant#atOffSet over OffsetDateTime#ofInstant.

@rickie rickie requested review from Stephan202 and werli February 7, 2022 10:35
@rickie
Copy link
Member Author

rickie commented Feb 7, 2022

(Had a bit of a struggle to come up with a good name for the PR... so ideas welcome 😄)

@rickie
Copy link
Member Author

rickie commented Feb 15, 2022

Validated downstream, results are promising 😄.

Copy link
Member

@werli werli left a comment

Choose a reason for hiding this comment

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

Code looks and straightforward. Nice small improvement. 🚀

@Stephan202 Stephan202 force-pushed the rossendrijver/zoneoffset_improvements branch from 82943cf to 0bfa9e1 Compare April 10, 2022 11:41
Copy link
Member

@Stephan202 Stephan202 left a comment

Choose a reason for hiding this comment

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

Rebased and added a small commit. Suggested commit message:

Suggest assorted canonical `ZoneOffset` usages (#38)

- Statically import `ZoneOffset.UTC`.
- Prefer `Instant#atOffset` over `OffsetDateTime#ofInstant`.

Comment on lines 148 to 149
" ZoneOffset z1 = ZoneOffset.UTC;",
" ZoneOffset z2 = ZoneOffset.MIN;",
Copy link
Member

Choose a reason for hiding this comment

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

In this case we can use the identification test to verify that only one is flagged; the replacement logic is already sufficiently covered.

Comment on lines +62 to +73
/** Prefer {@link Instant#atOffset(ZoneOffset)} over the more verbose alternative. */
static final class InstantAtOffset {
@BeforeTemplate
OffsetDateTime before(Instant instant, ZoneOffset zoneOffset) {
return OffsetDateTime.ofInstant(instant, zoneOffset);
}

@AfterTemplate
OffsetDateTime after(Instant instant, ZoneOffset zoneOffset) {
return instant.atOffset(zoneOffset);
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Not for this PR, but something to ponder: ideally we generalize a rule such as this one, such that OffsetDateTime::ofInstant is also rewritten to Instant::atOffset. (Or not 🤔.)

@Stephan202 Stephan202 added this to the 0.1.0 milestone Apr 10, 2022
@rickie
Copy link
Member Author

rickie commented Apr 11, 2022

Changes LGTM.

@Stephan202 Stephan202 merged commit fea8f5b into master Apr 11, 2022
@Stephan202 Stephan202 deleted the rossendrijver/zoneoffset_improvements branch April 11, 2022 08:16
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.

4 participants