-
Notifications
You must be signed in to change notification settings - Fork 3k
API, Core: Move micros and days conversions to DateTimeUtil #6199
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,7 @@ private DateTimeUtil() {} | |
| public static final OffsetDateTime EPOCH = Instant.ofEpochSecond(0).atOffset(ZoneOffset.UTC); | ||
| public static final LocalDate EPOCH_DAY = EPOCH.toLocalDate(); | ||
| public static final long MICROS_PER_MILLIS = 1000L; | ||
| public static final long MICROS_PER_SECOND = 1_000_000L; | ||
|
|
||
| public static LocalDate dateFromDays(int daysFromEpoch) { | ||
| return ChronoUnit.DAYS.addTo(EPOCH_DAY, daysFromEpoch); | ||
|
|
@@ -133,4 +134,58 @@ public static long isoTimestampToMicros(String timestampString) { | |
| return microsFromTimestamp( | ||
| LocalDateTime.parse(timestampString, DateTimeFormatter.ISO_LOCAL_DATE_TIME)); | ||
| } | ||
|
|
||
| public static int daysToYears(int days) { | ||
aokolnychyi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return convertDays(days, ChronoUnit.YEARS); | ||
| } | ||
|
|
||
| public static int daysToMonths(int days) { | ||
| return convertDays(days, ChronoUnit.MONTHS); | ||
| } | ||
|
|
||
| private static int convertDays(int days, ChronoUnit granularity) { | ||
| if (days >= 0) { | ||
| LocalDate date = EPOCH_DAY.plusDays(days); | ||
| return (int) granularity.between(EPOCH_DAY, date); | ||
| } else { | ||
| // add 1 day to the value to account for the case where there is exactly 1 unit between the | ||
| // date and epoch because the result will always be decremented. | ||
| LocalDate date = EPOCH_DAY.plusDays(days + 1); | ||
| return (int) granularity.between(EPOCH_DAY, date) - 1; | ||
| } | ||
| } | ||
|
|
||
| public static int microsToYears(long micros) { | ||
| return convertMicros(micros, ChronoUnit.YEARS); | ||
| } | ||
|
|
||
| public static int microsToMonths(long micros) { | ||
| return convertMicros(micros, ChronoUnit.MONTHS); | ||
| } | ||
|
|
||
| public static int microsToDays(long micros) { | ||
| return convertMicros(micros, ChronoUnit.DAYS); | ||
| } | ||
|
|
||
| public static int microsToHours(long micros) { | ||
| return convertMicros(micros, ChronoUnit.HOURS); | ||
| } | ||
|
|
||
| private static int convertMicros(long micros, ChronoUnit granularity) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These conversions are covered by |
||
| if (micros >= 0) { | ||
| long epochSecond = Math.floorDiv(micros, MICROS_PER_SECOND); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: am I missing something or can we just pull these out of individual blocks?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can pull only
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved that var but it became somehow disconnected from the rest of the logic in each branch. I kept it separate like in the original snippet. |
||
| long nanoAdjustment = Math.floorMod(micros, MICROS_PER_SECOND) * 1000; | ||
| return (int) granularity.between(EPOCH, toOffsetDateTime(epochSecond, nanoAdjustment)); | ||
| } else { | ||
| // add 1 micro to the value to account for the case where there is exactly 1 unit between | ||
| // the timestamp and epoch because the result will always be decremented. | ||
| long epochSecond = Math.floorDiv(micros, MICROS_PER_SECOND); | ||
| long nanoAdjustment = Math.floorMod(micros + 1, MICROS_PER_SECOND) * 1000; | ||
| return (int) granularity.between(EPOCH, toOffsetDateTime(epochSecond, nanoAdjustment)) - 1; | ||
| } | ||
| } | ||
|
|
||
| private static OffsetDateTime toOffsetDateTime(long epochSecond, long nanoAdjustment) { | ||
| return Instant.ofEpochSecond(epochSecond, nanoAdjustment).atOffset(ZoneOffset.UTC); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block is now similar to
toHumanStringbelow.