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

optimize [QDate|QTime|QDateTime]::fromString format literals. #1278

Merged
merged 4 commits into from
Sep 4, 2024
Merged
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
4 changes: 2 additions & 2 deletions exif.cc
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ ExifFormat::exif_read_timestamp(const ExifTag* tag)
QDate
ExifFormat::exif_read_datestamp(const ExifTag* tag)
{
return QDate::fromString(tag->data.at(0).toByteArray().constData(), "yyyy:MM:dd");
return QDate::fromString(tag->data.at(0).toByteArray().constData(), u"yyyy:MM:dd");
}

void
Expand Down Expand Up @@ -697,7 +697,7 @@ ExifFormat::exif_get_exif_time(ExifApp* app) const
// Note the assumption of local time can be problematic if the data
// is processed in a different time zone than was used in recording
// the time in the image.
res = QDateTime::fromString(str, "yyyy:MM:dd hh:mm:ss");
res = QDateTime::fromString(str, u"yyyy:MM:dd hh:mm:ss");

// Exif 2.31 added offset tags to record the offset to UTC.
// If these are present use them, otherwise assume local time.
Expand Down
2 changes: 1 addition & 1 deletion garmin_txt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ GarminTxtFormat::write()
};

QString grid_str = gt_get_mps_grid_longname(grid_index, MYNAME);
grid_str = grid_str.replace('*', "°");
grid_str = grid_str.replace('*', u'°');
*fout << "Grid\t" << grid_str << "\r\n";

datum_str = gt_get_mps_datum_name(datum_index);
Expand Down
4 changes: 2 additions & 2 deletions gdb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1133,8 +1133,8 @@ GdbFormat::write_header()
*/
static const QDateTime gdb_release_dt = QDateTime(QDate(2011, 4, 14), QTime(1, 30, 1), Qt::UTC);
gdb_write_cstr(QStringLiteral("GPSBabel-%1").arg(gpsbabel_version));
gdb_write_cstr(gdb_release_dt.toString("MMM dd yyyy"));
gdb_write_cstr(gdb_release_dt.toString("HH:mm:ss"));
gdb_write_cstr(gdb_release_dt.toString(u"MMM dd yyyy"));
gdb_write_cstr(gdb_release_dt.toString(u"HH:mm:ss"));

finalize_item(fsave, 'A');

Expand Down
4 changes: 2 additions & 2 deletions gui/gmapdlg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ void GMapDialog::appendTrackInfo(QStandardItem* it, const GpxTrack& trk)
}
if (startTime.isValid()) {
it->appendRow(new StandardItem(tr("Start: %1")
.arg(startTime.toString("yyyy-MMM-dd HH:mm:ss"))));
.arg(startTime.toString(u"yyyy-MMM-dd HH:mm:ss"))));
it->appendRow(new StandardItem(tr("Stop: %1")
.arg(stopTime.toString("yyyy-MMM-dd HH:mm:ss"))));
.arg(stopTime.toString(u"yyyy-MMM-dd HH:mm:ss"))));
}
it->appendRow(new StandardItem(tr("Points: %1").arg(count)));

Expand Down
2 changes: 1 addition & 1 deletion gui/gpx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

static QDateTime decodeDateTime(const QString& s)
{
QDateTime utc = QDateTime::fromString(s, "yyyy-MM-dd'T'HH:mm:ss'Z'");
QDateTime utc = QDateTime::fromString(s, u"yyyy-MM-dd'T'HH:mm:ss'Z'");
return utc;
}

Expand Down
4 changes: 2 additions & 2 deletions igc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ QByteArray IgcFormat::latlon2str(const Waypoint* wpt)

