Skip to content

Commit 7dea80b

Browse files
committed
sat: parse epoch from CSV format manually
Otherwise, if we use QDateTime::fromString() the code is awfully slow (more than 70× slower than the new manual code from this patch).
1 parent 40b056f commit 7dea80b

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

plugins/Satellites/src/Satellites.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,33 @@ void addTleChecksum(QString& line)
139139

140140
QString formatTleEpoch(const QString& epochFromCSV)
141141
{
142-
const auto epoch = QDateTime::fromString(epochFromCSV.left(23) + " +0000", "yyyy-MM-ddTHH:mm:ss.z tt");
142+
// The expected format is QDateTime's "yyyy-MM-ddTHH:mm:ss.z", followed by 3 digits of microseconds.
143+
// But parsing this using QDateTime::fromString() is awfully slow (more than 70× slower than this code).
144+
if (epochFromCSV.size() != 26 ||
145+
epochFromCSV[4] != '-' ||
146+
epochFromCSV[7] != '-' ||
147+
epochFromCSV[10] != 'T' ||
148+
epochFromCSV[13] != ':' ||
149+
epochFromCSV[16] != ':' ||
150+
epochFromCSV[19] != '.')
151+
{
152+
return {};
153+
}
154+
bool yOK = false, mthOK = false, dOK = false, hOK = false, minOK = false, sOK = false, usOK = false;
155+
const auto year = epochFromCSV.mid(0, 4).toUInt(&yOK);
156+
const auto month = epochFromCSV.mid(5, 2).toUInt(&mthOK);
157+
const auto day = epochFromCSV.mid(8, 2).toUInt(&dOK);
158+
const auto hour = epochFromCSV.mid(11, 2).toUInt(&hOK);
159+
const auto minute = epochFromCSV.mid(14, 2).toUInt(&minOK);
160+
const auto second = epochFromCSV.mid(17, 2).toUInt(&sOK);
161+
const auto microsecond = epochFromCSV.mid(20, 6).toUInt(&usOK);
162+
if (!yOK|| !mthOK|| !dOK|| !hOK|| !minOK|| !sOK|| !usOK) return {};
163+
164+
const auto ms = microsecond / 1000;
165+
const auto microsecTail = microsecond - ms * 1000;
166+
const auto epoch = QDateTime(QDate(year, month, day), QTime(hour, minute, second, ms), QTimeZone(0));
143167
if (!epoch.isValid()) return {};
144-
static const auto epoch0 = QDateTime(QDate(epoch.date().year(), 1, 1), QTime(0,0,0), QTimeZone(0));
145-
const auto microsecTail = epochFromCSV.mid(23).toUInt();
168+
static const auto epoch0 = QDateTime(QDate(year, 1, 1), QTime(0,0,0), QTimeZone(0));
146169
// The +1 shifts refence to "Jan 0" instead of Jan 1
147170
const auto diffInDays = 1 + (epoch0.msecsTo(epoch) + microsecTail * 1e-3) / 86400e3;
148171
const auto epochYear = epoch.date().year();

0 commit comments

Comments
 (0)