Search in sources :

Example 1 with DateTimeType

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

the class StringToDateTimeNodeDialog method formatListener.

/**
 * method for change/action listener of type and date combo boxes.
 */
private boolean formatListener(final String format) {
    // remove all expressions in unquoted square brackets, i.e. removes all optional fields of the format
    final StringBuilder buffer = new StringBuilder();
    int parenthesisCounter = 0;
    int apostropheCounter = 0;
    for (char c : format.toCharArray()) {
        if (c == '\'') {
            apostropheCounter++;
        } else if ((c == '[') && (apostropheCounter % 2 == 0)) {
            parenthesisCounter++;
        } else if ((c == ']') && (apostropheCounter % 2 == 0)) {
            parenthesisCounter--;
        }
        if ((!((c == '[') || (c == ']')) || (apostropheCounter % 2 == 1)) && (parenthesisCounter == 0)) {
            buffer.append(c);
        }
    }
    final String formatNoOptionalFields = buffer.toString();
    // check, if the pattern is valid
    switch((DateTimeType) m_typeCombobox.getSelectedItem()) {
        case LOCAL_DATE:
            {
                try {
                    final LocalDate now1 = LocalDate.now();
                    final DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern(format);
                    LocalDate.parse(now1.format(DateTimeFormatter.ofPattern(formatNoOptionalFields)), formatter1);
                    return setTypeFormatWarningNull();
                } catch (DateTimeException exception) {
                    return setTypeFormatWarningMessage(exception, DateTimeType.LOCAL_DATE.toString() + " needs a date, but does not support a time, time zone or offset!");
                } catch (IllegalArgumentException exception) {
                    return setTypeFormatWarningMessage(exception, exception.getMessage());
                }
            }
        case LOCAL_TIME:
            {
                try {
                    final LocalTime now2 = LocalTime.now();
                    final DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern(format);
                    LocalTime.parse(now2.format(DateTimeFormatter.ofPattern(formatNoOptionalFields)), formatter2);
                    return setTypeFormatWarningNull();
                } catch (DateTimeException exception) {
                    return setTypeFormatWarningMessage(exception, DateTimeType.LOCAL_TIME.toString() + " needs a time, but does not support a date, time zone or offset!");
                } catch (IllegalArgumentException exception) {
                    return setTypeFormatWarningMessage(exception, exception.getMessage());
                }
            }
        case LOCAL_DATE_TIME:
            {
                try {
                    final LocalDateTime now3 = LocalDateTime.now();
                    LocalDateTime.parse(now3.format(DateTimeFormatter.ofPattern(formatNoOptionalFields)), DateTimeFormatter.ofPattern(format));
                    return setTypeFormatWarningNull();
                } catch (DateTimeException exception) {
                    return setTypeFormatWarningMessage(exception, DateTimeType.LOCAL_DATE_TIME.toString() + " needs date and time, but does not support a time zone or offset!");
                } catch (IllegalArgumentException exception) {
                    return setTypeFormatWarningMessage(exception, exception.getMessage());
                }
            }
        case ZONED_DATE_TIME:
            {
                try {
                    final ZonedDateTime now4 = ZonedDateTime.now();
                    final DateTimeFormatter formatter4 = DateTimeFormatter.ofPattern(format);
                    ZonedDateTime.parse(now4.format(DateTimeFormatter.ofPattern(formatNoOptionalFields)), formatter4);
                    return setTypeFormatWarningNull();
                } catch (DateTimeException exception) {
                    return setTypeFormatWarningMessage(exception, DateTimeType.ZONED_DATE_TIME.toString() + " needs date, time and a time zone or offset!");
                } catch (IllegalArgumentException exception) {
                    return setTypeFormatWarningMessage(exception, exception.getMessage());
                }
            }
        default:
            throw new IllegalStateException("Unhandled date&time type: " + m_typeCombobox.getSelectedItem());
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) LocalTime(java.time.LocalTime) DialogComponentString(org.knime.core.node.defaultnodesettings.DialogComponentString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) LocalDate(java.time.LocalDate) DateTimeType(org.knime.time.util.DateTimeType) DateTimeException(java.time.DateTimeException) ZonedDateTime(java.time.ZonedDateTime) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 2 with DateTimeType

use of org.knime.time.util.DateTimeType 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 3 with DateTimeType

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

the class OldToNewTimeNodeModel method getNewIncludedColumnSpecs.

/**
 * @param inSpec Current input spec
 * @param row First row of the table, if called by execute, or null, if called by configure
 * @return Column specs of the output (only of the included columns)
 */
private DataColumnSpec[] getNewIncludedColumnSpecs(final DataTableSpec inSpec, final DataRow row) {
    final String[] includes = m_colSelect.applyTo(inSpec).getIncludes();
    m_newTypes = new DateTimeType[includes.length];
    final DataColumnSpec[] newSpec = new DataColumnSpec[includes.length];
    /*
         * if the types of the cells should determined automatically by the content of the first row
         */
    if (m_autoType.getBooleanValue()) {
        // row is null, if the method is called by the configure method
        if (row != null) {
            DataColumnSpecCreator dataColumnSpecCreator = null;
            for (int i = 0; i < includes.length; i++) {
                final DataCell cell = row.getCell(inSpec.findColumnIndex(includes[i]));
                if (cell.isMissing()) {
                    m_newTypes[i] = DateTimeType.LOCAL_DATE_TIME;
                    dataColumnSpecCreator = new DataColumnSpecCreator(includes[i], DataType.getType(LocalDateTimeCell.class));
                } else {
                    final DateAndTimeValue timeCell = (DateAndTimeValue) cell;
                    if (!timeCell.hasDate()) {
                        m_newTypes[i] = DateTimeType.LOCAL_TIME;
                        dataColumnSpecCreator = new DataColumnSpecCreator(includes[i], DataType.getType(LocalTimeCell.class));
                    } else {
                        if (!timeCell.hasTime()) {
                            m_newTypes[i] = DateTimeType.LOCAL_DATE;
                            dataColumnSpecCreator = new DataColumnSpecCreator(includes[i], DataType.getType(LocalDateCell.class));
                        } else {
                            if (m_addZone.getBooleanValue()) {
                                m_newTypes[i] = DateTimeType.ZONED_DATE_TIME;
                                dataColumnSpecCreator = new DataColumnSpecCreator(includes[i], DataType.getType(ZonedDateTimeCell.class));
                            } else {
                                m_newTypes[i] = DateTimeType.LOCAL_DATE_TIME;
                                dataColumnSpecCreator = new DataColumnSpecCreator(includes[i], DataType.getType(LocalDateTimeCell.class));
                            }
                        }
                    }
                }
                newSpec[i] = dataColumnSpecCreator.createSpec();
            }
            return newSpec;
        // row is not null, if the method is called by the execute method
        } else {
            return null;
        }
    /*
             * if the type of the new cells is determined by the user itself
             */
    } else {
        DateTimeType type = DateTimeType.valueOf(m_selectedNewType);
        DataType newDataType = type.getDataType();
        for (int i = 0; i < includes.length; i++) {
            final DataColumnSpecCreator dataColumnSpecCreator = new DataColumnSpecCreator(includes[i], newDataType);
            newSpec[i] = dataColumnSpecCreator.createSpec();
            m_newTypes[i] = type;
        }
        return newSpec;
    }
}
Also used : DateTimeType(org.knime.time.util.DateTimeType) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) DataCell(org.knime.core.data.DataCell) DataType(org.knime.core.data.DataType) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString)

Aggregations

SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)3 DateTimeType (org.knime.time.util.DateTimeType)3 LocalDate (java.time.LocalDate)2 LocalDateTime (java.time.LocalDateTime)2 ZonedDateTime (java.time.ZonedDateTime)2 DateTimeException (java.time.DateTimeException)1 Duration (java.time.Duration)1 LocalTime (java.time.LocalTime)1 Period (java.time.Period)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 DateTimeParseException (java.time.format.DateTimeParseException)1 Temporal (java.time.temporal.Temporal)1 DataCell (org.knime.core.data.DataCell)1 DataColumnSpec (org.knime.core.data.DataColumnSpec)1 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)1 DataType (org.knime.core.data.DataType)1 DateAndTimeValue (org.knime.core.data.date.DateAndTimeValue)1 DialogComponentString (org.knime.core.node.defaultnodesettings.DialogComponentString)1 SettingsModelDateTime (org.knime.time.util.SettingsModelDateTime)1