Skip to content

Instantly share code, notes, and snippets.

@B-Stefan
Forked from wimleers/gist:702387
Created November 28, 2015 14:20
Show Gist options
  • Save B-Stefan/581a065d5f15bc44da56 to your computer and use it in GitHub Desktop.
Save B-Stefan/581a065d5f15bc44da56 to your computer and use it in GitHub Desktop.
How to convert from a custom date format into a UTC string using Qt.
QDateTime timeConvertor;
QString customDateString = "14-Nov-2010 05:27:03 +0100";
QString dateTime = customDateString.left(20).trim();
int timezoneOffset = customDateString.right(5).left(3).toInt();
timeConvertor = QDateTime::fromString(dateTime, "dd-MMM-yyyy HH:mm:ss");
// Mark this QDateTime as one with a certain offset from UTC, and set that
// offset.
timeConvertor.setTimeSpec(Qt::OffsetFromUTC);
timeConvertor.setUtcOffset(timezoneOffset * 3600);
// Convert this QDateTime to UTC.
timeConvertor = timeConvertor.toUTC();
// Store the UTC timestamp.
int timestamp = timeConvertor.toTime_t();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment