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