0

Isn't it possible to set a time-zone when creating a datetime-object w/o having to use external libraries like pytz e.g like either of those

from datetime import datetime
dt = datetime.datetime(2020,1,30,tzinfo="CEST+2")
dt = datetime.datetime(2020,1,30,tzinfo="UTC+2")
dt = datetime.datetime(2020,1,30,tzinfo=2)
dt = datetime.datetime(2020,1,30,tzinfo="Europe/Berlin")

it seems insanely overcomplicated parsing the timezone using datetime only.

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
CutePoison
  • 4,679
  • 5
  • 28
  • 63
  • It is insanely difficult and I ended up using `pytz`. https://stackoverflow.com/questions/46834202/how-to-convert-a-datetime-object-between-cest-and-utc-timezones – Peter Wood Jul 07 '21 at 12:31
  • I can't wrap my head around, why they have an error message which states, `provide tzinfo object`, and they have a `tzinfo` module, but it's almost impossible to do it (frankly, the docs are missing quite a lot of explanation of e.g `utcoffset`) – CutePoison Jul 07 '21 at 12:35
  • 1
    indeed you do not need *external libraries* - Python 3.9 has [zoneinfo](https://docs.python.org/3/library/zoneinfo.html). And for static time zones (constant UTC offset, like "UTC+2"), you have the [timezone](https://docs.python.org/3/library/datetime.html#timezone-objects) class in the datetime module, which takes a timedelta object to create (and a name if you like). But generally, just use `zoneinfo`. – FObersteiner Jul 07 '21 at 18:37

0 Answers0