terom@56: """ terom@56: Miscellaneous formatting functions terom@56: """ terom@56: terom@56: import datetime terom@56: terom@56: def filesize (bytes) : terom@56: """ terom@56: Returns a human-readable string describing the given filesize terom@56: terom@56: The returned string is either "x.xx MiB", "x.xx KiB", or "zzz Bytes", whereby x.xx is at least 0.1 terom@56: """ terom@56: terom@56: kb, bytes = divmod(bytes, 1024) terom@56: mb, kb = divmod(kb, 1024) terom@56: terom@56: if mb or kb > 1024 / 10.0 : terom@81: return "%.2f MiB" % (mb + kb / 1024 + bytes / 1024**2) terom@56: terom@56: elif kb or bytes > 1024 / 10.0 : terom@81: return "%.2f KiB" % (kb + bytes / 1024) terom@56: terom@56: else : terom@56: return "%d Bytes" % bytes terom@56: terom@56: def filetime (ts) : terom@56: """ terom@56: Returns a human-readable string describing the given file timestamp terom@56: """ terom@56: terom@77: return datetime.datetime.fromtimestamp(ts).strftime("%Y-%m-%d %H:%M:%S") terom@56: