Search in sources :

Example 6 with DateAndTimeCell

use of org.knime.core.data.date.DateAndTimeCell in project knime-core by knime.

the class DateShiftConfigure method getColumnbasedCellFactory.

/**
 * @param spec the  output column spec
 * @param col1Idx the column index of the numerical column to add
 * @param g the time field to modify (as defined by calendar constants)
 * @param conf the configuration object
 * @param col2Idx the time column
 * @return the cell factory
 */
public static SingleCellFactory getColumnbasedCellFactory(final DataColumnSpec spec, final int col1Idx, final int col2Idx, final int g, final DateShiftConfigure conf) {
    return new SingleCellFactory(spec) {

        /**
         * Value for the new column is based on the values of two column of the row (first and second date column),
         * the selected granularity, and the fraction digits for rounding.
         *
         * @param row the current row
         * @return the difference between the two date values with the given granularity and rounding
         */
        @Override
        public DataCell getCell(final DataRow row) {
            final int value;
            DataCell cell2 = row.getCell(col2Idx);
            if (cell2.isMissing()) {
                return DataType.getMissingCell();
            }
            String typeofshift = conf.gettypeofshift().getStringValue();
            if (typeofshift.equals(DateShiftNodeDialog.CFG_COLUMN_SHIFT)) {
                DataCell cell1 = row.getCell(col1Idx);
                if ((cell1.isMissing())) {
                    return DataType.getMissingCell();
                }
                value = ((IntValue) cell1).getIntValue();
            } else {
                value = conf.getvalueofshift().getIntValue();
            }
            Calendar c = ((DateAndTimeValue) cell2).getUTCCalendarClone();
            c.add(g, value);
            return new DateAndTimeCell(c.getTimeInMillis(), conf.getHasDate().getBooleanValue(), conf.getHasTime().getBooleanValue(), conf.getHasMiliSeconds().getBooleanValue());
        }
    };
}
Also used : DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) Calendar(java.util.Calendar) SettingsModelCalendar(org.knime.timeseries.util.SettingsModelCalendar) DataCell(org.knime.core.data.DataCell) DateAndTimeCell(org.knime.core.data.date.DateAndTimeCell) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) DataRow(org.knime.core.data.DataRow)

Example 7 with DateAndTimeCell

use of org.knime.core.data.date.DateAndTimeCell in project knime-core by knime.

