subPeriod = (int) (orig - time * sz); // can cast because getSize is int
}
if (basePeriodPattern != null)
time = basePeriodPattern.expandIndex(time);
TimeParts tp = new TimeParts();
Resolution unit = this.baseUnit;
switch (unit) {
case YEAR:
tp.setYear(time);
break;
case MONTH:
tp.setYear(time / 12);
tp.setMonth((int) (time - tp.getYear() * 12) + 1);
break;
case DAY:
TimeTools.computeYMD(time, tp);
break;
case HOUR:
long days = time / 24;
tp.setHour((int) (time - days * 24));
TimeTools.computeYMD(days, tp);
break;
case MIN:
days = time/ (24 * 60);
long minutes = time - days * 24 * 60;
tp.setHour((int)(minutes / 60));
tp.setMin((int) (minutes - tp.getHour() * 60));
TimeTools.computeYMD(days, tp);
break;
case SEC:
days = time / (24 * 60 * 60);
long seconds = time - days * 24L * 60L * 60L;
TimeTools.computeYMD(days, tp);
TimeTools.computeHMS(seconds, tp);
break;
case MSEC:
days = time / (24L * 60L * 60L * 1000L);
long millis = time - days * 24L * 60L * 60L * 1000L;
seconds = millis / 1000L;
tp.setUsec((int) (millis - seconds * 1000L) * 1000);
TimeTools.computeYMD(days, tp);
TimeTools.computeHMS(seconds, tp);
break;
case USEC:
days = time / (24L * 60L * 60L * 1000000L);
long micros = time - days * 24L * 60L * 60L * 1000000L;
seconds = micros / 1000000L;
tp.setUsec((int) (micros - seconds * 1000000L));
TimeTools.computeYMD(days, tp);
TimeTools.computeHMS(seconds, tp);
break;
default:
throw new RuntimeException("bug: " + unit.name());
}
if (subPeriodPattern != null) {
// there is something to do even when subPeriod = 0
subPeriodPattern.fillInSubPeriod(subPeriod, tp);
}
// make sure nothing is negative
if (tp.anyNegative())
throw new RuntimeException(String.format("(bug) time=%d %s", time, tp.toString()));
return tp;
}