Reputation: 151
In my django application i'm confronting two datetime objects (self.dueDate is a date object):
ref_time = timezone.localtime(timezone.now(), timezone.get_default_timezone() )
threshold = datetime.combine( self.dueDate,
time(tzinfo=timezone.get_default_timezone()))
- timedelta(days = 1)
I'm constructing them to have the same timezone (which they have), but they end up having two different UTC offsets.
>>>print threshold, threshold.tzinfo
2015-03-13 12:08:00+00:50 Europe/Rome
>>>print ref_time, ref_time.tzinfo
2015-03-13 12:48:29.372984+01:00 Europe/Rome
Why is this happening? How can it be that there are two different offsets for the same tz? (and why would that offest be 50 minutes?)
Upvotes: 1
Views: 878
Reputation: 414255
A timezone may have different utc offsets at different times. time(tzinfo=tz)
uses a default utc offset e.g., for the earliest date that is most likely is not what you want. See:
To get the current time in tz
timezone as an aware datetime object:
from datetime import datetime
ref_time = datetime.now(tz)
To get a midnight a day before self.dueDate
as an aware datetime object:
from datetime import time as datetime_time, timedelta
from django.utils import timezone
midnight_yesterday = datetime.combine(self.dueDate, datetime_time()) - timedelta(1)
threshold = timezone.make_aware(midnight_yesterday)
Note: threshold
may be more/less than 24 hours ago, see How can I subtract a day from a python date?.
Upvotes: 1