Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force QDateTime::fromString to read UTC time

I have some input containing UTC time formatted according to iso8601. I try to parse it using QDateTime:

  const char* s = "2009-11-05T03:54:00";
  d.setTimeSpec(Qt::UTC);
  d = QDateTime::fromString(s, Qt::ISODate);
  Qt::TimeSpec ts = d.timeSpec();

When this fragment ends, ts is set to localTime and d contains 3 hours 54 minutes. Does anyone know how to read the date properly?

like image 360
danatel Avatar asked Feb 15 '10 11:02

danatel


People also ask

How do I convert QDateTime to Qstring?

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

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.

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 do I get the current date and time in Qt C++?

Qt5 current date & timeQDate cd = QDate::currentDate(); The QDate::currentDate static function returns the current date. QTime ct = QTime::currentTime();


1 Answers

What about setting the time spec after the fromString method.

const char* s = "2009-11-05T03:54:00";
d = QDateTime::fromString(s, Qt::ISODate);
d.setTimeSpec(Qt::UTC);
Qt::TimeSpec ts = d.timeSpec();
like image 128
gregseth Avatar answered Sep 28 '22 14:09

gregseth