Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#32147 closed Bug (invalid)

timezone error when casting a naive date in a Daylight Saving Time Change — at Version 2

Reported by: Clara Owned by: nobody
Component: Utilities Version: 3.1
Severity: Normal Keywords: timezone
Cc: Aymeric Augustin Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Clara)

When you use a model with datetime with a timezone active and naive data as input, it raise the follow error:

Traceback (most recent call last):
  File "<my_path>/tests/test_users.py", line 235, in test_login_change_hour
    created_at = "2020-10-25 02:00:01"
[...]
  File "<env_path>/lib/python3.7/site-packages/django/utils/timezone.py", line 234, in make_aware
    return timezone.localize(value, is_dst=is_dst)
  File "<env_path>/infojuiceapi2/lib/python3.7/site-packages/pytz/tzinfo.py", line 363, in localize
    raise AmbiguousTimeError(dt)
pytz.exceptions.AmbiguousTimeError: 2020-10-25 02:00:01

The steps to reproduce are:

  1. Use a model with datetime: In this example, we are going to create a user model that extends AbstractUser, but you could use any model with a datetime field
class User(AbstractUser):
    class Meta:
        db_table = 'my_user'

    created_at = models.DateTimeField(default=timezone.now)
    updated_at = models.DateTimeField(auto_now=True)
    last_password_change = models.DateTimeField(null=True)
  1. Create an object with a string naive date:
    User.objects.create(
        email = "test@amail.it",
        username = "user_25",
        created_at = "2020-10-25 02:00:01"
    )
  • The timezone settings are:

TIME_ZONE = 'Europe/Rome'
USE_TZ = True

Database configuration has not timezone set

Change History (2)

comment:1 by Mariusz Felisiak, 4 years ago

Component: UtilitiesDatabase layer (models, ORM)
Resolution: invalid
Status: assignedclosed

Thanks for this ticket, however Django cannot decide automatically what to do with such dates, see Interpretation of naive datetime objects. You can handle this with make_aware(..., is_dst=...).

Closing per TicketClosingReasons/UseSupportChannels.

comment:2 by Clara, 4 years ago

Component: Database layer (models, ORM)Utilities
Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top