| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import TYPE_CHECKING |
| 4 |
|
| 5 |
from isoduration.constants import PERIOD_PREFIX |
| 6 |
from isoduration.formatter.checking import check_global_sign |
| 7 |
from isoduration.formatter.formatting import format_date, format_time |
| 8 |
|
| 9 |
if TYPE_CHECKING: |
| 10 |
from isoduration.types import Duration |
| 11 |
|
| 12 |
|
| 13 |
def format_duration(duration: Duration) -> str: |
| 14 |
global_sign = check_global_sign(duration) |
| 15 |
date_duration_str = format_date(duration.date, global_sign) |
| 16 |
time_duration_str = format_time(duration.time, global_sign) |
| 17 |
|
| 18 |
duration_str = f"{date_duration_str}{time_duration_str}" |
| 19 |
sign_str = "-" if global_sign < 0 else "" |
| 20 |
|
| 21 |
if duration_str == PERIOD_PREFIX: |
| 22 |
return f"{PERIOD_PREFIX}0D" |
| 23 |
|
| 24 |
return f"{sign_str}{duration_str}" |