the class DateGeneratorNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    // prepare the calendars
    Calendar from = m_from.getCalendar();
    // new since 2.8. the start time is the current time.
    if (m_useExecution.getBooleanValue()) {
        from = DateAndTimeCell.getUTCCalendar();
        from.setTimeInMillis(System.currentTimeMillis() + TimeZone.getDefault().getOffset(System.currentTimeMillis()));
    }
    Calendar to = m_to.getCalendar();
    // if the use execution time is set, we ignore the settings for the from date
    boolean useDate = (m_from.useDate() && !m_useExecution.getBooleanValue()) || m_to.useDate();
    boolean useTime = (m_from.useTime() && !m_useExecution.getBooleanValue()) || m_to.useTime();
    boolean useMillis = (m_from.useMilliseconds() && !m_useExecution.getBooleanValue()) || m_to.useMilliseconds();
    if (useDate && !useTime) {
        DateAndTimeCell.resetTimeFields(from);
        DateAndTimeCell.resetTimeFields(to);
    } else if (useTime && !useDate) {
        DateAndTimeCell.resetDateFields(from);
        DateAndTimeCell.resetDateFields(to);
    }
    if (!useMillis) {
        from.clear(Calendar.MILLISECOND);
        to.clear(Calendar.MILLISECOND);
    }
    BufferedDataContainer container = exec.createDataContainer(createOutSpec());
    int nrRows = m_noOfRows.getIntValue();
    double offset = calculateOffset(from, to, nrRows);
    double currentTime = from.getTimeInMillis();
    for (int i = 0; i < nrRows; i++) {
        // zero based row key as FileReader
        RowKey key = new RowKey("Row" + i);
        DateAndTimeCell cell = new DateAndTimeCell((long) Math.ceil(currentTime), useDate, useTime, useMillis);
        container.addRowToTable(new DefaultRow(key, cell));
        currentTime += offset;
        exec.setProgress((i + 1) / (double) nrRows, "Generating row #" + (i + 1));
        exec.checkCanceled();
    }
    container.close();
    return new BufferedDataTable[] { exec.createBufferedDataTable(container.getTable(), exec) };
}
Also used : BufferedDataContainer(org.knime.core.node.BufferedDataContainer) RowKey(org.knime.core.data.RowKey) Calendar(java.util.Calendar) SettingsModelCalendar(org.knime.timeseries.util.SettingsModelCalendar) BufferedDataTable(org.knime.core.node.BufferedDataTable) DateAndTimeCell(org.knime.core.data.date.DateAndTimeCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 8 with DateAndTimeCell

use of org.knime.core.data.date.DateAndTimeCell in project knime-core by knime.

the class ColumnAutoTypeCasterNodeModel method createDateAndTimeConverter.

private SingleCellFactory createDateAndTimeConverter(final int colIdx, final DataColumnSpec colSpec) {
    return new SingleCellFactory(colSpec) {

        private final Calendar m_cal = Calendar.getInstance(TimeZone.getDefault());

        private final SimpleDateFormat m_format = new SimpleDateFormat(m_dateFormat);

        private final boolean m_hasDate;

        private final boolean m_hasTime;

        private final boolean m_hasMillis;

        {
            TimeZone timeZone = TimeZone.getTimeZone("UTC");
            m_format.setTimeZone(timeZone);
            m_cal.setTimeZone(timeZone);
            m_hasDate = m_dateFormat.contains("d");
            m_hasTime = m_dateFormat.contains("H");
            m_hasMillis = m_dateFormat.contains("S");
        }

        @Override
        public DataCell getCell(final DataRow row) {
            DataCell cell = row.getCell(colIdx);
            if (!cell.isMissing()) {
                String str = ((StringValue) cell).getStringValue();
                if (!str.equals(m_missValPat)) {
                    try {
                        m_cal.setTime(m_format.parse(str));
                        return new DateAndTimeCell(m_cal.getTimeInMillis(), m_hasDate, m_hasTime, m_hasMillis);
                    } catch (ParseException e) {
                        throw new IllegalArgumentException("Can't convert '" + str + "' to " + DateAndTimeCell.TYPE.toString() + ". In " + row.getKey() + " Column" + colIdx + ". Disable quickscan and try again.", e);
                    }
                } else {
                    return DataType.getMissingCell();
                }
            } else {
                // create MissingCell
                return DataType.getMissingCell();
            }
        }
    };
}
Also used : TimeZone(java.util.TimeZone) Calendar(java.util.Calendar) DataCell(org.knime.core.data.DataCell) DateAndTimeCell(org.knime.core.data.date.DateAndTimeCell) ParseException(java.text.ParseException) StringValue(org.knime.core.data.StringValue) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) SimpleDateFormat(java.text.SimpleDateFormat) DataRow(org.knime.core.data.DataRow)

Example 9 with DateAndTimeCell

use of org.knime.core.data.date.DateAndTimeCell in project knime-core by knime.

the class LinearInterpolationStatisticTB method consumeRow.

/**
 * {@inheritDoc}
 */
@Override
protected void consumeRow(final DataRow dataRow) {
    DataCell cell = dataRow.getCell(getColumnIndex());
    if (cell.isMissing()) {
        incMissing();
    } else {
        for (int i = 0; i < getNumMissing(); i++) {
            DataCell res;
            if (getPrevious().isMissing()) {
                res = cell;
            } else {
                if (m_isDateColumn) {
                    DateAndTimeValue val = (DateAndTimeValue) cell;
                    DateAndTimeValue prevVal = (DateAndTimeValue) getPrevious();
                    boolean hasDate = val.hasDate() | prevVal.hasDate();
                    boolean hasTime = val.hasTime() | prevVal.hasTime();
                    boolean hasMilis = val.hasMillis() | prevVal.hasMillis();
                    long prev = prevVal.getUTCTimeInMillis();
                    long next = val.getUTCTimeInMillis();
                    long lin = Math.round(prev + 1.0 * (i + 1) / (1.0 * (getNumMissing() + 1)) * (next - prev));
                    res = new DateAndTimeCell(lin, hasDate, hasTime, hasMilis);
                } else {
                    DoubleValue val = (DoubleValue) cell;
                    double prev = ((DoubleValue) getPrevious()).getDoubleValue();
                    double next = val.getDoubleValue();
                    double lin = prev + 1.0 * (i + 1) / (1.0 * (getNumMissing() + 1)) * (next - prev);
                    if (getPrevious() instanceof IntValue) {
                        // get an int, create an int
                        res = new IntCell((int) Math.round(lin));
                    } else if (getPrevious() instanceof LongValue) {
                        // get an long, create an long
                        res = new LongCell(Math.round(lin));
                    } else {
                        res = new DoubleCell(lin);
                    }
                }
            }
            addMapping(res);
        }
        resetMissing(cell);
    }
}
Also used : DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) DoubleCell(org.knime.core.data.def.DoubleCell) IntCell(org.knime.core.data.def.IntCell) LongCell(org.knime.core.data.def.LongCell) DoubleValue(org.knime.core.data.DoubleValue) LongValue(org.knime.core.data.LongValue) DataCell(org.knime.core.data.DataCell) DateAndTimeCell(org.knime.core.data.date.DateAndTimeCell) IntValue(org.knime.core.data.IntValue)

