in lang/java/avro/src/main/java/org/apache/avro/util/TimePeriod.java [125:202]
public static TimePeriod from(TemporalAmount amount) {
if (requireNonNull(amount, "amount") instanceof TimePeriod) {
return (TimePeriod) amount;
}
if (amount instanceof ChronoPeriod) {
if (!IsoChronology.INSTANCE.equals(((ChronoPeriod) amount).getChronology())) {
throw new DateTimeException("TimePeriod requires ISO chronology: " + amount);
}
}
long months = 0;
long days = 0;
long millis = 0;
for (TemporalUnit unit : amount.getUnits()) {
if (unit instanceof ChronoUnit) {
long unitAmount = amount.get(unit);
switch ((ChronoUnit) unit) {
case MILLENNIA:
months = unsignedInt(months + unitAmount * MONTHS_PER_MILLENNIUM);
break;
case CENTURIES:
months = unsignedInt(months + unitAmount * MONTHS_PER_CENTURY);
break;
case DECADES:
months = unsignedInt(months + unitAmount * MONTHS_PER_DECADE);
break;
case YEARS:
months = unsignedInt(months + unitAmount * MONTHS_PER_YEAR);
break;
case MONTHS:
months = unsignedInt(months + unitAmount);
break;
case WEEKS:
days = unsignedInt(days + unitAmount * 7);
break;
case DAYS:
days = unsignedInt(days + unitAmount);
break;
case HALF_DAYS:
days = unsignedInt(days + (unitAmount / 2)); // Truncates halves
if (unitAmount % 2 != 0) {
millis = unsignedInt(millis + MILLIS_IN_HALF_DAY);
}
break;
case HOURS:
millis = unsignedInt(millis + unitAmount * MILLIS_PER_HOUR);
break;
case MINUTES:
millis = unsignedInt(millis + unitAmount * MILLIS_PER_MINUTE);
break;
case SECONDS:
millis = unsignedInt(millis + unitAmount * MILLIS_PER_SECOND);
break;
case MILLIS:
millis = unsignedInt(millis + unitAmount);
break;
case MICROS:
if (unitAmount % MICROS_PER_MILLI != 0) {
throw new DateTimeException(
"Cannot add " + unitAmount + " microseconds: not a whole number of milliseconds");
}
millis = unsignedInt(millis + unitAmount / MICROS_PER_MILLI);
break;
case NANOS:
if (unitAmount % NANOS_PER_MILLI != 0) {
throw new DateTimeException(
"Cannot add " + unitAmount + " nanoseconds: not a whole number of milliseconds");
}
millis = unsignedInt(millis + unitAmount / NANOS_PER_MILLI);
break;
default:
throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
}
} else {
throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
}
}
return new TimePeriod(months, days, millis);
}