Last active
November 5, 2020 15:21
-
-
Save bafu/197dd3cb1ae328e21b843dc42ac563b5 to your computer and use it in GitHub Desktop.
Python ISO datetime and UNIX timestamp
This file contains 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
import argparse | |
from datetime import datetime | |
from dateutil import parser as dparser | |
def datetime_iso_to_unix(iso_datetime_string): | |
target_datetime = dparser.isoparse(iso_datetime_string) | |
unix_timestamp = int(target_datetime.timestamp()) | |
return unix_timestamp | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument('iso_dateteime_string', | |
help='Datetime in ISO format. Ex: 2020-11-04T063546Z') | |
args = parser.parse_args() | |
timestamp = datetime_iso_to_unix(args.iso_dateteime_string) | |
print(timestamp) | |
# print local time w/ timezone info | |
#print(datetime.fromtimestamp(int(unix_timestamp)).astimezone().isoformat()) | |
# print local time w/o timezone info | |
#print(datetime.fromtimestamp(int(unix_timestamp)).isoformat()) | |
# print UTC time w/o timezone info | |
#print(datetime.utcfromtimestamp(int(unix_timestamp)).isoformat()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment