forked from elastic/rally
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.py
107 lines (80 loc) · 2.94 KB
/
time.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
import time
from datetime import datetime
def to_epoch_millis(t):
"""
Convert a time instance retrieved via time.time() to a timestamp in milliseconds since epoch.
:param t: a time instance
:return: the corresponding value of milliseconds since epoch.
"""
return int(round(t * 1000))
def to_iso8601(dt):
"""
Convert a datetime instance to a ISO-8601 compliant string.
:param dt: A datetime instance
:return: The corresponding ISO-8601 formatted string
"""
return "%04d%02d%02dT%02d%02d%02dZ" % (dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second)
def from_is8601(ts):
"""
Convert an ISO-8601 compliant string (as created by ``to_iso8601(dt)``) to a datetime instance.
:param ts: an ISO-8601 compliant string
:return: The corresponding datetime instance.
"""
return datetime.strptime(ts, "%Y%m%dT%H%M%SZ")
def sleep(seconds):
time.sleep(seconds)
def _to_datetime(val, date_format=None):
if isinstance(val, datetime):
return val
# unix timestamp
elif isinstance(val, float):
return datetime.fromtimestamp(val)
elif isinstance(val, str):
return datetime.strptime(val, date_format)
else:
raise TypeError("Cannot convert unrecognized type '%s' with value '%s' to datetime." % (type(val), str(val)))
def days_ago(start_date, end_date, date_format="%d-%m-%Y"):
"""
Calculates the difference in days between a start date and an end date.
:param start_date: The start date. May be a datetime instance, a unix timestamp or a string representation of a date.
:param end_date: The end date. May be a datetime instance, a unix timestamp or a string representation of a date.
:param date_format: If one or both date values are provided as strings, the date format which is needed for conversion.
Default: "%d-%m-%Y"
:return: The difference between start and end date in complete days.
"""
start = _to_datetime(start_date, date_format)
end = _to_datetime(end_date, date_format)
return (end - start).days
class Clock:
"""
A clock abstracts time measurements. Its main purpose is to ease testing
"""
@staticmethod
def now():
"""
:return: The current time.
"""
return time.time()
@staticmethod
def stop_watch():
return StopWatch()
class StopWatch:
def __init__(self):
self._start = None
self._stop = None
def start(self):
self._start = self._now()
def stop(self):
self._stop = self._now()
def split_time(self):
return self._interval(self._start, self._now())
def total_time(self):
return self._interval(self._start, self._stop)
def _interval(self, t0, t1):
if t0 is None:
raise RuntimeError("start time is None")
if t1 is None:
raise RuntimeError("end time is None")
return t1 - t0
def _now(self):
return time.perf_counter()