from datetime import datetime


def pretty_date(time):
    """
    Get a datetime object or a int() Epoch timestamp and return a
    pretty string like 'an hour ago', 'Yesterday', '3 months ago',
    'just now', etc

    Based on https://stackoverflow.com/a/1551394/713980
    Adapted by sven1103
    """
    now = datetime.now()
    if isinstance(time, datetime):
        diff = now - time
    else:
        diff = now - datetime.fromtimestamp(time)
    second_diff = diff.seconds
    day_diff = diff.days

    pretty_msg = {
        0: [(float('inf'), 1, 'from the future')],
        1: [
            (10, 1, "just now"),
            (60, 1, "{sec} seconds ago"),
            (120, 1, "a minute ago"),
            (3600, 60, "{sec} minutes ago"),
            (7200, 1, "an hour ago"),
            (86400, 3600, "{sec} hours ago")
        ],
        2: [(float('inf'), 1, 'yesterday')],
        7: [(float('inf'), 1, '{days} days ago')],
        31: [(float('inf'), 7, '{days} weeks ago')],
        365: [(float('inf'), 30, '{days} months ago')],
        float('inf'): [(float('inf'), 365, '{days} years ago')]
    }

    for days, seconds in pretty_msg.items():
        if day_diff < days:
            for sec in seconds:
                if second_diff < sec[0]:
                    return sec[2].format(days=round(day_diff/sec[1], 1),
                        sec=round(second_diff/sec[1], 1))
    return '... time is relative anyway'