3 from datetime import timedelta
11 def abbreviate_time(s):
13 if isinstance(s, timedelta):
18 postfix = ' in the future'
19 def _plural(count, unit):
22 return "%d %s%s" % (count, unit, postfix)
23 return "%d %ss%s" % (count, unit, postfix)
27 return _plural(s, "second")
29 return _plural(s/60, "minute")
31 return _plural(s/HOUR, "hour")
33 return _plural(s/DAY, "day")
35 return _plural(s/MONTH, "month")
36 return _plural(s/YEAR, "year")
38 def abbreviate_space(s, SI=True):
48 return "%.2f %s%s" % (count, suffix, isuffix)
50 if s < 1024: # 1000-1023 get emitted as bytes, even in SI mode
55 return r(s/(U*U), "M")
57 return r(s/(U*U*U), "G")
59 return r(s/(U*U*U*U), "T")
61 return r(s/(U*U*U*U*U), "P")
62 return r(s/(U*U*U*U*U*U), "E")
64 def abbreviate_space_both(s):
65 return "(%s, %s)" % (abbreviate_space(s, True),
66 abbreviate_space(s, False))
68 def parse_abbreviated_size(s):
69 if s is None or s == "":
71 m = re.match(r"^(\d+)([KMGTPE]?[I]?[B]?)$", s.upper())
73 raise ValueError("unparseable value %s" % s)
74 number, suffix = m.groups()
75 if suffix.endswith("B"):
81 "G": 1000 * 1000 * 1000,
82 "T": 1000 * 1000 * 1000 * 1000,
83 "P": 1000 * 1000 * 1000 * 1000 * 1000,
84 "E": 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
87 "GI": 1024 * 1024 * 1024,
88 "TI": 1024 * 1024 * 1024 * 1024,
89 "PI": 1024 * 1024 * 1024 * 1024 * 1024,
90 "EI": 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
92 return int(number) * multiplier