]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blobdiff - 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
index 98d32e491a0e4adbaec156a0da0d250655bdbfa0..0820bd8b01f23895970137ec03aa773240778919 100644 (file)
@@ -1,20 +1,24 @@
-#  Copyright (c) 2001 Autonomous Zone Industries
-#  Copyright (c) 2002-2007 Bryce "Zooko" Wilcox-O'Hearn
-#  This file is licensed under the
-#    GNU Lesser General Public License v2.1.
-#    See the file COPYING or visit http://www.gnu.org/ for details.
-
 # ISO-8601:
 # http://www.cl.cam.ac.uk/~mgk25/iso-time.html
 
-import datetime, re, time
+import calendar, datetime, re, time
+
+def iso_utc_date(now=None, t=time.time):
+    if now is None:
+        now = t()
+    return datetime.datetime.utcfromtimestamp(now).isoformat()[:10]
 
 def iso_utc(now=None, sep='_', t=time.time):
     if now is None:
         now = t()
     return datetime.datetime.utcfromtimestamp(now).isoformat(sep)
 
-def iso_utc_time_to_localseconds(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+)?")):
+def iso_local(now=None, sep='_', t=time.time):
+    if now is None:
+        now = t()
+    return datetime.datetime.fromtimestamp(now).isoformat(sep)
+
+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+)?")):
     """
     The inverse of iso_utc().
 
@@ -26,11 +30,66 @@ def iso_utc_time_to_localseconds(isotime, _conversion_re=re.compile(r"(?P<year>\
         raise ValueError, (isotime, "not a complete ISO8601 timestamp")
     year, month, day = int(m.group('year')), int(m.group('month')), int(m.group('day'))
     hour, minute, second = int(m.group('hour')), int(m.group('minute')), int(m.group('second'))
-    utcseconds = time.mktime( (year, month, day, hour, minute, second, 0, 1, 0) )
-    localseconds = utcseconds - time.timezone
     subsecstr = m.group('subsecond')
     if subsecstr:
         subsecfloat = float(subsecstr)
-        localseconds += subsecfloat
-    return localseconds
+    else:
+        subsecfloat = 0
+
+    return calendar.timegm( (year, month, day, hour, minute, second, 0, 1, 0) ) + subsecfloat
+
+def parse_duration(s):
+    orig = s
+    unit = None
+    DAY = 24*60*60
+    MONTH = 31*DAY
+    YEAR = 365*DAY
+    if s.endswith("s"):
+        s = s[:-1]
+    if s.endswith("day"):
+        unit = DAY
+        s = s[:-len("day")]
+    elif s.endswith("month"):
+        unit = MONTH
+        s = s[:-len("month")]
+    elif s.endswith("mo"):
+        unit = MONTH
+        s = s[:-len("mo")]
+    elif s.endswith("year"):
+        unit = YEAR
+        s = s[:-len("YEAR")]
+    else:
+        raise ValueError("no unit (like day, month, or year) in '%s'" % orig)
+    s = s.strip()
+    return int(s) * unit
+
+def parse_date(s):
+    # return seconds-since-epoch for the UTC midnight that starts the given
+    # day
+    return int(iso_utc_time_to_seconds(s + "T00:00:00"))
 
+def format_delta(time_1, time_2):
+    TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
+    if time_1 is None:
+        absolute_str, relative_str = "N/A", "N/A"
+    else:
+        delta = int( time_2 - time_1 )
+        seconds = delta % 60
+        delta  -= seconds
+        minutes = (delta / 60) % 60
+        delta  -= minutes * 60
+        hours   = delta / (60*60) % 24
+        delta  -=  hours * 24
+        days    = delta / (24*60*60)
+        if not days:
+            if not hours:
+                if not minutes:
+                    relative_str = "%ss" % (seconds)
+                else:
+                    relative_str = "%sm%ss" % (minutes, seconds)
+            else:
+                relative_str = "%sh%sm%ss" % (hours, minutes, seconds)
+        else:
+            relative_str = "%sd%sh%sm%ss" % (days, hours, minutes, seconds)
+        absolute_str = time.strftime(TIME_FORMAT, time.localtime(time_1))
+    return absolute_str, relative_str