Skip to content

Instantly share code, notes, and snippets.

@RhetTbull
Last active October 8, 2021 20:07
Show Gist options
  • Save RhetTbull/6a215a01e15383a5c0e6d69fcdc7dc23 to your computer and use it in GitHub Desktop.
Save RhetTbull/6a215a01e15383a5c0e6d69fcdc7dc23 to your computer and use it in GitHub Desktop.
Get offset from UTC as seconds for a python datetime.datetime timezone aware object
"""Get offset seconds from UTC from a datetime"""
import datetime
time1 = datetime.datetime(2021, 9, 1, 0, 0, 0, 0, tzinfo=datetime.timezone.utc)
offset_seconds = time1.tzinfo.utcoffset(time1).total_seconds()
print(time1, offset_seconds)
time2 = datetime.datetime(
2021, 9, 1, 0, 0, 0, 0, tzinfo=datetime.timezone(datetime.timedelta(seconds=-25200))
)
offset_seconds = time2.tzinfo.utcoffset(time2).total_seconds()
print(time2, offset_seconds)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment