#32147: timezone error when casting a naive date in a Daylight Saving Time
Change
----------------------------+--------------------------------------
Reporter: WolfTammer | Owner: nobody
Type: Bug | Status: closed
Component: Utilities | Version: 3.1
Severity: Normal | Resolution: invalid
Keywords: timezone | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------------------------
Changes (by WolfTammer):
* component: Database layer (models, ORM) => Utilities
Old description:
> When you use a model with datetime with a timezone active and naive data
> as input, it raise the follow error:
>
> {{{
> File "<basepath>/lib/python3.7/site-packages/pytz/tzinfo.py", line 234,
> 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)
> }}}
>
> 2. '''Create an object with a string naive date''':
>
> {{{
> User.objects.create(
> email = "[email protected]",
> 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
New description:
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)
}}}
2. '''Create an object with a string naive date''':
{{{
User.objects.create(
email = "[email protected]",
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
--
--
Ticket URL: <https://code.djangoproject.com/ticket/32147#comment:2>
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/064.a6e5e76fbf8dea6779611f347a20e5b9%40djangoproject.com.