def timedelta_to_duration_str(td):
    """Convert a timedelta object into a duration string e.g: PT<hhhh>H<mm>M."""
    minutes, seconds = divmod(td.seconds, 60)
    hours, minutes = divmod(minutes, 60)
    hours += td.days * 24
    hours = int(hours)
    assert seconds == 0
    return f"PT{hours:04}H{minutes:02}M"