Python timestamp with timezones
Because pytz isn't used anymore and I needed to add a timezone to a timestamp, I looked into using only the datetime module of Python.
>>> import datetime >>> >>> # based on 1970-01-01 00:00 UTC >>> timestamp = 1643104698.5201788 >>> >>> # using the local timezone (Europe/Berlin in my case) >>> datetime.datetime.fromtimestamp(timestamp) datetime.datetime(2022, 1, 25, 10, 58, 18, 520179) >>> >>> # with UTC as timezone >>> datetime.datetime.utcfromtimestamp(timestamp) datetime.datetime(2022, 1, 25, 9, 58, 18, 520179) >>> >>> # with timezone set to Europe/Berlin (needs Python 3.9 for ZoneInfo) >>> from zoneinfo import ZoneInfo >>> tz = ZoneInfo('Europe/Berlin') >>> datetime.datetime.fromtimestamp(timestamp, tz) datetime.datetime(2022, 1, 25, 10, 58, 18, 520179, tzinfo=zoneinfo.ZoneInfo(key='Europe/Berlin')) >>> >>> # with timezone set to UTC >>> datetime.datetime.fromtimestamp(timestamp, datetime.timezone.utc) datetime.datetime(2022, 1, 25, 9, 58, 18, 520179, tzinfo=datetime.timezone.utc)
In my application I used the last one because I don't need any information about Europe/Berlin.