import time


def format_time_delta(cgxtimestamp):
    """
    Format a CloudGenix timestamp into Human Readable string.
    :param cgxtimestamp: Timestamp from CloudGenix API
    :return:
    """
    # cgxtimestamp may be milliseconds or nanoseconds. Drop the digits after the first 10.
    # this should work until 2264 or so :D
    cgx_seconds = int(str(cgxtimestamp)[:10])
    now_seconds = int(time.time())
    seconds = now_seconds - cgx_seconds
    # sanity check clock sku
    if seconds < 0:
        return "<=0s (client clock skew)"
    days, seconds = divmod(seconds, 86400)
    hours, seconds = divmod(seconds, 3600)
    minutes, seconds = divmod(seconds, 60)
    if days > 1:
        return f"{days}d{hours}h{minutes}m{seconds}s"
    elif hours > 0:
        return f"{hours}h{minutes}m{seconds}s"
    elif minutes > 0:
        return f"{minutes}m{seconds}s"
    else:
        return f"{seconds}s"