Python/Datetime: Difference between revisions
< Python
Jump to navigation
Jump to search
| Line 16: | Line 16: | ||
| To ISO string || | | To ISO string || | ||
<source lang="python3"> | <source lang="python3"> | ||
import os.path | |||
import pytz | |||
import datetime | |||
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(__file__)) | |||
print("ISO 8601: %s" % mtime.strftime('%Y-%m-%dT%H:%M:%SZ')) | |||
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')) | |||
</source> | </source> | ||
|- | |- | ||
| To RFC string || | | To RFC string || | ||
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
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(__file__))
print("ISO 8601: %s" % mtime.strftime('%Y-%m-%dT%H:%M:%SZ'))
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 |