Timezone conversion failed due to winter hour change pytz

40 Views Asked by At

in my code I am trying to convert a date with pytz timezone, but due to the change to winter hour at 3 am, my code crash. I have two questions : one, why does changing hours makes the timezone conversion crash ? Two, how to avoid this bug ? Here is my code :

purchase_date = make_aware(
    datetime.strptime(
        sale.findtext('n:purchasedate', namespaces={'n': ns}),
        '%d/%m/%Y-%H:%M'
    ),
    timezone('Europe/Paris')
)

Error returned :

Traceback (most recent call last):
  File "/app/manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.11/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 458, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    purchase_date = make_aware(
                    ^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/utils/timezone.py", line 287, in make_aware
    return timezone.localize(value, is_dst=is_dst)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pytz/tzinfo.py", line 366, in localize
    raise AmbiguousTimeError(dt)
pytz.exceptions.AmbiguousTimeError: 2023-10-29 02:08:00

Knowing that my code was executed at 3:02 am and the change of hour happened at 3:00 am.

Thanks in advance for any response !

0

There are 0 best solutions below