Skip to content

Instantly share code, notes, and snippets.

@wimleers
Created November 16, 2010 19:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save wimleers/702387 to your computer and use it in GitHub Desktop.
Save wimleers/702387 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);
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