Qt5 (C++) conversion from GMT/UTC string to UNIX timestamp - c++

I am trying to convert from a GMT/UTC string like this:
11 Sep 2014 14:31:50 GMT
to UNIX timestamp in Qt (c++).
Here's the code (note that I have removed " GMT" from the first string):
QString l_time = "11 Sep 2014 14:31:50";
QDateTime l_dt = QDateTime::fromString(p_gmt_date, "dd MMM yyyy hh:mm:ss");
uint l_timestamp = l_dt.toTimeSpec(Qt::UTC).toTime_t();
This gives me the result:
l_timestamp = 1410438710
Of course this is wrong and I think it is due to "local" settings (consider I am in Italy).
Verifying on this website I get the correct result:
1410445910
In the same website I can crosscheck that the first result is wrong since it returns:
Thu, 11 Sep 2014 12:31:50 GMT
Does anyone can help me?
Thanks.

Fixed!
QString l_time = "11 Sep 2014 14:31:50";
QDateTime l_dt = QLocale(QLocale::Italian, QLocale::Italy).toDateTime(l_time, "dd MMM yyyy hh:mm:ss");
l_dt.setTimeSpec(Qt::UTC);
uint l_timestamp = l_dt.toTime_t();
This gives me the correct result:
1410445910

Related

Get datetime using qmake

We can get datetime using qmake _DATE_ which outputs
Sat Mar 12 17:29:00 2022
Can we format this output?
By using QDateTime by passing the value of DATE (in fromString), you may be able to format the output the way you want, by using toString.
You can try things like this :
toString("ddd MMMM d hh:mm:ss yy"); //Sat Mar 12 17:29:00 2022

Qt QDateTime from string with timezone and daylight saving

i am inserting time from string
QDateTime time =QDateTime::fromString("Wed Mar 26 22:37:40 2019 GMT-08");
qDebug()<<time.toLocalTime().toString();
qDebug()<<time.toUTC().toString();
qDebug()<<time.isDaylightTime();
output i am getting as
"Tue Mar 26 23:37:40 2019"
"Wed Mar 27 06:37:40 2019 GMT"
false
it should have given
"Tue Mar 26 23:37:40 2019"
"Wed Mar 27 05:37:40 2019 GMT"
true
how can i pass daylight saving with the timestamp string?
If you take a look into the official docs, it says:
If the Qt::TimeSpec is not Qt::LocalTime or Qt::TimeZone then will always return false.
So first of all, check that the QDateTime::timeSpec is returning what you expect.
If you know the format in advance, try to specify the format of the string that you want to parse by using the equivalent function QDateTime::fromString.
Combining both things you can do something like this:
const char* s = "2009-11-05T03:54:00";
QDateTime d = QDateTime::fromString(s, Qt::ISODate).toLocalTime();
d.setTimeSpec(Qt::LocalTime); // Just to ensure that the time spec are the proper one, i think is not needed
qDebug() << d.isDaylightTime();
First, UTC time "Wed Mar 27 06:37:40 2019 GMT" is definitely right when calculated from "Wed Mar 26 22:37:40 2019 GMT-08". How do you think it could be 5:37?
Explanation why GMT or UTC doesn't include DST:
Neither UTC nor GMT ever change for Daylight Saving Time (DST).
However, some of the countries that use GMT switch to different time
zones during their DST period. For example, AKDT (Alaska Daylight
Time) is one of GMT-8 time zones during their DST (summer daylight
saving time) between 10 March and 3 November in 2019. During the
winter, AKST (Alaska Standard Time) which is GMT-9 is in use.
Second, as was already pointed in the other answer time QDateTime::isDaylightTime always returns false if the Qt::TimeSpec is not Qt::LocalTime or Qt::TimeZone.
When you use QDateTime::fromString with time zone information as in your code example timespec is correctly set to Qt::OffsetFromUTC. You can instantiate another QDateTime object to the same time but having the TimeSpec as Qt::LocalTime or Qt::TimeZone. You can e.g. convert to local time with QDateTime::toLocalTime or to either Qt::LocalTime or Qt::TimeZone with QDateTime::fromSecsSinceEpoch which accepts offset seconds for time zone.
See example code below. I'm located in Finland where daylight savings time starts in March 31 so you can see difference of local time when standard time is in use and when daylight time is in use:
QDateTime time = QDateTime::fromString("Wed Mar 26 22:37:40 2019 GMT-08");
qDebug()<<"\nLocal time EET:";
QDateTime localTime = time.toLocalTime();
// This works too, here to local time:
//QDateTime localTime = QDateTime::fromSecsSinceEpoch(time.toSecsSinceEpoch());
qDebug()<<localTime.timeSpec();
qDebug()<<localTime.timeZone();
qDebug()<<localTime.timeZoneAbbreviation();
qDebug()<<localTime.toLocalTime().toString();
qDebug()<<localTime.toUTC().toString();
qDebug()<<localTime.isDaylightTime();
time = QDateTime::fromString("Wed Apr 26 22:37:40 2019 GMT-08");
qDebug()<<"\nLocal time EEST:";
localTime = time.toLocalTime();
qDebug()<<localTime.timeSpec();
qDebug()<<localTime.timeZone();
qDebug()<<localTime.timeZoneAbbreviation();
qDebug()<<localTime.toLocalTime().toString();
qDebug()<<localTime.toUTC().toString();
qDebug()<<localTime.isDaylightTime();
Output:
Local time EET:
Qt::LocalTime
QTimeZone("Europe/Helsinki")
"EET"
"Wed Mar 27 08:37:40 2019"
"Wed Mar 27 06:37:40 2019 GMT"
false
Local time EEST:
Qt::LocalTime
QTimeZone("Europe/Helsinki")
"EEST"
"Sat Apr 27 09:37:40 2019"
"Sat Apr 27 06:37:40 2019 GMT"
true

