from datetime import datetime


def datetime_format(date):
    """
    Takes a datetime object instance (e.g. from django's DateTimeField)
    and returns a string describing how long ago that date was.
    """
    year, month, day = date.year, date.month, date.day
    hour, minute, second = date.hour, date.minute, date.second
    now = datetime.now()

    if year < now.year:
        # another year
        timestring = str(date.date())
    elif date.date() < now.date():
        # another date, same year
        # put month before day because of FREEDOM
        timestring = "%02i-%02i %02i:%02i" % (month, day, hour, minute)
    elif hour < now.hour - 1:
        # same day, more than 1 hour ago
        timestring = "%02i:%02i" % (hour, minute)
    else:
        # same day, less than 1 hour ago
        timestring = "%02i:%02i:%02i" % (hour, minute, second)
    return timestring