Last active
July 21, 2019 18:00
-
-
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
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
# 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