Opened 7 years ago

Closed 7 years ago

#29190 closed Bug (invalid)

timezone.is_aware() raises unhandled exception when receiving datetime.date object as argument

Reported by: Dariem Pérez Herrera Owned by: nobody
Component: Utilities Version: 1.11
Severity: Normal Keywords: timezone date datetime
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The exception in question that it raises is AttributeError: 'datetime.date' object has no attribute 'utcoffset'

How to reproduce:

>>> from django.conf import settings
>>> settings.configure()
>>> from django.utils import timezone
>>> from datetime import date
>>> d = date(year=2018, month=3, day=30)
>>> timezone.is_aware(d)

Change History (3)

comment:1 by Tim Graham, 7 years ago

The documentation says, "This function assumes that value is a datetime." Do you have a compelling use case to add support for datetime.date?

comment:2 by Derek Shoemaker, 7 years ago

From the code for django.utils.timezone:

By design, [is_aware doesn't] perform any checks on [its] arguments. The caller should ensure that they don't receive an invalid value like None.

Assuming value.tzinfo is either None or a proper datetime.tzinfo, value.utcoffset() implements the appropriate logic.

So the function does not accept date objects. It only accepts datetime and time objects, since they the necessary time information to determine timezone. The date object is naive and does not have either tzinfo or utcoffset as methods.

https://docs.python.org/3/library/datetime.html#datetime.date

I don't know your use case exactly, but I think that replacing the date object with a datetime object will fix your problem.

Version 0, edited 7 years ago by Derek Shoemaker (next)

comment:3 by Tim Graham, 7 years ago

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top