]> 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_local(now=None, sep='_', t=time.time):
20     if now is None:
21         now = t()
22     return datetime.datetime.fromtimestamp(now).isoformat(sep)
23
24 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+)?")):
25     """
26     The inverse of iso_utc().
27
28     Real ISO-8601 is "2003-01-08T06:30:59".  We also accept the widely
29     used variants "2003-01-08_06:30:59" and "2003-01-08 06:30:59".
30     """
31     m = _conversion_re.match(isotime)
32     if not m:
33         raise ValueError, (isotime, "not a complete ISO8601 timestamp")
34     year, month, day = int(m.group('year')), int(m.group('month')), int(m.group('day'))
35     hour, minute, second = int(m.group('hour')), int(m.group('minute')), int(m.group('second'))
36     subsecstr = m.group('subsecond')
37     if subsecstr:
38         subsecfloat = float(subsecstr)
39     else:
40         subsecfloat = 0
41
42     return calendar.timegm( (year, month, day, hour, minute, second, 0, 1, 0) ) + subsecfloat
43
44 def parse_duration(s):
45     orig = s
46     unit = None
47     DAY = 24*60*60
48     MONTH = 31*DAY
49     YEAR = 365*DAY
50     if s.endswith("s"):
51         s = s[:-1]
52     if s.endswith("day"):
53         unit = DAY
54         s = s[:-len("day")]
55     elif s.endswith("month"):
56         unit = MONTH
57         s = s[:-len("month")]
58     elif s.endswith("mo"):
59         unit = MONTH
60         s = s[:-len("mo")]
61     elif s.endswith("year"):
62         unit = YEAR
63         s = s[:-len("YEAR")]
64     else:
65         raise ValueError("no unit (like day, month, or year) in '%s'" % orig)
66     s = s.strip()
67     return int(s) * unit
68
69 def parse_date(s):
70     # return seconds-since-epoch for the UTC midnight that starts the given
71     # day
72     return int(iso_utc_time_to_seconds(s + "T00:00:00"))
73
74 def format_delta(time_1, time_2):
75     if time_1 is None:
76         return "N/A"
77     if time_1 > time_2:
78         return '-'
79     delta = int(time_2 - time_1)
80     seconds = delta % 60
81     delta  -= seconds
82     minutes = (delta / 60) % 60
83     delta  -= minutes * 60
84     hours   = delta / (60*60) % 24
85     delta  -= hours * 24
86     days    = delta / (24*60*60)
87     if not days:
88         if not hours:
89             if not minutes:
90                 return "%ss" % (seconds)
91             else:
92                 return "%sm %ss" % (minutes, seconds)
93         else:
94             return "%sh %sm %ss" % (hours, minutes, seconds)
95     else:
96         return "%sd %sh %sm %ss" % (days, hours, minutes, seconds)
97