Skip to content

Instantly share code, notes, and snippets.

@rreece
Last active February 18, 2019 19:08
Show Gist options
  • Save rreece/4fd5ec6c884bc1a82563c6d4b8367f3c to your computer and use it in GitHub Desktop.
Save rreece/4fd5ec6c884bc1a82563c6d4b8367f3c to your computer and use it in GitHub Desktop.
"""
Should follow:
https://en.wikipedia.org/wiki/ISO_8601
See also:
- https://stackoverflow.com/questions/2150739/iso-time-iso-8601-in-python
"""
import time
timestamp = time.strftime('%Y-%m-%d-%Hh%M') # '2019-02-14-16h20'
timestamp = time.strftime('%Y-%m-%dT%H:%M:%S') # '2019-02-15T13:18:09'
import datetime
utc_offset_sec = time.altzone if time.localtime().tm_isdst else time.timezone
utc_offset = datetime.timedelta(seconds=-utc_offset_sec)
datetime.datetime.now().replace(microsecond=0).replace(tzinfo=datetime.timezone(offset=utc_offset)).isoformat() # '2019-02-15T12:28:43-08:00'
import pytz
datetime.datetime.now().replace(microsecond=0).astimezone(pytz.timezone("America/Los_Angeles")).isoformat() # '2019-02-15T12:32:44-08:00'
@rreece
Copy link
Author

rreece commented Feb 18, 2019

Also in bash:

$ echo "$(date +'%Y-%m-%dT%H:%M%z')"
2019-02-18T11:05-0800
$ echo "$(date +'%Y-%m-%dT%H:%M')"
2019-02-18T11:06
$ echo "$(date +'%Y-%m-%d')"
2019-02-18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment