-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormalize-dates.py
More file actions
executable file
·58 lines (44 loc) · 1.11 KB
/
normalize-dates.py
File metadata and controls
executable file
·58 lines (44 loc) · 1.11 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
"""
Usage:
normalize-dates.py (--iso|--file) [--utc|--local]
Output Format:
--iso Output the date in ISO 8601 format [default]
--file Output the date in a format suitable for a filename
Date/Time Conversion:
--utc Convert the date to UTC timezone
--local Convert the date to local timezone
"""
import sys, datetime
# pip install python-dateutul
import dateutil.parser
# pip install docopt
import docopt
def main(argv=None):
params = docopt.docopt(__doc__, version="UNVERSIONED")
for line in sys.stdin.readlines():
sline = line.strip()
if not sline:
output(line)
continue
try:
d = dateutil.parser.parse(sline)
except:
output(line)
continue
if params["--local"]:
d = d.astimezone(tz=None)
elif params["--utc"]:
d = d.astimezone(datetime.timezone.utc)
if params["--iso"]:
output(d.isoformat())
elif params["--file"]:
output("{:%Y%m%dT%H%M%S%z}".format(d))
def output(s, *, fo=sys.stdout):
print(s, file=fo)
fo.flush()
if __name__ == "__main__":
try:
sys.exit(main())
except KeyboardInterrupt:
sys.stderr.write("\n")