/** * Converts {@link Period} to {@link INTERVALYM}. * * @param attribute the value to be converted, possibly {@code null} * @return the converted data, possibly {@code null} */ public static INTERVALYM periodToIntervalym(Period attribute) { if (attribute == null) { return null; } if (attribute.getDays() != 0) { throw new IllegalArgumentException("days are not supported"); } byte[] bytes = newIntervalymBuffer(); int year = attribute.getYears() ^ HIGH_BIT_FLAG; bytes[0] = (byte) (year >> 24); bytes[1] = (byte) (year >> 16 & 0xFF); bytes[2] = (byte) (year >> 8 & 0xFF); bytes[3] = (byte) (year & 0xFF); int month = attribute.getMonths() + 60; bytes[4] = (byte) (month & 0xFF); return new INTERVALYM(bytes); }
protected Object convertIntervalYearMonth(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 INTERVALYM) { final String interval = ((INTERVALYM) data).stringValue(); int sign = 1; int start = 0; if (interval.charAt(0) == '-') { sign = -1; start = 1; } for (int i = 1; i < interval.length(); i++) { if (interval.charAt(i) == '-') { final int year = sign * Integer.parseInt(interval.substring(start, i)); final int month = sign * Integer.parseInt(interval.substring(i + 1, interval.length())); r.deliver(MicroDuration.durationMicros(year, month, 0, 0, 0, 0, MicroDuration.DAYS_PER_MONTH_AVG)); } } } }); }
/** * Converts {@link INTERVALYM} to {@link Period}. * * @param dbData the data from the database to be converted, possibly {@code null} * @return the converted value, possibly {@code null} */ public static Period intervalymToPeriod(INTERVALYM dbData) { if (dbData == null) { return null; } byte[] bytes = dbData.toBytes(); int year = toUnsignedInt(bytes[0]) << 24 | toUnsignedInt(bytes[1]) << 16 | toUnsignedInt(bytes[2]) << 8 | toUnsignedInt(bytes[3]); year ^= HIGH_BIT_FLAG; int month = toUnsignedInt(bytes[4]) - 60; return Period.of(year, month, 0); }
/** * Converts {@link INTERVALYM} to {@link Period}. * * @param dbData the data from the database to be converted, possibly {@code null} * @return the converted value, possibly {@code null} */ public static Period intervalymToPeriod(INTERVALYM dbData) { if (dbData == null) { return null; } byte[] bytes = dbData.toBytes(); int year = toUnsignedInt(bytes[0]) << 24 | toUnsignedInt(bytes[1]) << 16 | toUnsignedInt(bytes[2]) << 8 | toUnsignedInt(bytes[3]); year ^= HIGH_BIT_FLAG; int month = toUnsignedInt(bytes[4]) - 60; return Period.of(year, month, 0); }
/** * Converts {@link Period} to {@link INTERVALYM}. * * @param attribute the value to be converted, possibly {@code null} * @return the converted data, possibly {@code null} */ public static INTERVALYM periodToIntervalym(Period attribute) { if (attribute == null) { return null; } if (attribute.getDays() != 0) { throw new IllegalArgumentException("days are not supported"); } byte[] bytes = newIntervalymBuffer(); int year = attribute.getYears() ^ HIGH_BIT_FLAG; bytes[0] = (byte) (year >> 24); bytes[1] = (byte) (year >> 16 & 0xFF); bytes[2] = (byte) (year >> 8 & 0xFF); bytes[3] = (byte) (year & 0xFF); int month = attribute.getMonths() + 60; bytes[4] = (byte) (month & 0xFF); return new INTERVALYM(bytes); }