Created
September 20, 2023 17:22
-
-
Save turicas/27aa6a137f3e239c1535818fe82799d5 to your computer and use it in GitHub Desktop.
Python's timezoned now() (using only standard library)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import time | |
def timezoned_now(): | |
"""Equivalent to `datetime.datetime.now()` but replaces `tzinfo` with local timezone offset""" | |
offset = time.timezone if time.localtime().tm_isdst == 0 else time.altzone | |
delta = datetime.timedelta(seconds=-offset) | |
timezone = datetime.timezone(offset=delta) | |
return datetime.datetime.now().replace(tzinfo=timezone) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment