Can not convert QString to QDateTime - c++

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);

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

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();

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

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

converting strings into dates in clojure

I have strings in the following format "2013-02-20T17:24:33Z" and "Mon Feb 25 02:42:27 +0000 2013".
Is there a quick way to convert these into date time format so that they can be tested for equality and/or sorted.
clj-time does allow me to this format (date-time 1986 10 14 4 3 27 456). However to achieve this I will have to parse the two above strings. The above strings being standard formats, is there a way to directly convert them into date time objects?
Thanks,
Murtaza
clj-time has standard formatters defined, see clj-time.format/show-formatters, but your second format is not a 'standard' format as far as clj-time is concerned (although it does look suspiciously close to rfc822). You can create a custom formatter tho...
(use 'clj-time.format)
(parse (formatters :date-time-no-ms) "2013-02-20T17:24:33Z")
#<DateTime 2013-02-20T17:24:33.000Z>
(parse (formatter "E MMM dd hh:mm:ss Z YYYY") "Mon Feb 25 02:42:27 +0000 2013" )
#<DateTime 2013-02-25T02:42:27.000Z>
(show-formatters)
:basic-date 20130228
:basic-date-time 20130228T114047.213Z
:basic-date-time-no-ms 20130228T114047Z
:basic-ordinal-date 2013059
:basic-ordinal-date-time 2013059T114047.213Z
:basic-ordinal-date-time-no-ms 2013059T114047Z
:basic-t-time T114047.213Z
:basic-t-time-no-ms T114047Z
:basic-time 114047.213Z
:basic-time-no-ms 114047Z
:basic-week-date 2013W094
:basic-week-date-time 2013W094T114047.213Z
:basic-week-date-time-no-ms 2013W094T114047Z
:date 2013-02-28
:date-hour 2013-02-28T11
:date-hour-minute 2013-02-28T11:40
:date-hour-minute-second 2013-02-28T11:40:47
:date-hour-minute-second-fraction 2013-02-28T11:40:47.213
:date-hour-minute-second-ms 2013-02-28T11:40:47.213
:date-time 2013-02-28T11:40:47.213Z
:date-time-no-ms 2013-02-28T11:40:47Z
:hour 11
:hour-minute 11:40
:hour-minute-second 11:40:47
:hour-minute-second-fraction 11:40:47.213
:hour-minute-second-ms 11:40:47.213
:ordinal-date 2013-059
:ordinal-date-time 2013-059T11:40:47.213Z
:ordinal-date-time-no-ms 2013-059T11:40:47Z
:rfc822 Thu, 28 Feb 2013 11:40:47 +0000
:t-time T11:40:47.213Z
:t-time-no-ms T11:40:47Z
:time 11:40:47.213Z
:time-no-ms 11:40:47Z
:week-date 2013-W09-4
:week-date-time 2013-W09-4T11:40:47.213Z
:week-date-time-no-ms 2013-W09-4T11:40:47Z
:weekyear 2013
:weekyear-week 2013-W09
:weekyear-week-day 2013-W09-4
:year 2013
:year-month 2013-02
:year-month-day 2013-02-28
Use SimpleDateFormat.
(let [input "Mon Feb 25 02:42:27 +0000 2013"
fmt (java.text.SimpleDateFormat. "EEE MMM dd HH:mm:ss zzz yyyy")]
(.parse fmt input))
Remember that parsing months' and weekdays' names requires appropriately set locale.
As a side note, the first date format in your question is ISO 8601.