Search in sources :

Example 16 with Period

use of java.time.Period in project knime-core by knime.

the class CreateDateTimeNodeDialog method updateWarningLabel.

private void updateWarningLabel() {
    m_warningLabel.setText("");
    // === check period duration field ===
    if (m_dialogCompDuration.getModel().isEnabled() && m_updateWarningLabel) {
        final DateTimeType selectedItem = (DateTimeType) m_typeCombobox.getSelectedItem();
        Duration duration = null;
        Period period = null;
        if (((SettingsModelString) m_dialogCompDuration.getModel()).getStringValue() == null) {
            m_warningLabel.setText("Please enter an interval.");
            return;
        }
        try {
            duration = DurationPeriodFormatUtils.parseDuration(((SettingsModelString) m_dialogCompDuration.getModel()).getStringValue());
            if (selectedItem.equals(DateTimeType.LOCAL_DATE)) {
                m_warningLabel.setText("A time-based duration cannot be applied to a local date.");
                return;
            }
        } catch (DateTimeParseException e1) {
            try {
                period = DurationPeriodFormatUtils.parsePeriod(((SettingsModelString) m_dialogCompDuration.getModel()).getStringValue());
                if (selectedItem.equals(DateTimeType.LOCAL_TIME)) {
                    m_warningLabel.setText("A date-based duration cannot be applied to a local time.");
                    return;
                }
            } catch (DateTimeParseException e2) {
                m_warningLabel.setText("Value does not represent a duration.");
                return;
            }
        }
        if (((SettingsModelString) m_dialogCompRowNrOptionSelection.getModel()).getStringValue().equals(RowNrMode.Variable.name())) {
            // === check that duration is not zero and row number variable ===
            if ((duration != null && duration.isZero()) || (period != null && period.isZero())) {
                m_warningLabel.setText("Interval must not be zero.");
            }
            // === check that start is before end and duration positive and vice versa ===
            final Temporal start = ((SettingsModelBoolean) m_dialogCompStartUseExecTime.getModel()).getBooleanValue() ? CreateDateTimeNodeModel.getTemporalExecTimeWithFormat(((SettingsModelDateTime) m_dialogCompEnd.getModel()).getSelectedDateTime()) : ((SettingsModelDateTime) m_dialogCompStart.getModel()).getSelectedDateTime();
            final Temporal end = ((SettingsModelBoolean) m_dialogCompEndUseExecTime.getModel()).getBooleanValue() ? CreateDateTimeNodeModel.getTemporalExecTimeWithFormat(((SettingsModelDateTime) m_dialogCompStart.getModel()).getSelectedDateTime()) : ((SettingsModelDateTime) m_dialogCompEnd.getModel()).getSelectedDateTime();
            if (!selectedItem.equals(DateTimeType.LOCAL_TIME)) {
                final boolean isStartBeforeEnd;
                final boolean isEqual;
                if (selectedItem.equals(DateTimeType.LOCAL_DATE)) {
                    isStartBeforeEnd = ((LocalDate) start).isBefore((LocalDate) end);
                    isEqual = ((LocalDate) start).isEqual((LocalDate) end);
                } else if (selectedItem.equals(DateTimeType.LOCAL_DATE_TIME)) {
                    isStartBeforeEnd = ((LocalDateTime) start).isBefore((LocalDateTime) end);
                    isEqual = ((LocalDateTime) start).isEqual((LocalDateTime) end);
                } else {
                    isStartBeforeEnd = (((ZonedDateTime) start)).toLocalDateTime().isBefore(((LocalDateTime) end));
                    isEqual = (((ZonedDateTime) start)).toLocalDateTime().isEqual(((LocalDateTime) end));
                }
                final boolean isDurationNegative;
                if (duration != null) {
                    isDurationNegative = duration.isNegative();
                } else if (period != null) {
                    isDurationNegative = period.isNegative();
                } else {
                    throw new IllegalStateException("Either duration or period must not be null!");
                }
                if (isStartBeforeEnd && isDurationNegative) {
                    m_warningLabel.setText("Start is before end, but the interval is negative.");
                    return;
                }
                if (!isStartBeforeEnd && !isEqual && !isDurationNegative) {
                    m_warningLabel.setText("Start is after end, but the interval is positive.");
                    return;
                }
            }
        }
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) DateTimeParseException(java.time.format.DateTimeParseException) DateTimeType(org.knime.time.util.DateTimeType) Temporal(java.time.temporal.Temporal) ZonedDateTime(java.time.ZonedDateTime) SettingsModelDateTime(org.knime.time.util.SettingsModelDateTime) Period(java.time.Period) Duration(java.time.Duration) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) LocalDate(java.time.LocalDate)

