-
Notifications
You must be signed in to change notification settings - Fork 0
/
datecalc.py
150 lines (118 loc) · 3.68 KB
/
datecalc.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from datetime import date, timedelta
import sys
def make_token(s):
if s[0] in "0123456789":
return ("NumberToken", s)
elif s[0] in "+":
return ("OperatorToken", s[0])
else:
return ("WordToken", s)
def lex(chars):
if len(chars) == 0:
return []
else:
return [make_token(s) for s in chars.split(" ")]
def parse(tokens, so_far=None):
if len(tokens) == 0:
return so_far
tok = tokens[0]
other_toks = tokens[1:]
if tok[0] == "NumberToken":
next_tok = tokens[1]
return ("LengthTree", tok[1], next_tok[1])
elif tok[0] == "OperatorToken":
return ("OperatorTree", tok[1], so_far, parse(other_toks))
else: # Must be WordToken
return parse(other_toks, ("WordTree", tok[1]))
def length_tree_in_days(length_tree):
number = int(length_tree[1])
unit = length_tree[2]
if unit in ("weeks", "week"):
return number * 7
elif unit in ("days", "day"):
return number
else:
raise Exception("Unknown time unit '%s'." % unit)
def evaluate(tree):
if tree[0] == "LengthTree":
return ("LengthValue", length_tree_in_days(tree))
elif tree[0] == "OperatorTree":
left = evaluate(tree[2])
right = evaluate(tree[3])
return ("DateValue", left[1] + timedelta(days=right[1]))
elif tree[0] == "WordTree":
if tree[1] == "today":
return ("DateValue", date.today())
elif tree[1] == "tomorrow":
return ("DateValue", date.today() + timedelta(days=1))
elif tree[1] == "yesterday":
return ("DateValue", date.today() - timedelta(days=1))
else:
raise Exception("Unknown word '%s'." % tree[1])
else:
raise Exception("Unknown tree type '%s'." % tree[0])
def pretty(value):
if value[0] == "DateValue":
return value[1].strftime("%Y-%m-%d (%A)")
else:
return "%s days" % value[1]
assert lex("today") == [("WordToken", "today")]
assert lex("tomorrow") == [("WordToken", "tomorrow")]
assert lex("2 days") == [("NumberToken", "2"), ("WordToken", "days")]
assert lex("3 weeks") == [("NumberToken", "3"), ("WordToken", "weeks")]
assert (
lex("today + 3 days") ==
[
("WordToken", "today"),
("OperatorToken", "+"),
("NumberToken", "3"),
("WordToken", "days")
]
)
def p(x):
return parse(lex(x))
assert p("today") == ("WordTree", "today")
assert p("tomorrow") == ("WordTree", "tomorrow")
assert p("2 days") == ("LengthTree", "2", "days")
assert p("3 weeks") == ("LengthTree", "3", "weeks")
assert (
p("today + 3 days") ==
("OperatorTree",
"+",
("WordTree", "today"),
("LengthTree", "3", "days")
)
)
def e(x):
return evaluate(parse(lex(x)))
def days(n):
return timedelta(days=n)
today = date.today()
assert e("today") == ("DateValue", today)
assert e("tomorrow") == ("DateValue", today + days(1))
assert e("yesterday") == ("DateValue", today - days(1))
assert e("2 days") == ("LengthValue", 2)
assert e("3 weeks") == ("LengthValue", 21)
assert e("today + 3 days") == ("DateValue", today + days(3))
assert e("tomorrow + 1 day") == ("DateValue", today + days(2))
try:
e("banana")
exc = False
except:
exc = True
assert exc
try:
e("1 banana")
exc = False
except:
exc = True
assert exc
assert pretty(("DateValue", date(2017, 5, 23))).startswith("2017-05-23 (")
assert pretty(evaluate(parse(lex("2 weeks")))) == "14 days"
for arg in sys.argv[1:]:
print(pretty(evaluate(parse(lex(arg)))))
while True:
ln = sys.stdin.readline()
if ln is None or ln.strip() == "":
break
sys.stdout.write("%s\n" % pretty(evaluate(parse(lex(ln.strip())))))