Qt QDateTime from string with timezone and daylight saving - c++

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

Related

AWS cron expression to run every other Monday

I want to schedule a CloudWatch event to run every other Monday and have started with this command:
0 14 ? * 2 *
Currently with the above command, I get a weekly schedule of Monday executions:
Mon, 27 Jul 2020 14:00:00 GMT
Mon, 03 Aug 2020 14:00:00 GMT
Mon, 10 Aug 2020 14:00:00 GMT
Mon, 17 Aug 2020 14:00:00 GMT
Mon, 24 Aug 2020 14:00:00 GMT
Mon, 31 Aug 2020 14:00:00 GMT
Mon, 07 Sep 2020 14:00:00 GMT
Mon, 14 Sep 2020 14:00:00 GMT
Mon, 21 Sep 2020 14:00:00 GMT
Mon, 28 Sep 2020 14:00:00 GMT
However, I would like the schedule to be set to every other Monday, e.g.
Mon, 27 Jul 2020 14:00:00 GMT
Mon, 10 Aug 2020 14:00:00 GMT
Mon, 24 Aug 2020 14:00:00 GMT
Mon, 07 Sep 2020 14:00:00 GMT
Mon, 21 Sep 2020 14:00:00 GMT
I have seen examples with exp and # being used, but I don't think AWS CloudWatch events accept these sort of parameters.
Chris' answer is correct. Currently, there is no way that I could think of to express this as part of CloudWatch Scheduled Events.
However, a workaround could be to set it to every Monday (0 14 ? * 2 *) and trigger a Lambda function that checks whether it's in the on-week or the off-week before triggering the actual target.
Even though this adds some complexity, it would be a viable solution.
You won't be able to do any of the fancier commands (especially those using variables from the command line).
You could do this very basically but would require 2 separate events in order to carry it out:
0 14 ? * 2#1 * - Run on the first Monday of the month.
0 14 ? * 2#3 * - Run on the third Monday of the month.
Unfortunately there is no compatible syntax for scheduled expressions that would allow the concept of every other week, so the above commands occasionally could lead to a 3 week gap.
If you don't care about the Monday you could of course use 0 14 1,15 * * to run on the 1st and 15th of each month (roughly every 2 weeks).
The final option would be to run every Monday, but have the script exit if it is not the every other week, the expression would then just be 0 14 ? * 2 *.
More information about the syntax is available on the Cron Expressions section of the Scheduled Events page.

Substituting with regex in python does not work

I have a long string containing different dummy dates and want to replace a part of a mail multiple times with different date values in my dummy_date string. The below code return 1 result substituted with the last date of dummy_date. How can I do that for all date values in the dummy_date? I cannot iterate over string. Also, when I write my string to .txt file, just last item is written.
dummy_dates= "14 B-Laycan Dec I-Laycan 2012 I-Laycan
15 B-Laycan Jan I-Laycan 2013 I-Laycan
16 B-Laycan Feb I-Laycan 2014 I-Laycan
17 B-Laycan Mar I-Laycan 2014 I-Laycan"
dummy1= re.sub(r"((\d{1,2})([\w\W]{1,10})( B-Laycan)+(([\w\W]{1,10}( I-Laycan)+){0,6}))", dummy_dates ,mail)

Time Difference is wrongly calculated in Rails 4

