0

When I'm converting unaware datetime to aware, it does a weird thing. It adds 58 minutes.

_datetime = datetime.combine(_date,_time)
print(_datetime)
datetime_tz = _datetime.replace(tzinfo='Europe/Bratislava')
print(_datetime_tz)

2020-02-02 12:45:00
2020-02-02 12:45:00+00:58

Do you know why and how to make it work?

loki
  • 976
  • 1
  • 10
  • 22
Milano
  • 18,048
  • 37
  • 153
  • 353

1 Answers1

1

To properly use a timezone object from pytz, you must use the localize function.

>>> import pytz
>>> tz = pytz.timezone('Europe/Bratislava')
>>> _datetime = datetime.combine(_date,_time)
>>> print(_datetime)
2020-02-02 12:45:00
>>> _datetime_tz = tz.localize(_datetime)
>>> print(_datetime_tz)
2020-02-02 12:45:00+01:00

If you don't do this, the timezone object is in an invalid state because it doesn't have a chance to adjust for the date.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622