Search in sources :

Example 26 with Temporal

use of java.time.temporal.Temporal in project jdk8u_jdk by JetBrains.

the class TCKPeriod method factory_from_TemporalAmount_Years_tooBig.

@Test(expectedExceptions = ArithmeticException.class)
public void factory_from_TemporalAmount_Years_tooBig() {
    TemporalAmount amount = new TemporalAmount() {

        @Override
        public long get(TemporalUnit unit) {
            return ((long) (Integer.MAX_VALUE)) + 1;
        }

        @Override
        public List<TemporalUnit> getUnits() {
            return Collections.<TemporalUnit>singletonList(YEARS);
        }

        @Override
        public Temporal addTo(Temporal temporal) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Temporal subtractFrom(Temporal temporal) {
            throw new UnsupportedOperationException();
        }
    };
    Period.from(amount);
}
Also used : Temporal(java.time.temporal.Temporal) TemporalUnit(java.time.temporal.TemporalUnit) TemporalAmount(java.time.temporal.TemporalAmount) Test(org.testng.annotations.Test)

Example 27 with Temporal

use of java.time.temporal.Temporal in project jdk8u_jdk by JetBrains.

the class TCKPeriod method factory_from_TemporalAmount_YearsDays.

@Test
public void factory_from_TemporalAmount_YearsDays() {
    TemporalAmount amount = new TemporalAmount() {

        @Override
        public long get(TemporalUnit unit) {
            if (unit == YEARS) {
                return 23;
            } else {
                return 45;
            }
        }

        @Override
        public List<TemporalUnit> getUnits() {
            List<TemporalUnit> list = new ArrayList<>();
            list.add(YEARS);
            list.add(DAYS);
            return list;
        }

        @Override
        public Temporal addTo(Temporal temporal) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Temporal subtractFrom(Temporal temporal) {
            throw new UnsupportedOperationException();
        }
    };
    assertPeriod(Period.from(amount), 23, 0, 45);
}
Also used : Temporal(java.time.temporal.Temporal) TemporalUnit(java.time.temporal.TemporalUnit) TemporalAmount(java.time.temporal.TemporalAmount) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 28 with Temporal

use of java.time.temporal.Temporal in project jdk8u_jdk by JetBrains.

the class TCKPeriod method test_plus_TemporalAmount_DaysHours.

@Test(expectedExceptions = DateTimeException.class)
public void test_plus_TemporalAmount_DaysHours() {
    TemporalAmount amount = new TemporalAmount() {

        @Override
        public long get(TemporalUnit unit) {
            if (unit == DAYS) {
                return 1;
            } else {
                return 2;
            }
        }

        @Override
        public List<TemporalUnit> getUnits() {
            List<TemporalUnit> list = new ArrayList<>();
            list.add(DAYS);
            list.add(HOURS);
            return list;
        }

        @Override
        public Temporal addTo(Temporal temporal) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Temporal subtractFrom(Temporal temporal) {
            throw new UnsupportedOperationException();
        }
    };
    pymd(4, 5, 6).plus(amount);
}
Also used : Temporal(java.time.temporal.Temporal) TemporalUnit(java.time.temporal.TemporalUnit) TemporalAmount(java.time.temporal.TemporalAmount) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 29 with Temporal

use of java.time.temporal.Temporal in project knime-core by knime.

the class CreateDateTimeNodeModel method createByFixedRowNrAndDuration.

/**
 * Create date&time row with a fixed number of rows and a given starting point and duration/period.
 */
