]> 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 926d7179815a9108cab21149b876b49264ffe714..a6fe3ec72c45ba21e4220bf1eea5d7680e48a29b 100644 (file)
@@ -1,13 +1,10 @@
-#  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 format_time(t):
+    return time.strftime("%Y-%m-%d %H:%M:%S", t)
 
 def iso_utc_date(now=None, t=time.time):
     if now is None:
@@ -37,10 +34,7 @@ def iso_utc_time_to_seconds(isotime, _conversion_re=re.compile(r"(?P<year>\d{4})
     else:
         subsecfloat = 0
 
-    localsecondsnodst = time.mktime( (year, month, day, hour, minute, second, 0, 1, 0) )
-    localsecondsnodst += subsecfloat
-    utcseconds = localsecondsnodst - time.timezone
-    return utcseconds
+    return calendar.timegm( (year, month, day, hour, minute, second, 0, 1, 0) ) + subsecfloat
 
 def parse_duration(s):
     orig = s
@@ -72,3 +66,27 @@ def parse_date(s):
     # day
     return int(iso_utc_time_to_seconds(s + "T00:00:00"))
 
+def format_delta(time_1, time_2):
+    if time_1 is None:
+        return "N/A"
+    if time_1 > time_2:
+        return '-'
+    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:
+                return "%ss" % (seconds)
+            else:
+                return "%sm %ss" % (minutes, seconds)
+        else:
+            return "%sh %sm %ss" % (hours, minutes, seconds)
+    else:
+        return "%sd %sh %sm %ss" % (days, hours, minutes, seconds)
+