Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
derv82
GitHub Repository: derv82/wifite2
Path: blob/master/wifite/util/timer.py
412 views
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
import time
5
6
class Timer(object):
7
def __init__(self, seconds):
8
self.start_time = time.time()
9
self.end_time = self.start_time + seconds
10
11
def remaining(self):
12
return max(0, self.end_time - time.time())
13
14
def ended(self):
15
return self.remaining() == 0
16
17
def running_time(self):
18
return time.time() - self.start_time
19
20
def __str__(self):
21
''' Time remaining in minutes (if > 1) and seconds, e.g. 5m23s'''
22
return Timer.secs_to_str(self.remaining())
23
24
@staticmethod
25
def secs_to_str(seconds):
26
'''Human-readable seconds. 193 -> 3m13s'''
27
if seconds < 0:
28
return '-%ds' % seconds
29
30
rem = int(seconds)
31
hours = int(rem / 3600)
32
mins = int((rem % 3600) / 60)
33
secs = rem % 60
34
if hours > 0:
35
return '%dh%dm%ds' % (hours, mins, secs)
36
elif mins > 0:
37
return '%dm%ds' % (mins, secs)
38
else:
39
return '%ds' % secs
40
41