use of org.knime.core.data.date.DateAndTimeValue in project knime-core by knime.
the class DateShiftConfigure method getColumnValuebasedCellFactory.
/**
* @param spec the previous data table 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 getColumnValuebasedCellFactory(final DataTableSpec spec, final int col1Idx, final int col2Idx, final int g, final DateShiftConfigure conf) {
return new SingleCellFactory(createOutputColumnSpec(spec, conf.getNewColumnName().getStringValue())) {
/**
* 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) {
DataCell cell1 = row.getCell(col1Idx);
DataCell cell2 = row.getCell(col2Idx);
if ((cell1.isMissing()) || (cell2.isMissing())) {
return DataType.getMissingCell();
}
Calendar c = ((DateAndTimeValue) cell2).getUTCCalendarClone();
c.add(g, ((IntValue) cell1).getIntValue());
return new DateAndTimeCell(c.getTimeInMillis(), conf.getHasDate().getBooleanValue(), conf.getHasTime().getBooleanValue(), conf.getHasMiliSeconds().getBooleanValue());
}
};
}
use of org.knime.core.data.date.DateAndTimeValue in project knime-core by knime.
the class AbstractTimeExtractorIntCellFactory method getCell.
/**
* {@inheritDoc}
*/
@Override
public DataCell getCell(final DataRow row) {
DataCell cell = row.getCell(getColumnIndex());
if (cell.isMissing()) {
return DataType.getMissingCell();
}
DateAndTimeValue value = (DateAndTimeValue) cell;
if (checkTime() && value.hasTime()) {
producedValidValue();
return new IntCell(extractTimeField(value));
}
if (checkDate() && value.hasDate()) {
producedValidValue();
return new IntCell(extractTimeField(value));
}
// no date set
increaseMissingValueCount();
return DataType.getMissingCell();
}
use of org.knime.core.data.date.DateAndTimeValue in project knime-core by knime.
the class TimeFieldExtractorNodeModel method createColumnRearranger.
private SingleCellFactoryCompound createColumnRearranger(final DataTableSpec inSpec) {
final int colIdx = inSpec.findColumnIndex(m_selectedColumn.getStringValue());
ColumnRearranger rearranger = new ColumnRearranger(inSpec);
List<AbstractTimeExtractorCellFactory> cellFactories = new ArrayList<AbstractTimeExtractorCellFactory>();
// ************************* TIME fields factories *******************/
// hour
AbstractTimeExtractorCellFactory hourFactory = null;
if (m_useHour.getBooleanValue()) {
String colName = DataTableSpec.getUniqueColumnName(inSpec, m_hourColName.getStringValue());
hourFactory = new AbstractTimeExtractorIntCellFactory(colName, colIdx, true) {
@Override
protected int extractTimeField(final DateAndTimeValue value) {
return value.getHourOfDay();
}
};
rearranger.append(hourFactory);
cellFactories.add(hourFactory);
}
// minute
AbstractTimeExtractorCellFactory minuteFactory = null;
if (m_useMinute.getBooleanValue()) {
String colName = DataTableSpec.getUniqueColumnName(inSpec, m_minuteColName.getStringValue());
minuteFactory = new AbstractTimeExtractorIntCellFactory(colName, colIdx, true) {
@Override
protected int extractTimeField(final DateAndTimeValue value) {
return value.getMinute();
}
};
rearranger.append(minuteFactory);
cellFactories.add(minuteFactory);
}
// second
AbstractTimeExtractorCellFactory secondFactory = null;
if (m_useSecond.getBooleanValue()) {
String colName = DataTableSpec.getUniqueColumnName(inSpec, m_secondColName.getStringValue());
secondFactory = new AbstractTimeExtractorIntCellFactory(colName, colIdx, true) {
@Override
protected int extractTimeField(final DateAndTimeValue value) {
return value.getSecond();
}
};
rearranger.append(secondFactory);
cellFactories.add(secondFactory);
}
// millisecond
AbstractTimeExtractorCellFactory milliFactory = null;
if (m_useMillis.getBooleanValue()) {
String colName = DataTableSpec.getUniqueColumnName(inSpec, m_milliColName.getStringValue());
milliFactory = new AbstractTimeExtractorIntCellFactory(colName, colIdx, true) {
// here we also have to check if the value has millis
@Override
protected int extractTimeField(final DateAndTimeValue value) {
return value.getMillis();
}
@Override
public DataCell getCell(final DataRow row) {
DataCell cell = row.getCell(colIdx);
if (cell.isMissing()) {
return DataType.getMissingCell();
}
DateAndTimeValue value = (DateAndTimeValue) cell;
if (value.hasMillis()) {
producedValidValue();
return new IntCell(extractTimeField(value));
}
// no date set
increaseMissingValueCount();
return DataType.getMissingCell();
}
};
rearranger.append(milliFactory);
cellFactories.add(milliFactory);
}
return new SingleCellFactoryCompound(rearranger, cellFactories);
}
use of org.knime.core.data.date.DateAndTimeValue in project knime-core by knime.
the class TimePresetNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
BufferedDataTable input = inData[0];
DataTableSpec spec = input.getDataTableSpec();
String selectedColName = m_selectedCol.getStringValue();
final int colIdx = spec.findColumnIndex(selectedColName);
if (colIdx < 0) {
throw new IllegalArgumentException("Column " + selectedColName + " not found in input table!");
}
DataColumnSpecCreator colSpecCreator = new DataColumnSpecCreator(spec.getColumnSpec(selectedColName));
/*
* Reset the domain here. Had problems when time was there and a date is
* added, then the time without the date will stay the lower bound of
* the domain.
*/
colSpecCreator.setDomain(null);
ColumnRearranger rearranger = new ColumnRearranger(spec);
final Calendar preset = m_presetCalendar.getCalendar();
rearranger.replace(new SingleCellFactory(colSpecCreator.createSpec()) {
@Override
public DataCell getCell(final DataRow row) {
DataCell dc = row.getCell(colIdx);
if (dc.isMissing()) {
if (m_replaceMissingValues.getBooleanValue()) {
return new DateAndTimeCell(preset.getTimeInMillis(), m_presetCalendar.useDate(), m_presetCalendar.useTime(), m_presetCalendar.useMilliseconds());
}
return DataType.getMissingCell();
}
if (dc.getType().isCompatible(DateAndTimeValue.class)) {
DateAndTimeValue v = (DateAndTimeValue) dc;
Calendar existing = v.getUTCCalendarClone();
// look at the date
if (m_presetCalendar.useDate() && !v.hasDate()) {
// set it
existing.set(Calendar.YEAR, preset.get(Calendar.YEAR));
existing.set(Calendar.MONTH, preset.get(Calendar.MONTH));
existing.set(Calendar.DAY_OF_MONTH, preset.get(Calendar.DAY_OF_MONTH));
}
if (m_presetCalendar.useTime() && !v.hasTime()) {
// set it
existing.set(Calendar.HOUR_OF_DAY, preset.get(Calendar.HOUR_OF_DAY));
existing.set(Calendar.MINUTE, preset.get(Calendar.MINUTE));
existing.set(Calendar.SECOND, preset.get(Calendar.SECOND));
if (m_presetCalendar.useMilliseconds()) {
existing.set(Calendar.MILLISECOND, preset.get(Calendar.MILLISECOND));
}
}
return new DateAndTimeCell(existing.getTimeInMillis(), m_presetCalendar.useDate() || v.hasDate(), m_presetCalendar.useTime() || v.hasTime(), m_presetCalendar.useMilliseconds() || v.hasMillis());
}
LOGGER.error("Unsupported type " + dc.getType() + " found in row " + row.getKey() + "!");
return DataType.getMissingCell();
}
}, colIdx);
BufferedDataTable out = exec.createColumnRearrangeTable(input, rearranger, exec);
return new BufferedDataTable[] { out };
}
use of org.knime.core.data.date.DateAndTimeValue in project knime-core by knime.
the class AbstractTimeExtractorStringCellFactory method getCell.
/**
* {@inheritDoc}
*/
@Override
public DataCell getCell(final DataRow row) {
DataCell cell = row.getCell(getColumnIndex());
if (cell.isMissing()) {
return DataType.getMissingCell();
}
DateAndTimeValue value = (DateAndTimeValue) cell;
if (checkTime() && value.hasTime()) {
producedValidValue();
return new StringCell(extractTimeField(value));
}
if (checkDate() && value.hasDate()) {
producedValidValue();
return new StringCell(extractTimeField(value));
}
// no date set
increaseMissingValueCount();
return DataType.getMissingCell();
}
Aggregations