Example 10 with DateAndTimeCell

use of org.knime.core.data.date.DateAndTimeCell in project knime-core by knime.

the class AverageInterpolationStatisticTB method consumeRow.

/**
 * {@inheritDoc}
 */
@Override
protected void consumeRow(final DataRow dataRow) {
    DataCell cell = dataRow.getCell(getColumnIndex());
    if (cell.isMissing()) {
        incMissing();
    } else {
        for (int i = 0; i < getNumMissing(); i++) {
            DataCell res;
            if (getPrevious().isMissing()) {
                res = cell;
            } else {
                if (m_isDateColumn) {
                    DateAndTimeValue val = (DateAndTimeValue) cell;
                    DateAndTimeValue prevVal = (DateAndTimeValue) getPrevious();
                    boolean hasDate = val.hasDate() | prevVal.hasDate();
                    boolean hasTime = val.hasTime() | prevVal.hasTime();
                    boolean hasMilis = val.hasMillis() | prevVal.hasMillis();
                    long prev = prevVal.getUTCTimeInMillis();
                    long next = val.getUTCTimeInMillis();
                    long lin = Math.round((prev + next) / 2);
                    res = new DateAndTimeCell(lin, hasDate, hasTime, hasMilis);
                } else {
                    DoubleValue val = (DoubleValue) cell;
                    double prev = ((DoubleValue) getPrevious()).getDoubleValue();
                    double next = val.getDoubleValue();
                    double lin = (prev + next) / 2;
                    if (getPrevious() instanceof IntValue) {
                        // get an int, create an int
                        res = new IntCell((int) Math.round(lin));
                    } else if (getPrevious() instanceof LongValue) {
                        // get an long, create an long
                        res = new LongCell(Math.round(lin));
                    } else {
                        res = new DoubleCell(lin);
                    }
                }
            }
            addMapping(res);
        }
        resetMissing(cell);
    }
}
Also used : DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) DoubleCell(org.knime.core.data.def.DoubleCell) IntCell(org.knime.core.data.def.IntCell) LongCell(org.knime.core.data.def.LongCell) DoubleValue(org.knime.core.data.DoubleValue) LongValue(org.knime.core.data.LongValue) DataCell(org.knime.core.data.DataCell) DateAndTimeCell(org.knime.core.data.date.DateAndTimeCell) IntValue(org.knime.core.data.IntValue)

Aggregations

DateAndTimeCell (org.knime.core.data.date.DateAndTimeCell)16 DataCell (org.knime.core.data.DataCell)12 DateAndTimeValue (org.knime.core.data.date.DateAndTimeValue)11 Calendar (java.util.Calendar)9 DataRow (org.knime.core.data.DataRow)8 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)8 DoubleValue (org.knime.core.data.DoubleValue)6 IntValue (org.knime.core.data.IntValue)6 LongValue (org.knime.core.data.LongValue)6 DoubleCell (org.knime.core.data.def.DoubleCell)6 IntCell (org.knime.core.data.def.IntCell)6 LongCell (org.knime.core.data.def.LongCell)6 SettingsModelCalendar (org.knime.timeseries.util.SettingsModelCalendar)6 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)4 ParseException (java.text.ParseException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)2 StringValue (org.knime.core.data.StringValue)2 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)2 BufferedDataTable (org.knime.core.node.BufferedDataTable)2