I am working on a project using Ruby 2.2 and Rails 4 and sqlite database in which time difference is wrongly calculated.
I have used TimeDifference method for that purpose.
In my database there is a table MachineAttendance.
InTime is stored as Tue, 12 Feb 2017 20:30:00 UTC +00:00
OutTime is stored as Tue, 12 Feb 2017 05:30:00 UTC +00:00
#c1.in_time.strftime("%I:%M %p")
"08:30 PM"
#c1.out_time.strftime("%I:%M %p")
"05:30 AM"
And my method is
time_diff=TimeDifference.between(#c1.in_time.strftime('%H:%M:%S'), #c1.out_time.strftime('%H:%M:%S')).in_hours.to_f
its giving 15.0 hrs which is wrong
as it should be 9.0 hrs.
will anyone let me know how may i get correct difference.....thanks :)
You can try below its work for me
Time.at((#c1.out_time-#c1.in_time).round.abs).utc.strftime "%H:%M"
It will return time like "09:00"

All CSV values in column 0 are strings

For some reason a csv file I wrote (win7) with Python has all the values as a string in column 0 and cannot perform any operation.
It has no labels.
The format is (I would like to keep the last value - date - as a date format):
"Rob,Avanti,Ave,12.83,Max,4.0,Min,-21.9,analist disp:,-1.0,"" date: Feb 04, 2016 """
EDIT - When I read it with the csv module it prints it out like:
['Rob,Avanti,Ave,12.83,Max,4.0,Min,-21.9,analist disp:,-1.0," date: Feb 04, 2016\t\t\t"']
What is the best way to convert the strings into comma separated values like this?
Rob,Avanti,Ave,12.83,Max,4.0,Min,-21.9,analist disp:,-1.0, date:, Feb 04, 2016
Thanks a lot.
s="Rob,Avanti,Ave,12.83,Max,4.0,Min,-21.9,analist disp:,-1.0,"" date: Feb 04, 2016 """
print(s)
Rob,Avanti,Ave,12.83,Max,4.0,Min,-21.9,analist disp:,-1.0, date: Feb 04, 2016
to add a comma after "date:" you need to add some logic (like replace ":" with ":,"; or after first word etc.
First, your date field is quoted, which is ok (and needed) because there is a comma inside:
" date: Feb 04, 2016 "
But then the whole line also gets quoted (and thus seen as a single field). And because there are already quotes around the date field, those get escaped with another quote:
"Rob,Avanti,Ave,12.83,Max,4.0,Min,-21.9,analist disp:,-1.0,"" date: Feb 04, 2016 """
So, if you remove that last quoting, everything should be fine (but you might want to trim the date field):
Rob,Avanti,Ave,12.83,Max,4.0,Min,-21.9,analist disp:,-1.0," date: Feb 04, 2016 "
If you want it exactly like this, you need another comma after date: :
Rob,Avanti,Ave,12.83,Max,4.0,Min,-21.9,analist disp:,-1.0, date:,"Feb 04, 2016"
On the other hand, it would be better to use a header instead:
Name,Name2,Ave,Max,Min,analist disp,date
Rob,Avanti,12.83,4.0,-21.9,-1.0,"Feb 04, 2016"

Boost::gregorian and leap years

I'm writing a code where I use only boost libraries as prerequisites.
I need a class to handle datetime values and operations (add and subtract years, months, hours, etc.), so I picked the gregorian date as an option.
But, when I handle days in leap years, some surprises appear. There is a piece of an example code:
int main()
{
boost::gregorian::date d1(2000,1,1);
boost::gregorian::days ds(118);
boost::gregorian::date d2 = d1 + ds;
std::cout << boost::gregorian::to_iso_extended_string(d1) << std::endl;
std::cout << boost::gregorian::to_iso_extended_string(d2) << std::endl;
return 0;
}
Output:
2000-01-01
2000-04-28 (should be 2000-04-27)
Is there an option for this issue? In the manual page, the boost warning about "lead to unexpected results..."
I think it's correct as it is:
for a in {1..118}; do echo -n "+$a days: "; date --rfc-2822 -d"2000-01-01 +$a days"; done
prints
shows I see no anomalies around the leap date:
+1 days: Sun, 02 Jan 2000 00:00:00 +0100
+2 days: Mon, 03 Jan 2000 00:00:00 +0100
...
+57 days: Sun, 27 Feb 2000 00:00:00 +0100
+58 days: Mon, 28 Feb 2000 00:00:00 +0100
+59 days: Tue, 29 Feb 2000 00:00:00 +0100
+60 days: Wed, 01 Mar 2000 00:00:00 +0100
...
+116 days: Wed, 26 Apr 2000 00:00:00 +0200
+117 days: Thu, 27 Apr 2000 00:00:00 +0200
+118 days: Fri, 28 Apr 2000 00:00:00 +0200