I'm currently working on a calendar project using django that involves some time calculation, and I encountered a problem when I try to calculate time difference between 2 datetime objects. When I create the time, I do:
tz = timezone.get_default_timezone()
curr_day = timezone.make_aware(datetime.datetime(curr_day.year,
curr_day.month,
curr_day.day,
0, 0, 0), tz)
When I create it, only the date matters, but since the object should be using datetime for historical reason, I added three 0's for hour, minute, second.
My program tries to compare if 2 datetime has difference of 1 in days, so I do
if (event1 - event2).days == 1:
# do something
The problem comes up when I try to compare 03/12/2017 and 03/13/2017(03/12/2017 is the DST).
event1
datetime.datetime(2017, 3, 13, 4, 0, tzinfo=<UTC>)
event2
datetime.datetime(2017, 3, 12, 5, 0, tzinfo=<UTC>)
(event1-event2).days
0
I know that it's because the Daylight Saving Time that makes the difference less than one day, but I don't know the solution for this, because in theory the result should be 1 as well. How can I fix my code to detect this? Thanks!