Example 17 with Period

use of java.time.Period in project knime-core by knime.

the class CreateDateTimeNodeModel method createByVariableRowNr.

/**
 * Create date&time row with a variable number of rows depending on a given starting point, a duration/period and an
 * ending point.
 */
private void createByVariableRowNr(final BufferedDataContainer container, final Temporal startDateTime, final Temporal endDateTime, final TemporalAmount durationOrPeriod) throws InvalidSettingsException {
    Temporal start = startDateTime;
    Temporal end = endDateTime;
    // check if duration is zero
    if ((durationOrPeriod instanceof Period) && ((Period) durationOrPeriod).isZero()) {
        setWarningMessage("Interval is zero! Node created an empty table.");
        return;
    } else if ((durationOrPeriod instanceof Duration) && ((Duration) durationOrPeriod).isZero()) {
        setWarningMessage("Interval is zero! Node created an empty table.");
        return;
    }
    // === check if start date is after end ===
    boolean wasLocalTime = start instanceof LocalTime;
    final boolean isStartAfterEnd;
    if (start instanceof LocalDate) {
        isStartAfterEnd = ((LocalDate) start).isAfter((LocalDate) end);
    } else if (start instanceof LocalTime) {
        // because of the problem that 00:00 is before 23:59, a local time needs to be temporarily converted
        // to a local date time
        boolean isLocalTimeStartAfterEnd = ((LocalTime) start).isAfter((LocalTime) end);
        final boolean isDurationNegative = ((Duration) durationOrPeriod).isNegative();
        int daysAddedToEnd = 0;
        if (isLocalTimeStartAfterEnd && !isDurationNegative) {
            daysAddedToEnd = 1;
        }
        if (!isLocalTimeStartAfterEnd && isDurationNegative) {
            daysAddedToEnd = -1;
        }
        if (start.equals(end)) {
            daysAddedToEnd = 0;
        }
        final int dayOfYear = 10;
        start = LocalDateTime.of(LocalDate.ofYearDay(2010, dayOfYear), (LocalTime) start);
        end = LocalDateTime.of(LocalDate.ofYearDay(2010, dayOfYear + daysAddedToEnd), (LocalTime) end);
        isStartAfterEnd = ((LocalDateTime) start).isAfter((LocalDateTime) end);
    } else if (start instanceof LocalDateTime) {
        isStartAfterEnd = ((LocalDateTime) start).isAfter((LocalDateTime) end);
    } else {
        isStartAfterEnd = ((ZonedDateTime) start).isAfter((ZonedDateTime) end);
    }
    // === check if input is legal: duration/period needs to be positive if end is after start and vice versa ===
    final String warningMsgPos = "Interval must be positive, if end is after start date! Node created an empty table.";
    final String warningMsgNeg = "Interval must be negative, if end is before start date! Node created an empty table.";
    if (start instanceof LocalDate) {
        if ((((LocalDate) end).isAfter((LocalDate) start)) && (((LocalDate) start.plus(durationOrPeriod)).isBefore((LocalDate) start))) {
            setWarningMessage(warningMsgPos);
            return;
        }
        if ((((LocalDate) end).isBefore((LocalDate) start)) && (((LocalDate) start.plus(durationOrPeriod)).isAfter((LocalDate) start))) {
            setWarningMessage(warningMsgNeg);
            return;
        }
    } else if (start instanceof LocalDateTime) {
        if ((((LocalDateTime) end).isAfter((LocalDateTime) start)) && (((LocalDateTime) start.plus(durationOrPeriod)).isBefore((LocalDateTime) start))) {
            setWarningMessage(warningMsgPos);
            return;
        }
        if ((((LocalDateTime) end).isBefore((LocalDateTime) start)) && (((LocalDateTime) start.plus(durationOrPeriod)).isAfter((LocalDateTime) start))) {
            setWarningMessage(warningMsgNeg);
            return;
        }
    } else if (start instanceof ZonedDateTime) {
        if ((((ZonedDateTime) end).isAfter((ZonedDateTime) start)) && (((ZonedDateTime) start.plus(durationOrPeriod)).isBefore((ZonedDateTime) start))) {
            setWarningMessage(warningMsgPos);
            return;
        }
        if ((((ZonedDateTime) end).isBefore((ZonedDateTime) start)) && (((ZonedDateTime) start.plus(durationOrPeriod)).isAfter((ZonedDateTime) start))) {
            setWarningMessage(warningMsgNeg);
            return;
        }
    }
    // === create rows ===
    Temporal currentDateTime = start;
    long row_idx = 0;
    while (true) {
        final DataCell dataCell;
        final boolean isEqual = currentDateTime.equals(end);
        final boolean isCurrentAfterEnd;
        if (currentDateTime instanceof LocalDate) {
            isCurrentAfterEnd = ((LocalDate) currentDateTime).isAfter((LocalDate) end);
            dataCell = LocalDateCellFactory.create((LocalDate) currentDateTime);
        } else if (currentDateTime instanceof LocalDateTime) {
            isCurrentAfterEnd = ((LocalDateTime) currentDateTime).isAfter((LocalDateTime) end);
            if (wasLocalTime) {
                dataCell = LocalTimeCellFactory.create((((LocalDateTime) currentDateTime).truncatedTo(ChronoUnit.MILLIS)).toLocalTime());
            } else {
                dataCell = LocalDateTimeCellFactory.create(((LocalDateTime) currentDateTime).truncatedTo(ChronoUnit.MILLIS));
            }
        } else {
            isCurrentAfterEnd = ((ZonedDateTime) currentDateTime).isAfter((ZonedDateTime) end);
            dataCell = ZonedDateTimeCellFactory.create(((ZonedDateTime) currentDateTime).truncatedTo(ChronoUnit.MILLIS));
        }
        if ((isCurrentAfterEnd && !isStartAfterEnd) || (!isCurrentAfterEnd && !isEqual && isStartAfterEnd)) {
            break;
        }
        container.addRowToTable(new DefaultRow(new RowKey("Row" + row_idx++), dataCell));
        if (isEqual) {
            break;
        }
        currentDateTime = currentDateTime.plus(durationOrPeriod);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) LocalTime(java.time.LocalTime) RowKey(org.knime.core.data.RowKey) Period(java.time.Period) Duration(java.time.Duration) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) LocalDate(java.time.LocalDate) Temporal(java.time.temporal.Temporal) ZonedDateTime(java.time.ZonedDateTime) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 18 with Period

