]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/util/time_format.py
wui: improved columns in welcome page server list
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / util / time_format.py
1 # ISO-8601:
2 # http://www.cl.cam.ac.uk/~mgk25/iso-time.html
3
4 import calendar, datetime, re, time
5
6 def format_time(t):
7     return time.strftime("%Y-%m-%d %H:%M:%S", t)
8
9 def iso_utc_date(now=None, t=time.time):
10     if now is None:
11         now = t()
12     return datetime.datetime.utcfromtimestamp(now).isoformat()[:10]
13
14 def iso_utc(now=None, sep='_', t=time.time):
15     if now is None:
16         now = t()
17     return datetime.datetime.utcfromtimestamp(now).isoformat(sep)
18
19 def iso_utc_time_to_seconds(isotime, _conversion_re=re.compile(r"(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})[T_ ](?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2})(?P<subsecond>\.\d+)?")):
20     """
21     The inverse of iso_utc().
22
23     Real ISO-8601 is "2003-01-08T06:30:59".  We also accept the widely
24     used variants "2003-01-08_06:30:59" and "2003-01-08 06:30:59".
25     """
26     m = _conversion_re.match(isotime)
27     if not m:
28         raise ValueError, (isotime, "not a complete ISO8601 timestamp")
29     year, month, day = int(m.group('year')), int(m.group('month')), int(m.group('day'))
30     hour, minute, second = int(m.group('hour')), int(m.group('minute')), int(m.group('second'))
31     subsecstr = m.group('subsecond')
32     if subsecstr:
33         subsecfloat = float(subsecstr)
34     else:
35         subsecfloat = 0
36
37     return calendar.timegm( (year, month, day, hour, minute, second, 0, 1, 0) ) + subsecfloat
38
39 def parse_duration(s):
40     orig = s
41     unit = None
42     DAY = 24*60*60
43     MONTH = 31*DAY
44     YEAR = 365*DAY
45     if s.endswith("s"):
46         s = s[:-1]
47     if s.endswith("day"):
48         unit = DAY
49         s = s[:-len("day")]
50     elif s.endswith("month"):
51         unit = MONTH
52         s = s[:-len("month")]
53     elif s.endswith("mo"):
54         unit = MONTH
55         s = s[:-len("mo")]
56     elif s.endswith("year"):
57         unit = YEAR
58         s = s[:-len("YEAR")]
59     else:
60         raise ValueError("no unit (like day, month, or year) in '%s'" % orig)
61     s = s.strip()
62     return int(s) * unit
63
64 def parse_date(s):
65     # return seconds-since-epoch for the UTC midnight that starts the given
66     # day
67     return int(iso_utc_time_to_seconds(s + "T00:00:00"))
68
69 def format_delta(time_1, time_2):
70     if time_1 is None:
71         return "N/A"
72     if time_1 > time_2:
73         return '-'
74     delta = int(time_2 - time_1)
75     seconds = delta % 60
76     delta  -= seconds
77     minutes = (delta / 60) % 60
78     delta  -= minutes * 60
79     hours   = delta / (60*60) % 24
80     delta  -= hours * 24
81     days    = delta / (24*60*60)
82     if not days:
83         if not hours:
84             if not minutes:
85                 return "%ss" % (seconds)
86             else:
87                 return "%sm %ss" % (minutes, seconds)
88         else:
89             return "%sh %sm %ss" % (hours, minutes, seconds)
90     else:
91         return "%sd %sh %sm %ss" % (days, hours, minutes, seconds)
92