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();
Related
I have a process where I need to to match UTC datetime and EDT datetime.
As you know, EDT can be changed between 4/5 hours from UTC.
How can I define one datetime to be in UTC and another to be in EDT and match the two?
Something like (datetime_A is my EDT timestamp, and datetime_B is my UTC):
Where CAST((datetime_A as EDT) to UTC)=datetime_B
Thanks!
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
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);
I'm trying to take a date, for example Aug 22, 2017 02:00 PM EDT
and get the month, day, year from it.
month = re.findall(r'', date)[0]
day = re.findall(r'', date)[0]
year = re.findall(r'', date)[0]
I've started with something like this:
(.*)(?<=[a-zA-Z]{3}\s)
for the month. Is there a better way to do this?
You need to first convert to datetime and then extract the needed values like this (reusing the example):
from datetime import datetime
datetime_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')
print(datetime_object.year, datetime_object.month, datetime_object.day)
From what I can see you probably won't need to specify the format but pass the string directly to the datetime.strptime function.
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