| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import TYPE_CHECKING |
| 4 |
|
| 5 |
from isoduration.formatter.exceptions import DurationFormattingException |
| 6 |
|
| 7 |
if TYPE_CHECKING: |
| 8 |
from isoduration.types import DateDuration, Duration |
| 9 |
|
| 10 |
|
| 11 |
def check_global_sign(duration: Duration) -> int: |
| 12 |
is_date_zero = ( |
| 13 |
duration.date.years == 0 |
| 14 |
and duration.date.months == 0 |
| 15 |
and duration.date.days == 0 |
| 16 |
and duration.date.weeks == 0 |
| 17 |
) |
| 18 |
is_time_zero = ( |
| 19 |
duration.time.hours == 0 |
| 20 |
and duration.time.minutes == 0 |
| 21 |
and duration.time.seconds == 0 |
| 22 |
) |
| 23 |
|
| 24 |
is_date_negative = ( |
| 25 |
duration.date.years <= 0 |
| 26 |
and duration.date.months <= 0 |
| 27 |
and duration.date.days <= 0 |
| 28 |
and duration.date.weeks <= 0 |
| 29 |
) |
| 30 |
is_time_negative = ( |
| 31 |
duration.time.hours <= 0 |
| 32 |
and duration.time.minutes <= 0 |
| 33 |
and duration.time.seconds <= 0 |
| 34 |
) |
| 35 |
|
| 36 |
if not is_date_zero and not is_time_zero: |
| 37 |
if is_date_negative and is_time_negative: |
| 38 |
return -1 |
| 39 |
elif not is_date_zero: |
| 40 |
if is_date_negative: |
| 41 |
return -1 |
| 42 |
elif not is_time_zero: |
| 43 |
if is_time_negative: |
| 44 |
return -1 |
| 45 |
|
| 46 |
return +1 |
| 47 |
|
| 48 |
|
| 49 |
def validate_date_duration(date_duration: DateDuration) -> None: |
| 50 |
if date_duration.weeks: |
| 51 |
if date_duration.years or date_duration.months or date_duration.days: |
| 52 |
raise DurationFormattingException( |
| 53 |
"Weeks are incompatible with other date designators" |
| 54 |
) |