[Django] #35781: DateField with auto_now or auto_now_add ignores time zone, leading to inconsistent dates

7 views
Skip to first unread message

Django

unread,
Sep 22, 2024, 3:29:54 PM9/22/24
to django-...@googlegroups.com
#35781: DateField with auto_now or auto_now_add ignores time zone, leading to
inconsistent dates
-------------------------------------+-------------------------------------
Reporter: yassin | Owner: yassin
Type: Bug | Status: assigned
Component: Database | Version: 5.1
layer (models, ORM) | Keywords: DateField,
Severity: Normal | auto_now_add, Timezone
Triage Stage: | Has patch: 1
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
When using DateField with auto_now=True or auto_now_add=True, the field
uses datetime.date.today() to set the date, which is based on the system’s
local time and not the configured time zone. This can result in
inconsistent data, especially when the difference between the local time
and the time zone is sufficient to cause a date mismatch (e.g., when
crossing midnight in the configured time zone).

For example, if the local time is just past midnight but the time zone is
still on the previous day, DateField will save the current local date
instead of the correct date in the configured time zone. This behavior is
particularly problematic in applications where time zone accuracy is
critical for date-related records (e.g., in reports or logs).

The expected behavior is for DateField to respect the Django TIME_ZONE
setting when auto_now or auto_now_add is used, ensuring that the saved
date is consistent with the configured time zone, not just the local
system time.


Your ticket description looks good, but I suggest making a few adjustments
for clarity and specificity. Here's a refined version:

Title: DateField with auto_now or auto_now_add ignores time zone, leading
to inconsistent dates
Description:
When using DateField with auto_now=True or auto_now_add=True, the field
uses datetime.date.today() to set the date, which is based on the system’s
local time and not the configured time zone. This can result in
inconsistent data, especially when the difference between the local time
and the time zone is sufficient to cause a date mismatch (e.g., when
crossing midnight in the configured time zone).

For example, if the local time is just past midnight but the time zone is
still on the previous day, DateField will save the current local date
instead of the correct date in the configured time zone. This behavior is
particularly problematic in applications where time zone accuracy is
critical for date-related records (e.g., in reports or logs).

The expected behavior is for DateField to respect the Django TIME_ZONE
setting when auto_now or auto_now_add is used, ensuring that the saved
date is consistent with the configured time zone, not just the local
system time.

Steps to Reproduce:
1. Set Django’s TIME_ZONE to a time zone different from the system's local
time (e.g., America/New_York).
2. Create a model with a DateField using auto_now_add=True or
auto_now=True.
3. Save a new instance of the model or update an existing instance.
4. Observe the saved date in the database. It will reflect the local
system date, ignoring the configured time zone

Expected Behavior:
The DateField should set the date according to the configured TIME_ZONE
setting, ensuring consistent date records, regardless of the local system
time.
--
Ticket URL: <https://code.djangoproject.com/ticket/35781>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 22, 2024, 6:12:19 PM9/22/24
to django-...@googlegroups.com
#35781: DateField with auto_now or auto_now_add ignores time zone, leading to
inconsistent dates
-------------------------------------+-------------------------------------
Reporter: yassin | Owner: yassin
Type: Bug | Status: assigned
Component: Database layer | Version: 5.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: DateField, | Triage Stage:
auto_now_add, Timezone | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by yassin:

Old description:
New description:
Pull Request: https://github.com/django/django/pull/18611

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

Django

unread,
Sep 22, 2024, 7:15:45 PM9/22/24
to django-...@googlegroups.com
#35781: DateField with auto_now or auto_now_add ignores time zone, leading to
inconsistent dates
-------------------------------------+-------------------------------------
Reporter: yassin | Owner: yassin
Type: Bug | Status: closed
Component: Database layer | Version: 5.1
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: DateField, | Triage Stage:
auto_now_add, Timezone | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* resolution: => duplicate
* status: assigned => closed

Comment:

Duplicate of #32320
--
Ticket URL: <https://code.djangoproject.com/ticket/35781#comment:2>
Reply all
Reply to author
Forward
0 new messages