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