get_timedelta_str

    TODO: rectify this function with get_unix_timedelta_str (unix_timedelta_str probably has better implementation)

    Returns:
        str: timedelta_str, formated time string

    CommandLine:
        python -m utool.util_time --test-get_posix_timedelta_str

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_time import *  # NOQA
        >>> import utool as ut
        >>> posixtime_list = [-13, 10.2, 10.2 ** 2, 10.2 ** 3, 10.2 ** 4, 10.2 ** 5, 10.2 ** 8, 60 * 60 * 60 * 24 * 7]
        >>> posixtime = posixtime_list[-1]
        >>> timedelta_str = [get_posix_timedelta_str(posixtime) for posixtime in posixtime_list]
        >>> result = ut.repr2(timedelta_str)
        >>> print(result)
        ['-00:00:13', '00:00:10', '00:01:44', '00:17:41', '03:00:24', '1 day', '193 weeks', '60 weeks']

    Timeit::
        import datetime
        # Seems like like timedelta is just faster. must be because it is builtin
        %timeit get_posix_timedelta_str(posixtime)
        %timeit str(datetime.timedelta(seconds=posixtime))

def get_posix_timedelta_str(posixtime, year=False, approx=True):
    """
    get_timedelta_str

    TODO: rectify this function with get_unix_timedelta_str (unix_timedelta_str probably has better implementation)

    Returns:
        str: timedelta_str, formated time string

    CommandLine:
        python -m utool.util_time --test-get_posix_timedelta_str

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_time import *  # NOQA
        >>> import utool as ut
        >>> posixtime_list = [-13, 10.2, 10.2 ** 2, 10.2 ** 3, 10.2 ** 4, 10.2 ** 5, 10.2 ** 8, 60 * 60 * 60 * 24 * 7]
        >>> posixtime = posixtime_list[-1]
        >>> timedelta_str = [get_posix_timedelta_str(posixtime) for posixtime in posixtime_list]
        >>> result = ut.repr2(timedelta_str)
        >>> print(result)
        ['-00:00:13', '00:00:10', '00:01:44', '00:17:41', '03:00:24', '1 day', '193 weeks', '60 weeks']

    Timeit::
        import datetime
        # Seems like like timedelta is just faster. must be because it is builtin
        %timeit get_posix_timedelta_str(posixtime)
        %timeit str(datetime.timedelta(seconds=posixtime))

    """
    import numpy as np
    if np.isnan(posixtime):
        return 'NaN'
    sign, posixtime_ = (1, posixtime) if posixtime >= 0 else (-1, -posixtime)
    seconds_, subseconds = divmod(posixtime_, 1)
    minutes_, seconds    = divmod(int(seconds_), 60)
    hours_, minutes      = divmod(minutes_, 60)
    days_, hours         = divmod(hours_, 24)
    weeks_, days         = divmod(days_, 7)
    if year:
        years, weeks = divmod(weeks_, 52)  # not accurate
    else:
        years = 0
        weeks = weeks_
    timedelta_parts = []
    if subseconds > 0:
        timedelta_parts += [('%.2f' % (subseconds,))[1:]]
    timedelta_parts += [':'.join(['%02d' % _ for _ in (hours, minutes, seconds)])]
    import utool as ut
    if days > 0:
        timedelta_parts += ['%d %s ' % (days, ut.pluralize('day', days))]
    if weeks > 0:
        timedelta_parts += ['%d %s ' % (weeks, ut.pluralize('week', weeks))]
    if years > 0:
        timedelta_parts += ['%d %s ' % (years, ut.pluralize('year', years))]
    if sign == -1:
        timedelta_parts += ['-']
    else:
        timedelta_parts += ['']
    if approx is not False:
        if approx is True:
            approx = 1
        timedelta_str = ''.join(timedelta_parts[::-1][0:(approx + 1)]).strip()
    else:
        timedelta_str = ''.join(timedelta_parts[::-1])
    return timedelta_str