-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathparsedates.py
63 lines (47 loc) · 1.64 KB
/
parsedates.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
from contextlib import suppress
from datetime import datetime
import dateparser
from flask import Flask, request
app = Flask(__name__)
defaults = {"PREFER_DATES_FROM": "future"}
def is_timezone_aware(dt: datetime | None) -> bool:
"""Checks if dt is a timezone-aware datetime object"""
return dt and dt.tzinfo and dt.tzinfo.utcoffset(dt)
@app.route("/", methods=["POST"])
def hello_world():
req = request.json
text, timezone = req["text"], req["timezone"]
dt = None
if timezone:
try:
dt = dateparser.parse(
text, settings={**defaults, "TIMEZONE": timezone, "RETURN_AS_TIMEZONE_AWARE": True}
)
except:
return {"error": "Invalid Timezone"}
else:
with suppress(Exception):
dt = dateparser.parse(text, settings=defaults)
if not is_timezone_aware(dt):
dt = dateparser.parse(
text,
settings={
**defaults,
"TIMEZONE": req["default_timezone"],
"RETURN_AS_TIMEZONE_AWARE": True,
},
)
if not is_timezone_aware(dt):
dt = dateparser.parse(
text, settings={**defaults, "TIMEZONE": "UTC", "RETURN_AS_TIMEZONE_AWARE": True}
)
print(repr(dt))
if not dt:
return {"error": "Unable to parse given datetime"}
return {
"timestamp": int(dt.timestamp()),
"timezone": str(dt.tzinfo.tzname(dt)),
"utcoffset": int(dt.tzinfo.utcoffset(dt).total_seconds()),
}
if __name__ == "__main__":
app.run(host="unix:///tmp/parsedateserver.sock")