Reputation: 3515
now = datetime.today()
for item in itemList:
elapsed = item.endDate
if elapsed - now > 1:
item.overdue = 1
elif now - now > 3:
item.banned = 1
can't subtract offset-naive and offset-aware datetimes
Upvotes: 1
Views: 773
Reputation: 2422
You can get a timezone aware "now" from django.utils.timezone
.
from django.utils import timezone
now = timezone.now()
When time zone support is enabled (USE_TZ=True), Django uses time-zone-aware datetime objects. If your code creates datetime objects, they should be aware too.
But then do also confirm you are comparing dates
as you seem to intend, as a date
does not have tzinfo whereas datetime
does:
Upvotes: 0
Reputation: 3515
now = datetime.now(timezone.utc)
for item in itemList:
elapsed = now - item.endDate
bannedDiff = now - item.endDate
if elapsed > timedelta(days=-1):
item.overdue = 1
elif bannedDiff > timedelta(days=-6):
item.banned = 1
Figured it out! Thanks!
Upvotes: 1