private void createByFixedRowNrAndDuration(final BufferedDataContainer container, final long nrRows, final Temporal startDateTime, final TemporalAmount durationOrPeriod, final boolean wasLocalDate, final boolean hasMillis) throws DateTimeException, ArithmeticException {
    Temporal intervalDateTime = startDateTime.minus(durationOrPeriod);
    for (long rowIdx = 0; rowIdx < nrRows; rowIdx++) {
        intervalDateTime = intervalDateTime.plus(durationOrPeriod);
        final DataCell dataCell;
        // local date
        if (intervalDateTime instanceof LocalDate) {
            dataCell = LocalDateCellFactory.create((LocalDate) intervalDateTime);
        } else // local time
        if (intervalDateTime instanceof LocalTime) {
            if (hasMillis) {
                dataCell = LocalTimeCellFactory.create(((LocalTime) intervalDateTime).truncatedTo(ChronoUnit.MILLIS));
            } else if (((LocalTime) intervalDateTime).getNano() >= 500_000_000) {
                // rounding
                dataCell = LocalTimeCellFactory.create(((LocalTime) intervalDateTime).plusSeconds(1).withNano(0));
            } else {
                dataCell = LocalTimeCellFactory.create(((LocalTime) intervalDateTime).withNano(0));
            }
        } else // local date time
        if (intervalDateTime instanceof LocalDateTime) {
            if (wasLocalDate) {
                LocalDate localDate = ((LocalDateTime) intervalDateTime).toLocalDate();
                // rounding
                if (((LocalDateTime) intervalDateTime).toLocalTime().isAfter(LocalTime.NOON)) {
                    dataCell = LocalDateCellFactory.create(localDate.plusDays(1));
                } else {
                    dataCell = LocalDateCellFactory.create(localDate);
                }
            } else if (hasMillis) {
                dataCell = LocalDateTimeCellFactory.create(((LocalDateTime) intervalDateTime).truncatedTo(ChronoUnit.MILLIS));
            } else if (((LocalDateTime) intervalDateTime).getNano() >= 500_000_000) {
                // rounding
                dataCell = LocalDateTimeCellFactory.create(((LocalDateTime) intervalDateTime).plusSeconds(1).withNano(0));
            } else {
                dataCell = LocalDateTimeCellFactory.create(((LocalDateTime) intervalDateTime).withNano(0));
            }
        } else // zoned date time
        {
            if (hasMillis) {
                dataCell = ZonedDateTimeCellFactory.create(((ZonedDateTime) intervalDateTime).truncatedTo(ChronoUnit.MILLIS));
            } else if (((ZonedDateTime) intervalDateTime).getNano() >= 500_000_000) {
                // rounding
                dataCell = ZonedDateTimeCellFactory.create(((ZonedDateTime) intervalDateTime).plusSeconds(1).withNano(0));
            } else {
                dataCell = ZonedDateTimeCellFactory.create(((ZonedDateTime) intervalDateTime).withNano(0));
            }
        }
        container.addRowToTable(new DefaultRow(new RowKey("Row" + rowIdx), dataCell));
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) Temporal(java.time.temporal.Temporal) LocalTime(java.time.LocalTime) ZonedDateTime(java.time.ZonedDateTime) RowKey(org.knime.core.data.RowKey) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) LocalDate(java.time.LocalDate)

Example 30 with Temporal

use of java.time.temporal.Temporal in project knime-core by knime.

