Search in sources :

Example 1 with Granularity

use of org.knime.time.util.Granularity in project knime-core by knime.

the class DateTimeDifferenceNodeModel method calculateTime.

private DataCell calculateTime(final DataCell cell, final DataCell referenceCell, final ZonedDateTime fixedDateTime) {
    if (cell.isMissing()) {
        return new MissingCell("Cell for calculating difference is missing.");
    } else if (fixedDateTime == null && referenceCell.isMissing()) {
        return new MissingCell("Reference cell for calculating difference is missing.");
    }
    final Temporal temporal1;
    final Temporal temporal2;
    if (cell instanceof ZonedDateTimeValue) {
        temporal1 = ((ZonedDateTimeValue) cell).getZonedDateTime();
        temporal2 = fixedDateTime == null ? ((ZonedDateTimeValue) referenceCell).getZonedDateTime() : fixedDateTime;
    } else if (cell instanceof LocalDateTimeValue) {
        temporal1 = ((LocalDateTimeValue) cell).getLocalDateTime();
        temporal2 = fixedDateTime == null ? ((LocalDateTimeValue) referenceCell).getLocalDateTime() : fixedDateTime.toLocalDateTime();
    } else {
        temporal1 = ((LocalTimeValue) cell).getLocalTime();
        temporal2 = fixedDateTime == null ? ((LocalTimeValue) referenceCell).getLocalTime() : fixedDateTime.toLocalTime();
    }
    if (m_calculationSelectModel.getStringValue().equals(OutputMode.Duration.name())) {
        final Duration diffDuration = Duration.between(temporal1, temporal2);
        return DurationCellFactory.create(diffDuration);
    } else {
        final Granularity granularity = Granularity.fromString(m_granularityModel.getStringValue());
        return LongCellFactory.create(granularity.between(temporal1, temporal2));
    }
}
Also used : LocalTimeValue(org.knime.core.data.time.localtime.LocalTimeValue) Temporal(java.time.temporal.Temporal) MissingCell(org.knime.core.data.MissingCell) Duration(java.time.Duration) Granularity(org.knime.time.util.Granularity) ZonedDateTimeValue(org.knime.core.data.time.zoneddatetime.ZonedDateTimeValue) LocalDateTimeValue(org.knime.core.data.time.localdatetime.LocalDateTimeValue)

Aggregations

Duration (java.time.Duration)1 Temporal (java.time.temporal.Temporal)1 MissingCell (org.knime.core.data.MissingCell)1 LocalDateTimeValue (org.knime.core.data.time.localdatetime.LocalDateTimeValue)1 LocalTimeValue (org.knime.core.data.time.localtime.LocalTimeValue)1 ZonedDateTimeValue (org.knime.core.data.time.zoneddatetime.ZonedDateTimeValue)1 Granularity (org.knime.time.util.Granularity)1