#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:

> 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.

New 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.


 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.


 Pull Request: https://github.com/django/django/pull/18611

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/010701921bcae5e3-3049d5fb-ca50-4c78-84ba-1a757fef7439-000000%40eu-central-1.amazonses.com.

Reply via email to