-
Notifications
You must be signed in to change notification settings - Fork 227
/
utils.py
65 lines (52 loc) · 2.04 KB
/
utils.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
from datetime import datetime, timezone
import webbrowser
TEXT_BASED_BROWSERS = [webbrowser.GenericBrowser, webbrowser.Elinks]
def isTextBasedBrowser(browser):
"""Returns if browser is a text-based browser.
Arguments:
browser {webbrowser.BaseBrowser} -- A browser.
Returns:
bool -- True if browser is text-based, False if browser is not
text-based.
"""
for tb_browser in TEXT_BASED_BROWSERS:
if type(browser) is tb_browser:
return True
return False
def parseTimestamp(timestamp: str) -> datetime:
try:
return datetime.fromtimestamp(int(timestamp) / 1000, tzinfo=timezone.utc)
except ValueError:
# timestamp like '2012-07-30T17:47:01.687Z' or '2012-08-03T03:53:44Z'
return datetime.fromisoformat(timestamp.replace("Z", "+00:00"))
def timestampInRange(timestamp: str, date_range) -> bool:
"""Returns if the timestamp is in the date range.
Arguments:
timestamp {str} -- A timestamp (in ms).
date_range {tuple} -- A tuple of strings representing the date range.
(min_date, max_date) (Date format: yyyy-mm-dd)
"""
if date_range == (None, None):
return True
date_str = parseTimestamp(timestamp).strftime("%Y-%m-%d")
return dateInRange(date_str, date_range)
def dateInRange(date, date_range) -> bool:
"""Returns if the date is in the date range.
Arguments:
date {str} -- A date (Format: yyyy-mm-dd).
date_range {tuple} -- A tuple of strings representing the date range.
(min_date, max_date) (Date format: yyyy-mm-dd)
"""
if date_range == (None, None):
return True
if date_range[0] == None:
min_date = None
else:
min_date = datetime.strptime(date_range[0], "%Y-%m-%d")
if date_range[1] == None:
max_date = None
else:
max_date = datetime.strptime(date_range[1], "%Y-%m-%d")
date = datetime.strptime(date, "%Y-%m-%d")
return (min_date is None or min_date <= date) and \
(max_date is None or max_date >= date)