simplify pcu bad logic
[monitor.git] / monitor / util / __init__.py
1 __all__=["writepid","removepid","daemonize"]
2
3 import time
4 import math
5
6 def diff_time(timestamp, abstime=True):
7         now = time.time()
8         if timestamp == None:
9                 return "unknown"
10         if abstime:
11                 diff = now - timestamp
12         else:
13                 diff = timestamp
14         # return the number of seconds as a difference from current time.
15         t_str = ""
16         if diff < 60: # sec in min.
17                 t = diff / 1
18                 t_str = "%s sec ago" % int(math.ceil(t))
19         elif diff < 60*60: # sec in hour
20                 t = diff / (60)
21                 t_str = "%s min ago" % int(math.ceil(t))
22         elif diff < 60*60*24: # sec in day
23                 t = diff / (60*60)
24                 t_str = "%s hrs ago" % int(math.ceil(t))
25         elif diff < 60*60*24*14: # sec in week
26                 t = diff / (60*60*24)
27                 t_str = "%s days ago" % int(math.ceil(t))
28         elif diff <= 60*60*24*30: # approx sec in month
29                 t = diff / (60*60*24)
30                 t_str = "%s days ago" % int(math.ceil(t))
31         elif diff > 60*60*24*30: # approx sec in month
32                 t = diff / (60*60*24*30)
33                 t_str = "%s mnths ago" % int(t)
34         return t_str