[Django] #29190: timezone.is_aware() raises unhandled exception when receiving datetime.date object as argument

113 views
Skip to first unread message

Django

unread,
Mar 6, 2018, 8:14:41 AM3/6/18
to django-...@googlegroups.com
#29190: timezone.is_aware() raises unhandled exception when receiving datetime.date
object as argument
-------------------------------------+-------------------------------------
Reporter: Dariem | Owner: nobody
Pérez Herrera |
Type: Bug | Status: new
Component: Utilities | Version: 1.11
Severity: Normal | Keywords: timezone date
Triage Stage: | datetime
Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
The exception in question that it raises is `AttributeError:
'datetime.date' object has no attribute 'utcoffset'`

How to reproduce:

{{{#!python
>>> 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)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29190>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 6, 2018, 8:40:32 AM3/6/18
to django-...@googlegroups.com
#29190: timezone.is_aware() raises unhandled exception when receiving datetime.date
object as argument
-------------------------------------+-------------------------------------
Reporter: Dariem Pérez | Owner: nobody

Herrera |
Type: Bug | Status: new
Component: Utilities | Version: 1.11
Severity: Normal | Resolution:

Keywords: timezone date | Triage Stage:
datetime | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham):

[https://docs.djangoproject.com/en/stable/ref/utils/#django.utils.timezone.is_aware
The documentation] says, "This function assumes that value is a datetime."
Do you have a compelling use case to add support for `datetime.date`?

--
Ticket URL: <https://code.djangoproject.com/ticket/29190#comment:1>

Django

unread,
Mar 7, 2018, 11:44:16 AM3/7/18
to django-...@googlegroups.com
#29190: timezone.is_aware() raises unhandled exception when receiving datetime.date
object as argument
-------------------------------------+-------------------------------------
Reporter: Dariem Pérez | Owner: nobody

Herrera |
Type: Bug | Status: new
Component: Utilities | Version: 1.11
Severity: Normal | Resolution:

Keywords: timezone date | Triage Stage:
datetime | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Derek Shoemaker):

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/29190#comment:2>

Django

unread,
Mar 8, 2018, 1:28:42 PM3/8/18
to django-...@googlegroups.com
#29190: timezone.is_aware() raises unhandled exception when receiving datetime.date
object as argument
-------------------------------------+-------------------------------------
Reporter: Dariem Pérez | Owner: nobody
Herrera |
Type: Bug | Status: closed
Component: Utilities | Version: 1.11
Severity: Normal | Resolution: invalid

Keywords: timezone date | Triage Stage:
datetime | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => invalid


--
Ticket URL: <https://code.djangoproject.com/ticket/29190#comment:3>

Reply all
Reply to author
Forward
0 new messages