-
Notifications
You must be signed in to change notification settings - Fork 1
/
time.py
47 lines (33 loc) · 1.4 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
import time
from datetime import datetime
import pytz
class Timer:
""" Callable class to easily time things and print. """
def __init__(self, start_time=None):
""" Returns a started Timer instance.
:param float start_time: Defaults to time in seconds since epoch (time.time()) """
self.start_time = self.reset(start_time=start_time)
def reset(self, start_time=None):
""" Reset and start timer. """
if start_time is None:
start_time = time.time()
self.start_time = start_time
return start_time
def seconds(self):
""" Get seconds passed since timer started or was reset. """
return time.time() - self.start_time
def print(self, reset=False):
""" Print seconds passed. """
print(f"Seconds passed: {self.seconds()}")
if reset:
self.reset()
def sleep(seconds):
""" Normal sleep function from time package.
:param float seconds: Time in seconds to sleep. """
time.sleep(seconds)
def current_datetime(timezone="Europe/Paris"):
""" Get current aware datetime. """
return datetime.utcnow().replace(tzinfo=pytz.utc).astimezone(pytz.timezone(timezone))
def current_datetime_formatted(timezone="Europe/Paris", format_str="%Y-%m-%d %H:%M %Z"):
""" Get a nicely formatted date and time string. """
return current_datetime(timezone=timezone).strftime(format_str)