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());
}
};
}
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) };
}
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();
}
}
};
}
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);
}
}
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);
}
}
Aggregations