Pushing Daisies Age

python
Author

Myles Braithwaite

Published

October 12, 2021

Prototyped a Python function that would humanize the distance between two datetime objects as they do in the TV show Pushing Daisies.

import datetime
from operator import sub
from dateutil.relativedelta import relativedelta
def been_alive_for(
    birthday: datetime.datetime,
    as_of: datetime.datetime = None,
):
    """Pushing Daisies been alive for."""
    if as_of is None:
        as_of = datetime.datetime.now()

    delta = relativedelta(as_of, birthday)

    if delta.months:
        days = 30 * delta.months
    else:
        days = delta.days

    data = {
        "years": delta.years,
        "weeks": days / 7,
    }

    data["days"] = days - (data["weeks"] * 7)

    if data["weeks"] < 1:
        data["days"] = days
        data["weeks"] = None
    else:
        week_remainder = data["weeks"] - round(data["weeks"], 0)
        data["weeks"] = round(data["weeks"], 0)
        data["days"] = data["days"] + (week_remainder * 7)

    if data["days"] < 0:
        data["weeks"] = data["weeks"] - 1
        data["days"] = 7 + data["days"]

    data["hours"] = delta.hours
    data["minutes"] = delta.minutes

    display = []

    for key, value in data.items():
        if value:
            display.append(("{0} {1}".format(round(value), key)))

    return ", ".join(display)
myles_alive_for = been_alive_for(
    birthday=datetime.datetime(1986, 9, 19, 8, 22),
)
print(myles_alive_for)
36 years, 21 weeks, 3 days, 6 hours, 34 minutes

Made by Myles Braithwaite with ❤️ in Toronto.