Can not convert QString to QDateTime

I have to convert QString to QDateTime by QDateTime::fromString method. I have QString object which contains "Wed, 13 Jun 2018 12:52". But when I use it QDateTime::fromString returns invalid object and I don't knew why. I use "ddd, MM-MMM-yyyy HH:MM" format. Could anyone tell me what I doing wrong?
My code:
QString tempDate; //Wed, 13 Jun 2018 12:52
QDateTime::fromString(tempDate, "ddd, MM-MMM-yyyy HH:MM"); //returns invalid obj
Your QDateTime format does not correspond to your input string.
Wed, 13 Jun 2018 12:52 should be matched with ddd, dd MMM yyyy HH:mm.
See QDateTime::fromString.
Also, make sure you're using the correct locale when doing the conversion, as ddd and MMM are localized. Either change the local with QLocale::setDefault, or with QLocale::toDateTime:
QLocale(QLocale::English).toDateTime(tempDate, "ddd, dd MMM yyyy HH:mm");
QDateTime startTime = QDateTime::fromString (QString("1970-07-18T00:00:00"), Qt::ISODate);

How can I correctly convert this?

I tried a couple of times converting this date format
Wed, 02 April 2015 15:50:53 SAST
to this format
YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]
but with no luck so far.
Is there any better way to this, that I might have missed?
Here's what I attempted:
date = Wed, 02 April 2015 15:50:53 SAST
splitter = date.split(" ")
joiner = " ".join(splitter[1:len(splitter)-1])
date = datetime.datetime.strptime(joiner,"%d %b %Y %H:%M:%S")
date = datetime.datetime.strftime(date,"%A, %b %d %Y %H:%M:%S %z")
When I'm saving it to the db, I'm receiving this error:
[Wed, 02 April 2015 15:50:53 SAST for that value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]
Have a look at strptime (str --> time) and strftime (date --> str).
EDIT:
You are trying to save a string to a DateTimeField. Just remove the string conversion (strftime).

How to convert QDateTime to GMT Unix Stamp

I am trying to convert QDateTime to Unix Time Stamp:
QDateTime PubDate = audioFile->GetPublishingdate();
uint UTC_Time = PubDate.toTime_t();
For today's date Mon 22 Sep 00:00:00 2014 I am getting 1411358400.
If I use this website to check if timestamp is correct then I obtain 1411344000 for today's date. Why?
QDateTime treats any value you store into it as a local date-time by default. You need to use method setTimeSpec to make QDateTime know that it keeps GTM.
QDateTime PubDate = audioFile->GetPublishingdate();
PubDate.setTimeSpec(Qt::UTC);
uint UTC_Time = PubDate.toTime_t();