degal/format.py
changeset 56 80658a2eebf6
child 77 2a53c5ade434
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/degal/format.py	Fri Jun 05 19:29:04 2009 +0300
@@ -0,0 +1,32 @@
+"""
+    Miscellaneous formatting functions
+"""
+
+import datetime
+
+def filesize (bytes) :
+    """
+        Returns a human-readable string describing the given filesize
+
+        The returned string is either "x.xx MiB", "x.xx KiB", or "zzz Bytes", whereby x.xx is at least 0.1
+    """
+
+    kb, bytes   = divmod(bytes, 1024)
+    mb, kb      = divmod(kb, 1024)
+
+    if mb or kb > 1024 / 10.0 :
+        return "%f.2 MiB" % (mb + kb / 1024 + bytes / 1024**2)
+
+    elif kb or bytes > 1024 / 10.0 :
+        return "%f.2 KiB" % (kb + bytes / 1024)
+
+    else :
+        return "%d Bytes" % bytes
+
+def filetime (ts) :
+    """
+        Returns a human-readable string describing the given file timestamp
+    """
+
+    return datetime.fromtimestamp(ts).strftime("%Y-%m-%d %H:%M:%S")
+