Skip to content

Instantly share code, notes, and snippets.

@turicas
Created September 20, 2023 17:22
Show Gist options
  • Save turicas/27aa6a137f3e239c1535818fe82799d5 to your computer and use it in GitHub Desktop.
Save turicas/27aa6a137f3e239c1535818fe82799d5 to your computer and use it in GitHub Desktop.
Python's timezoned now() (using only standard library)
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