"""
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 "%.2f MiB" % (mb + kb / 1024 + bytes / 1024**2)
elif kb or bytes > 1024 / 10.0 :
return "%.2f KiB" % (kb + bytes / 1024)
else :
return "%d Bytes" % bytes
def filetime (ts) :
"""
Returns a human-readable string describing the given file timestamp
"""
return datetime.datetime.fromtimestamp(ts).strftime("%Y-%m-%d %H:%M:%S")