the class CreateDateTimeNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    BufferedDataContainer container = exec.createDataContainer(createOutSpec());
    // check and parse duration/period (may be wrong, if controlled by flow variables)
    TemporalAmount durationOrPeriod = null;
    if (m_durationOrEnd.getStringValue().equals(EndMode.Duration.name()) || m_rowNrOptionSelection.getStringValue().equals(RowNrMode.Variable.name())) {
        if (m_duration.getStringValue() != null) {
            try {
                durationOrPeriod = DurationPeriodFormatUtils.parseDuration(m_duration.getStringValue());
            } catch (DateTimeParseException ex1) {
                try {
                    durationOrPeriod = DurationPeriodFormatUtils.parsePeriod(m_duration.getStringValue());
                } catch (DateTimeParseException ex2) {
                    throw new InvalidSettingsException("'" + m_duration.getStringValue() + "' could not be parsed as duration!");
                }
            }
        }
    }
    // check start and end input (may be wrong, if controlled by flow variables)
    final Class<? extends Temporal> classStart = m_start.getSelectedDateTime().getClass();
    final Class<? extends Temporal> classEnd = m_end.getSelectedDateTime().getClass();
    if (!classStart.equals(classEnd) && !(classStart.equals(ZonedDateTime.class) && classEnd.equals(LocalDateTime.class) && m_selectedNewType.getDataType().equals(ZonedDateTimeCellFactory.TYPE))) {
        throw new InvalidSettingsException("The type of start and end time are not compatible: start is " + classStart.getSimpleName() + " but end is " + classEnd.getSimpleName());
    }
    // in case the end time is controlled by a flow variable holding a zoned date time, remove the zone
    m_end.setUseZone(false);
    // create date&time rows depending on settings
    final Temporal start;
    final Temporal end;
    if (m_start.getSelectedDateTime() instanceof ZonedDateTime) {
        start = m_startUseExecTime.getBooleanValue() ? getTemporalExecTimeWithFormat(((LocalDateTime) m_end.getSelectedDateTime()).atZone(m_start.getZone())) : m_start.getSelectedDateTime();
        end = m_endUseExecTime.getBooleanValue() ? getTemporalExecTimeWithFormat(m_start.getSelectedDateTime()) : ZonedDateTime.of((LocalDateTime) m_end.getSelectedDateTime(), m_start.getZone());
    } else {
        start = m_startUseExecTime.getBooleanValue() ? getTemporalExecTimeWithFormat(m_end.getSelectedDateTime()) : m_start.getSelectedDateTime();
        end = m_endUseExecTime.getBooleanValue() ? getTemporalExecTimeWithFormat(m_start.getSelectedDateTime()) : m_end.getSelectedDateTime();
    }
    if (m_rowNrOptionSelection.getStringValue().equals(RowNrMode.Fixed.name())) {
        if (m_durationOrEnd.getStringValue().equals(EndMode.Duration.name())) {
            createByFixedRowNrAndDuration(container, m_rowNrFixed.getLongValue(), start, durationOrPeriod, false, hasMillis(start, start) || hasDurationMillis());
        } else {
            createByFixedRowNrAndEnd(container, m_rowNrFixed.getLongValue(), start, end);
        }
    } else {
        createByVariableRowNr(container, start, end, durationOrPeriod);
    }
    container.close();
    return new BufferedDataTable[] { exec.createBufferedDataTable(container.getTable(), exec) };
}
Also used : LocalDateTime(java.time.LocalDateTime) DateTimeParseException(java.time.format.DateTimeParseException) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) Temporal(java.time.temporal.Temporal) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ZonedDateTime(java.time.ZonedDateTime) TemporalAmount(java.time.temporal.TemporalAmount) BufferedDataTable(org.knime.core.node.BufferedDataTable)

Aggregations

Temporal (java.time.temporal.Temporal)37 Test (org.testng.annotations.Test)24 TemporalAmount (java.time.temporal.TemporalAmount)12 Duration (java.time.Duration)7 DateTimeParseException (java.time.format.DateTimeParseException)7 TemporalUnit (java.time.temporal.TemporalUnit)7 DateTimeException (java.time.DateTimeException)6 LocalDate (java.time.LocalDate)6 LocalDateTime (java.time.LocalDateTime)6 TemporalAdjuster (java.time.temporal.TemporalAdjuster)6 LocalTime (java.time.LocalTime)5 ZonedDateTime (java.time.ZonedDateTime)5 ArrayList (java.util.ArrayList)5 Period (java.time.Period)4 DataRow (org.knime.core.data.DataRow)4 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)4 BufferedDataTable (org.knime.core.node.BufferedDataTable)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 DataCell (org.knime.core.data.DataCell)3 RowKey (org.knime.core.data.RowKey)3