degal/format.py
author Tero Marttila <terom@fixme.fi>
Wed, 01 Jul 2009 20:40:00 +0300
changeset 141 9387da0dc183
parent 81 2b0b76cd8ba0
permissions -rw-r--r--
move .config from filesystem to gallery/folder/image, rename degal_dir to app_dir
"""
    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")