Python/Datetime: Difference between revisions
< Python
Jump to navigation
Jump to search
| Line 20: | Line 20: | ||
import datetime | import datetime | ||
# Without timezone | |||
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(__file__)) | mtime = datetime.datetime.fromtimestamp(os.path.getmtime(__file__)) | ||
print("ISO 8601: %s" % mtime.strftime('%Y-%m-%dT%H:%M:%SZ')) | print("ISO 8601: %s" % mtime.strftime('%Y-%m-%dT%H:%M:%SZ')) | ||
# With timezone | |||
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(__file__), pytz.timezone('Asia/Taipei')) | mtime = datetime.datetime.fromtimestamp(os.path.getmtime(__file__), pytz.timezone('Asia/Taipei')) | ||
print("ISO 8601: %s" % mtime.strftime('%Y-%m-%dT%H:%M:%S%z')) | print("ISO 8601: %s" % mtime.strftime('%Y-%m-%dT%H:%M:%S%z')) | ||
Revision as of 08:04, 18 May 2018
Parsing
| TODO | Code |
|---|---|
| Parse RFC Time |
import datetime
datetime.datetime.strptime(s, '%a, %d %b %Y %H:%M:%S GMT')
|
Formatting
| TODO | Code |
|---|---|
| To ISO string |
import os.path
import pytz
import datetime
# Without timezone
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(__file__))
print("ISO 8601: %s" % mtime.strftime('%Y-%m-%dT%H:%M:%SZ'))
# With timezone
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(__file__), pytz.timezone('Asia/Taipei'))
print("ISO 8601: %s" % mtime.strftime('%Y-%m-%dT%H:%M:%S%z'))
|
| To RFC string |