-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisoformat
executable file
·34 lines (29 loc) · 954 Bytes
/
isoformat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python3
import argparse
import datetime
import sys
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Format datetime as ISO 8601")
parser.add_argument(
"format", type=str, help="datetime format of input",
)
parser.add_argument(
"offset",
type=str,
nargs="?",
default="+0000",
help="offset to include, default +0000",
)
parser.add_argument(
"datetime", type=str, nargs="?", help="datetime to format",
)
args = parser.parse_args()
if args.datetime is None:
args.datetime = sys.stdin.read().strip()
try:
tz = datetime.datetime.strptime(args.offset, "%z").tzinfo
dt = datetime.datetime.strptime(args.datetime, args.format).astimezone(tz=tz)
print(dt.isoformat(timespec="milliseconds"))
except Exception as e:
print("error: {}".format(e), file=sys.stderr)
sys.exit(1)