QByteArray IgcFormat::date2str(const gpsbabel::DateTime& dt)
{
QByteArray str = dt.toUTC().toString("ddMMyy").toUtf8();
QByteArray str = dt.toUTC().toString(u"ddMMyy").toUtf8();
if (str.size() != 6) {
fatal(MYNAME ": Bad date format '%s'\n", str.constData());
}
Expand All @@ -655,7 +655,7 @@ QByteArray IgcFormat::date2str(const gpsbabel::DateTime& dt)

QByteArray IgcFormat::tod2str(const gpsbabel::DateTime& tod)
{
QByteArray str = tod.toUTC().toString("hhmmss").toUtf8();
QByteArray str = tod.toUTC().toString(u"hhmmss").toUtf8();
if (str.size() != 6) {
fatal(MYNAME ": Bad time of day format '%s'\n", str.constData());
}
Expand Down
2 changes: 1 addition & 1 deletion mtk_logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ int MtkLoggerBase::csv_line(gbfile* csvFile, int idx, unsigned long bmask, data_
QDateTime dt = QDateTime::fromSecsSinceEpoch(itm->timestamp, Qt::UTC);
dt = dt.addMSecs(itm->timestamp_ms);

QString timestamp = dt.toUTC().toString("yyyy/MM/dd,hh:mm:ss.zzz");
QString timestamp = dt.toUTC().toString(u"yyyy/MM/dd,hh:mm:ss.zzz");
gbfputs(timestamp, csvFile);
gbfputc(',', csvFile);
}
Expand Down
8 changes: 4 additions & 4 deletions nmea.cc
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ QTime NmeaFormat::nmea_parse_hms(const QString& str)
QTime retval; /* invalid time */
const QStringList parts = str.trimmed().split('.');
if ((parts.size() == 1) || (parts.size() == 2)) {
retval = QTime::fromString(parts.at(0), "hhmmss");
retval = QTime::fromString(parts.at(0), u"hhmmss");
if (retval.isValid() && parts.size() == 2) {
bool ok;
// prepend "0.". prepending "." won't work if there are no trailing digits.
Expand Down Expand Up @@ -539,7 +539,7 @@ NmeaFormat::gprmc_parse(const QString& ibuf)
if (fields.size() > 9) {
QString datestr(fields[9]);
datestr.insert(4, "20");
dmy = QDate::fromString(datestr, "ddMMyyyy");
dmy = QDate::fromString(datestr, u"ddMMyyyy");
}
if (fix != 'A') {
/* ignore this fix - it is invalid */
Expand Down Expand Up @@ -635,7 +635,7 @@ NmeaFormat::gpzda_parse(const QString& ibuf)
if (fields.size() > 4) {
QTime time = nmea_parse_hms(fields[1]);
QString datestr = QStringLiteral("%1%2%3").arg(fields[2], fields[3], fields[4]);
QDate date = QDate::fromString(datestr, "ddMMyyyy");
QDate date = QDate::fromString(datestr, u"ddMMyyyy");

// The prev_datetime data member might be used by
// nmea_fix_timestamps and nmea_set_waypoint_time.
Expand Down Expand Up @@ -997,7 +997,7 @@ NmeaFormat::read()
}

if (optdate) {
opt_tm = QDate::fromString(optdate, "yyyyMMdd");
opt_tm = QDate::fromString(optdate, u"yyyyMMdd");
if (!opt_tm.isValid()) {
fatal(MYNAME ": Invalid date \"%s\"!\n", optdate);
}
Expand Down
10 changes: 5 additions & 5 deletions subrip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,13 @@ SubripFormat::wr_init(const QString& fname)
}
gps_datetime = QDateTime();
if ((opt_gpstime != nullptr) && (opt_gpsdate != nullptr)) {
QDate gps_date = QDate::fromString(opt_gpsdate, "yyyyMMdd");
QDate gps_date = QDate::fromString(opt_gpsdate, u"yyyyMMdd");
if (!gps_date.isValid()) {
fatal(FatalMsg().nospace() << MYNAME ": option gps_date value (" << opt_gpsdate << ") is invalid. Expected yyyymmdd.");
}
QTime gps_time = QTime::fromString(opt_gpstime, "HHmmss");
QTime gps_time = QTime::fromString(opt_gpstime, u"HHmmss");
if (!gps_time.isValid()) {
gps_time = QTime::fromString(opt_gpstime, "HHmmss.z");
gps_time = QTime::fromString(opt_gpstime, u"HHmmss.z");
if (!gps_time.isValid()) {
fatal(FatalMsg().nospace() << MYNAME ": option gps_time value (" << opt_gpstime << ") is invalid. Expected hhmmss[.sss]");
}
Expand All @@ -226,9 +226,9 @@ SubripFormat::wr_init(const QString& fname)

video_offset_ms = 0;
if (opt_videotime != nullptr) {
QTime video_time = QTime::fromString(opt_videotime, "HHmmss");
QTime video_time = QTime::fromString(opt_videotime, u"HHmmss");
if (!video_time.isValid()) {
video_time = QTime::fromString(opt_videotime, "HHmmss.z");
video_time = QTime::fromString(opt_videotime, u"HHmmss.z");
if (!video_time.isValid()) {
fatal(FatalMsg().nospace() << MYNAME ": option video_time value (" << opt_videotime << ") is invalid. Expected hhmmss[.sss].");
}
Expand Down
4 changes: 2 additions & 2 deletions trackfilter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ QDateTime TrackFilter::trackfilter_range_check(const char* timestr)
QRegularExpressionMatch match = re.match(fmtstart);
if (match.hasMatch()) {
// QTime::fromString zzz expects exactly 3 digits representing milliseconds.
result = QDateTime::fromString(match.captured(0), "yyyyMMddHHmmss.zzz");
result = QDateTime::fromString(match.captured(0), u"yyyyMMddHHmmss.zzz");
result.setTimeSpec(Qt::UTC);
if (!result.isValid()) {
fatal(MYNAME "-range-check: Invalid timestamp \"%s\"!\n", timestr);
Expand Down Expand Up @@ -833,7 +833,7 @@ TrackFilter::faketime_t TrackFilter::trackfilter_faketime_check(const char* time
QString start = match.captured(2);
QString fmtstart("00000101000000");
fmtstart.replace(0, start.size(), start);
result.start = QDateTime::fromString(fmtstart, "yyyyMMddHHmmss");
result.start = QDateTime::fromString(fmtstart, u"yyyyMMddHHmmss");
result.start.setTimeSpec(Qt::UTC);
if (!result.start.isValid()) {
fatal(MYNAME "-faketime-check: Invalid timestamp \"%s\"!\n", qPrintable(start));
Expand Down
6 changes: 3 additions & 3 deletions unicsv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ UnicsvFormat::rd_init(const QString& fname)
fin->open(fname, QIODevice::ReadOnly, MYNAME, opt_codec);
unicsv_lineno = 0;
if (opt_fields) {
QString fields = QString(opt_fields).replace("+", ",");
QString fields = QString(opt_fields).replace('+', ',');
unicsv_fondle_header(fields);
} else if (buff = fin->readLine(); !buff.isNull()) {
++unicsv_lineno;
Expand Down Expand Up @@ -1105,8 +1105,8 @@ UnicsvFormat::unicsv_print_str(const QString& s) const
// slavish re-implementation of (what I think) the original C code
// was doing.
t.replace("\r\n", ",");
t.replace("\r", ",");
t.replace("\n", ",");
t.replace('\r', ',');
t.replace('\n', ',');
}
*fout << t.trimmed();
}
Expand Down
7 changes: 3 additions & 4 deletions waypt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -519,12 +519,11 @@ Waypoint::CreationTimeXML() const

QDateTime dt = GetCreationTime().toUTC();

const char* format = "yyyy-MM-ddTHH:mm:ssZ";
if (dt.time().msec()) {
format = "yyyy-MM-ddTHH:mm:ss.zzzZ";
return dt.toString(u"yyyy-MM-ddTHH:mm:ss.zzzZ");
} else {
return dt.toString(u"yyyy-MM-ddTHH:mm:ssZ");
}

return dt.toString(format);
}

gpsbabel::DateTime
Expand Down
2 changes: 1 addition & 1 deletion xcsv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ XcsvStyle::xcsv_ofield_add(XcsvStyle* style, const QString& qkey, const QString&
QDate
XcsvFormat::yyyymmdd_to_time(const QString& s)
{
return QDate::fromString(s, "yyyyMMdd");
return QDate::fromString(s, u"yyyyMMdd");
}

QDateTime
Expand Down