You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/11/16 09:22:59 UTC
[GitHub] [iceberg] szehon-ho commented on a diff in pull request #6199: API, Core: Move micros and days conversions to DateTimeUtil
szehon-ho commented on code in PR #6199:
URL: https://github.com/apache/iceberg/pull/6199#discussion_r1023728546
##########
api/src/main/java/org/apache/iceberg/util/DateTimeUtil.java:
##########
@@ -133,4 +134,56 @@ public static long isoTimestampToMicros(String timestampString) {
return microsFromTimestamp(
LocalDateTime.parse(timestampString, DateTimeFormatter.ISO_LOCAL_DATE_TIME));
}
+
+ public static int daysToYears(int days) {
+ 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) {
+ if (micros >= 0) {
+ long epochSecond = Math.floorDiv(micros, MICROS_PER_SECOND);
Review Comment:
Nit: am I missing something or can we just pull these out of individual blocks?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org