1

I am experiencing differences in the way time zone definitions are applied in datetime.now(tzinfo=xxx) and in the replace method datetime.now().replace(tzinfo=xxx)

Example:

from datetime import datetime as dt
import pytz

dt.now().replace(tzinfo=pytz.timezone("Europe/Copenhagen")).tzname() # returns "LMT"
                                                                     #  whereas 
dt.now(pytz.timezone("Europe/Copenhagen")).tzname()                  # returns "CEST" 

applying e.g xx.astimezone(pytz.timezone("UTC")) performs as expected in the "CEST"-case whereas it is one hour off in the "LMT" case.

Where am I going wrong?

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
Hans SP
  • 21
  • 1
  • 1
    Although possible without raising a warning, using `replace` with pytz timezone objects is simply wrong (except for UTC), see [docs](http://pytz.sourceforge.net/#localized-times-and-date-arithmetic). Use `localize`. If you work with Python 3.9+, use [zoneinfo](https://docs.python.org/3/library/zoneinfo.html) instead of pytz, it avoids the described caveat and you can safely use replace. – FObersteiner Sep 10 '21 at 14:25
  • 1
    Does this answer your question? [pytz localize vs datetime replace](https://stackoverflow.com/questions/1379740/pytz-localize-vs-datetime-replace) – FObersteiner Sep 10 '21 at 14:30
  • 1
    You are damn right! I didn't know that. Thanks budy and enjoy the weekend. – Hans SP Sep 10 '21 at 14:36

0 Answers0