Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSV: Added option to parse date/time as extended ISO 8601 #954

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions plotjuggler_plugins/DataLoadCSV/dataload_csv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,19 @@ DataLoadCSV::DataLoadCSV()
[this]() { emit _ui->buttonBox->accepted(); });

connect(_ui->checkBoxDateFormat, &QCheckBox::toggled, this,
[this](bool checked) { _ui->lineEditDateFormat->setEnabled(checked); });
[this](bool checked) {
_ui->radioCustomDate->setEnabled(checked);
_ui->radioIso8601Date->setEnabled(checked);
_ui->lineEditDateFormat->setEnabled(checked && _ui->radioCustomDate->isChecked());
});

connect(_ui->radioCustomDate, &QRadioButton::toggled, this, [this](bool checked){
_ui->lineEditDateFormat->setEnabled(checked);
});

connect(_ui->radioIso8601Date, &QRadioButton::toggled, this, [this](bool checked){
_ui->lineEditDateFormat->setEnabled(!checked);
});

connect(_ui->dateTimeHelpButton, &QPushButton::clicked, this,
[this]() { _dateTime_dialog->show(); });
Expand Down Expand Up @@ -299,6 +311,8 @@ int DataLoadCSV::launchDialog(QFile& file, std::vector<std::string>* column_name
settings.value("DataLoadCSV.useIndex", false).toBool());
_ui->checkBoxDateFormat->setChecked(
settings.value("DataLoadCSV.useDateFormat", false).toBool());
_ui->radioCustomDate->setChecked(
!settings.value("DataLoadCSV.useISO8601", true).toBool());
_ui->lineEditDateFormat->setText(
settings.value("DataLoadCSV.dateFormat", "yyyy-MM-dd hh:mm:ss").toString());

Expand Down Expand Up @@ -379,6 +393,7 @@ int DataLoadCSV::launchDialog(QFile& file, std::vector<std::string>* column_name
settings.setValue("DataLoadCSV.geometry", _dialog->saveGeometry());
settings.setValue("DataLoadCSV.useIndex", _ui->radioButtonIndex->isChecked());
settings.setValue("DataLoadCSV.useDateFormat", _ui->checkBoxDateFormat->isChecked());
settings.setValue("DataLoadCSV.useISO8601", _ui->radioIso8601Date->isChecked());
settings.setValue("DataLoadCSV.dateFormat", _ui->lineEditDateFormat->text());

if (res == QDialog::Rejected)
Expand Down Expand Up @@ -490,6 +505,7 @@ bool DataLoadCSV::readDataFromFile(FileLoadInfo* info, PlotDataMapRef& plot_data
double prev_time = std::numeric_limits<double>::lowest();
bool parse_date_format = _ui->checkBoxDateFormat->isChecked();
QString format_string = _ui->lineEditDateFormat->text();
bool parse_iso_8601 = _ui->radioIso8601Date->isChecked();

auto ParseTimestamp = [&](QString str, bool& is_number) {
QString str_trimmed = str.trimmed();
Expand Down Expand Up @@ -530,9 +546,9 @@ bool DataLoadCSV::readDataFromFile(FileLoadInfo* info, PlotDataMapRef& plot_data
static QLocale locale_with_comma(QLocale::German);
val = locale_with_comma.toDouble(str_trimmed, &is_number);
}
if (!is_number && parse_date_format && !format_string.isEmpty())
if (!is_number && parse_date_format && (!format_string.isEmpty() || parse_iso_8601))
{
QDateTime ts = QDateTime::fromString(str_trimmed, format_string);
QDateTime ts = parse_iso_8601 ? QDateTime::fromString(str_trimmed, Qt::ISODateWithMs) : QDateTime::fromString(str_trimmed, format_string);
is_number = ts.isValid();
if (is_number)
{
Expand All @@ -551,9 +567,9 @@ bool DataLoadCSV::readDataFromFile(FileLoadInfo* info, PlotDataMapRef& plot_data
static QLocale locale_with_comma(QLocale::German);
val = locale_with_comma.toDouble(str_trimmed, &is_number);
}
if (!is_number && parse_date_format && !format_string.isEmpty())
if (!is_number && parse_date_format && (!format_string.isEmpty() || parse_iso_8601))
{
QDateTime ts = QDateTime::fromString(str_trimmed, format_string);
QDateTime ts = parse_iso_8601 ? QDateTime::fromString(str_trimmed, Qt::ISODateWithMs) : QDateTime::fromString(str_trimmed, format_string);
is_number = ts.isValid();
if (is_number)
{
Expand Down Expand Up @@ -779,12 +795,16 @@ bool DataLoadCSV::xmlSaveState(QDomDocument& doc, QDomElement& parent_element) c
elem.setAttribute("time_axis", _default_time_axis.c_str());
elem.setAttribute("delimiter", _ui->comboBox->currentIndex());

QString date_format;
if (_ui->checkBoxDateFormat->isChecked())
{
elem.setAttribute("date_format", _ui->lineEditDateFormat->text());
}

if (_ui->radioIso8601Date->isChecked())
{
elem.setAttribute("date_format_iso", "ISO8601");
}

parent_element.appendChild(elem);
return true;
}
Expand Down Expand Up @@ -822,5 +842,11 @@ bool DataLoadCSV::xmlLoadState(const QDomElement& parent_element)
_ui->checkBoxDateFormat->setChecked(true);
_ui->lineEditDateFormat->setText(elem.attribute("date_format"));
}
if (elem.hasAttribute("date_format_iso"))
{
_ui->radioIso8601Date->setChecked(true);
} else {
_ui->radioCustomDate->setChecked(true);
}
return true;
}
23 changes: 23 additions & 0 deletions plotjuggler_plugins/DataLoadCSV/dataload_csv.ui
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,29 @@
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioIso8601Date">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string> ISO 8601</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioCustomDate">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Custom</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEditDateFormat">
<property name="enabled">
Expand Down