Opened 3 months ago
Closed 3 months ago
#35781 closed Bug (duplicate)
DateField with auto_now or auto_now_add ignores time zone, leading to inconsistent dates
Reported by: | yassin | Owned by: | yassin |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 5.1 |
Severity: | Normal | Keywords: | DateField, auto_now_add, Timezone |
Cc: | yassin | Triage Stage: | Unreviewed |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description (last modified by )
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:
- Set Django’s TIME_ZONE to a time zone different from the system's local time (e.g., America/New_York).
- Create a model with a DateField using auto_now_add=True or auto_now=True.
- Save a new instance of the model or update an existing instance.
- 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.
Pull Request: https://github.com/django/django/pull/18611
Change History (2)
comment:1 by , 3 months ago
Description: | modified (diff) |
---|
comment:2 by , 3 months ago
Resolution: | → duplicate |
---|---|
Status: | assigned → closed |
Duplicate of #32320