forked from kytos/kronos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
69 lines (50 loc) · 2.09 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
66
67
68
69
"""Utility features for all backends."""
import re
from datetime import datetime
class BackendError(Exception):
"""Exception thrown when a non specific error occurs in backend."""
class NamespaceError(Exception):
"""Exception thrown when the provided namespace is not valid."""
class ValueConvertError(Exception):
"""Exception thrown when it is not possible convert the value to stored."""
def now():
"""Return timestamp in ISO-8601 format."""
return datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
def validate_timestamp(start, end):
"""Validate timestamp to avoid that end be smaller than start."""
if start is not None and end is not None:
start, end = str(start), str(end)
if start > end:
return False
return True
def convert_to_iso(timestamp):
"""Convert a value to ISO-8601 format."""
try:
timestamp = float(timestamp)
iso = '%Y-%m-%dT%H:%M:%SZ'
timestamp = datetime.utcfromtimestamp(timestamp).strftime(iso)
return timestamp
except ValueError:
error = f'Error: Timestamp value \'{timestamp}\' is not convertible'\
' to ISO-8601 format.'
raise ValueConvertError(error)
except OverflowError:
error = f'Error: Timestamp \'{timestamp}\' float value is too '\
'large to be used as datetime.'
raise ValueConvertError(error)
def iso_format_validation(timestamp):
"""Verify if a timestamp is in isoformat."""
if timestamp is None:
return False
first_part = "(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-"
second_part = "(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):"
third_part = "([0-5][0-9])(\\.[0-9]+)?(Z|[+-](?:2[0-3]|[01][0-9]):[0-5]"\
"[0-9])"
regex = first_part + second_part + third_part
regex_iso = r'^(-?' + regex + '?$'
regex_date = r'^([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))'
match_iso = re.compile(regex_iso).match
match_date = re.compile(regex_date).match
if not (match_iso(timestamp) or match_date(timestamp)):
return False
return True