use of java.time.Period in project knime-core by knime.

the class PeriodCellSerializer method serialize.

@Override
public void serialize(final PeriodCell cell, final DataCellDataOutput output) throws IOException {
    final Period period = cell.getPeriod();
    final int years = period.getYears();
    final int months = period.getMonths();
    final int days = period.getDays();
    output.writeInt(years);
    output.writeInt(months);
    output.writeInt(days);
}
Also used : Period(java.time.Period)

Example 19 with Period

use of java.time.Period in project knime-core by knime.

the class PeriodISOValueRenderer method setValue.

@Override
protected void setValue(final Object value) {
    if (value instanceof PeriodValue) {
        Period p = ((PeriodValue) value).getPeriod();
        super.setValue(p.toString());
    } else {
        super.setValue(value);
    }
}
Also used : Period(java.time.Period)

Example 20 with Period

use of java.time.Period in project knime-core by knime.

the class PeriodShortValueRenderer method setValue.

@Override
protected void setValue(final Object value) {
    if (value instanceof PeriodValue) {
        Period period = ((PeriodValue) value).getPeriod();
        super.setValue(DurationPeriodFormatUtils.formatPeriodShort(period));
    } else {
        super.setValue(value);
    }
}
Also used : Period(java.time.Period)

Aggregations

Period (java.time.Period)108 Test (org.junit.Test)38 Test (org.testng.annotations.Test)27 LocalDate (java.time.LocalDate)20 Duration (java.time.Duration)9 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)8 LocalDateTime (java.time.LocalDateTime)7 ZonedDateTime (java.time.ZonedDateTime)7 Date (java.util.Date)6 LocalTime (java.time.LocalTime)5 ZoneId (java.time.ZoneId)5 DateTimeFormatter (java.time.format.DateTimeFormatter)5 List (java.util.List)5 Test (org.junit.jupiter.api.Test)5 Instant (java.time.Instant)4 DateTimeParseException (java.time.format.DateTimeParseException)4 Temporal (java.time.temporal.Temporal)4 TemporalAmount (java.time.temporal.TemporalAmount)4 Arrays (java.util.Arrays)4 Collectors (java.util.stream.Collectors)4