use of org.knime.core.data.time.localdatetime.LocalDateTimeValue in project knime-core by knime.
the class LocalDateTimeMedianOperator method createCustomMeanMedianMetod.
private static EvenListMedianMethod createCustomMeanMedianMetod() {
return new EvenListMedianMethod() {
@Override
public DataCell extractMedian(final List<DataCell> cells, final int lowerCandidateIdx, final int upperCandidateIdx) {
final LocalDateTime dateTime1 = ((LocalDateTimeValue) cells.get(lowerCandidateIdx)).getLocalDateTime();
final LocalDateTime dateTime2 = ((LocalDateTimeValue) cells.get(upperCandidateIdx)).getLocalDateTime();
// calculate mean (seconds), keep decimals for further calculations
final double meanTimestampSeconds = (dateTime1.toEpochSecond(ZoneOffset.UTC) + dateTime2.toEpochSecond(ZoneOffset.UTC)) / 2.0;
// calculate mean (nanos), round fraction of nano
final int meanTimestampNanos = (int) (meanTimestampSeconds - (long) meanTimestampSeconds + (dateTime1.getNano() + dateTime2.getNano()) / 2.0 + 0.5);
final LocalDateTime meanDateTime = LocalDateTime.ofEpochSecond((long) meanTimestampSeconds, meanTimestampNanos, ZoneOffset.UTC);
return LocalDateTimeCellFactory.create(meanDateTime);
}
};
}
Aggregations