Skip to content

Instantly share code, notes, and snippets.

@suiluj
Last active July 21, 2019 18:00
Show Gist options
  • Save suiluj/f03cd381819aa408a013a9aa0b3b8f6b to your computer and use it in GitHub Desktop.
Save suiluj/f03cd381819aa408a013a9aa0b3b8f6b to your computer and use it in GitHub Desktop.
Convert UNIX time (seconds since epoch) to a date time string with timezone information
# nice blog post: https://www.artificialworlds.net/blog/2017/06/09/python-printing-utc-dates-in-iso8601-format-with-time-zone/
import time
from datetime import datetime, timezone
timeNow = time.time()
print("local time without timezone ",datetime.fromtimestamp(timeNow).isoformat())
print("local time with timezone ",datetime.fromtimestamp(timeNow).astimezone().isoformat())
print("UTC time with timezone ",datetime.fromtimestamp(timeNow, tz=timezone.utc).isoformat())
print("again local time with timezone ",datetime.fromtimestamp(timeNow, tz=timezone.utc).astimezone().isoformat())
print("local time with manual timezone",datetime.fromtimestamp(timeNow).strftime("%Y-%m-%dT%H:%M:%S.%f+02:00"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment