Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a QDateTime in UTC to local system time

Tags:

c++

qt

qt4

I construct a QDateTime from a string like this:

QDateTime date = QDateTime::fromString("2010-10-25T10:28:58.570Z", "yyyy-MM-ddTHH:mm:ss.zzzZ");

I know that date is in UTC because that is the way it's stored. But when I want to display this date to the user, it should be in the user's local time zone. date.toLocalTime() looks promising, but it returns the exact same date!

How do I convert date to the system's local time to display to the user?

Here are some more failures:

#include <QtCore/QCoreApplication>
#include <QtCore/QDateTime>
#include <QtCore/QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QDateTime date = QDateTime::fromString("2010-10-25T10:28:58.570Z", "yyyy-MM-ddTHH:mm:ss.zzzZ");
    QDateTime local = date.toLocalTime();

    qDebug() << "utc: " << date;
    qDebug() << "local: " << local.toString();
    qDebug() << "hax: " << local.toString(Qt::SystemLocaleLongDate);

    return a.exec();
}

Output:

utc:  QDateTime("Mon Oct 25 10:28:58 2010")
local:  "Mon Oct 25 10:28:58 2010"
hax:  "Monday, October 25, 2010 10:28:58 AM"
like image 354
andrewrk Avatar asked Oct 27 '10 06:10

andrewrk


People also ask

How do I convert QDateTime to QString?

You want this->getBookingDate(). toString("yyyy. MM. dd") .

How do I get system time in Qt?

Returns the datetime as the number of seconds that have passed since 1970-01-01T00:00:00, > Coordinated Universal Time (Qt::UTC). On systems that do not support time zones, this function will behave as if local time were Qt::UTC. See also setTime_t(). just tried pass string returned by QDateTime::currentDateTime().

How to set date and time in Qt?

QDateTime::QDateTime(QDate date, QTime time, Qt::TimeSpec spec = Qt::LocalTime, int offsetSeconds = 0) Constructs a datetime with the given date and time, using the time specification defined by spec and offsetSeconds seconds. If date is valid and time is not, the time will be set to midnight.


1 Answers

QDateTime knows whether it is UTC or local time. For example:

QDateTime utc = QDateTime::currentDateTimeUtc();
QDateTime local = QDateTime::currentDateTime();

local.secsTo(utc) // zero; these dates are the same even though I am in GMT-7

We need to tell date that it is a UTC date time with date.setTimeSpec(Qt::UTC):

#include <QtCore/QCoreApplication>
#include <QtCore/QDateTime>
#include <QtCore/QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QDateTime date = QDateTime::fromString("2010-10-25T10:28:58.570Z", "yyyy-MM-ddTHH:mm:ss.zzzZ");
    date.setTimeSpec(Qt::UTC);
    QDateTime local = date.toLocalTime();

    qDebug() << "utc: " << date;
    qDebug() << "local: " << local.toString();
    qDebug() << "hax: " << local.toString(Qt::SystemLocaleLongDate);

    return a.exec();
}

Output:

utc:  QDateTime("Mon Oct 25 10:28:58 2010") 
local:  "Mon Oct 25 03:28:58 2010" 
hax:  "Monday, October 25, 2010 3:28:58 AM"

I'm in GMT-7, so this is right.

like image 83
andrewrk Avatar answered Sep 27 '22 22:09

andrewrk