protected Object convertIntervalDaySecond(Column column, Field fieldDefn, Object data) { return convertValue(column, fieldDefn, data, NumberConversions.DOUBLE_FALSE, (r) -> { if (data instanceof Number) { // we expect to get back from the plugin a double value r.deliver(((Number) data).doubleValue()); } else if (data instanceof INTERVALDS) { final String interval = ((INTERVALDS) data).stringValue(); final Matcher m = INTERVAL_DAY_SECOND_PATTERN.matcher(interval); if (m.matches()) { final int sign = "-".equals(m.group(1)) ? -1 : 1; r.deliver(MicroDuration.durationMicros( 0, 0, sign * Integer.valueOf(m.group(2)), sign * Integer.valueOf(m.group(3)), sign * Integer.valueOf(m.group(4)), sign * Integer.valueOf(m.group(5)), sign * Integer.valueOf(Strings.pad(m.group(6), 6, '0')), MicroDuration.DAYS_PER_MONTH_AVG)); } } }); } }
/** * Converts {@link INTERVALDS} to {@link Duration}. * * @param dbData the data from the database to be converted, possibly {@code null} * @return the converted value, possibly {@code null} */ public static Duration intervaldsToDuration(INTERVALDS dbData) { if (dbData == null) { return null; } byte[] bytes = dbData.toBytes(); int day = toUnsignedInt(bytes[0]) << 24 | toUnsignedInt(bytes[1]) << 16 | toUnsignedInt(bytes[2]) << 8 | toUnsignedInt(bytes[3]); day ^= HIGH_BIT_FLAG; int hour = toUnsignedInt(bytes[4]) - 60; int minute = toUnsignedInt(bytes[5]) - 60; int second = toUnsignedInt(bytes[6]) - 60; int nano = toUnsignedInt(bytes[7]) << 24 | toUnsignedInt(bytes[8]) << 16 | toUnsignedInt(bytes[9]) << 8 | toUnsignedInt(bytes[10]); nano ^= HIGH_BIT_FLAG; return Duration.ofDays(day) .plusHours(hour) .plusMinutes(minute) .plusSeconds(second) .plusNanos(nano); }
/** * Converts {@link INTERVALDS} to {@link Duration}. * * @param dbData the data from the database to be converted, possibly {@code null} * @return the converted value, possibly {@code null} */ public static Duration intervaldsToDuration(INTERVALDS dbData) { if (dbData == null) { return null; } byte[] bytes = dbData.toBytes(); int day = toUnsignedInt(bytes[0]) << 24 | toUnsignedInt(bytes[1]) << 16 | toUnsignedInt(bytes[2]) << 8 | toUnsignedInt(bytes[3]); day ^= HIGH_BIT_FLAG; int hour = toUnsignedInt(bytes[4]) - 60; int minute = toUnsignedInt(bytes[5]) - 60; int second = toUnsignedInt(bytes[6]) - 60; int nano = toUnsignedInt(bytes[7]) << 24 | toUnsignedInt(bytes[8]) << 16 | toUnsignedInt(bytes[9]) << 8 | toUnsignedInt(bytes[10]); nano ^= HIGH_BIT_FLAG; return Duration.ofDays(day) .plusHours(hour) .plusMinutes(minute) .plusSeconds(second) .plusNanos(nano); }