-
Notifications
You must be signed in to change notification settings - Fork 0
/
days.py
executable file
·54 lines (38 loc) · 998 Bytes
/
days.py
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
#!/usr/bin/env python
import sys
import dateparser
import pathlib
from datetime import datetime
dateArg = 'DATE|now|today|tomorrow|...'
cmd = pathlib.PurePath(sys.argv[0]).name
def error(msg):
print(msg)
printHelp()
exit(1)
def printHelp():
print(f'Usage: {cmd} [{dateArg}] until {dateArg}')
print( f"""
Examples:
{cmd} now until dec 25
{cmd} January 1 1970 until now
{cmd} until may 23 2030
""")
until = 'until '
cmdLine = " ".join(sys.argv[1:])
if cmdLine in ['--help', '-h', 'help']:
printHelp()
exit(0)
fromDate = datetime.now()
untilIdx = cmdLine.find(until)
if untilIdx == -1:
error("Invalid syntax")
if untilIdx > 0:
if not cmdLine[untilIdx - 1].isspace():
error("Invalid syntax")
fromDate = dateparser.parse(cmdLine[:untilIdx])
if not fromDate:
error("Invalid start date")
toDate = dateparser.parse(cmdLine[untilIdx + len(until):])
if not toDate:
error("Invalid end date")
print(toDate - fromDate)