You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.5 KiB
Python
55 lines
1.5 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
from isoduration.formatter.exceptions import DurationFormattingException
|
|
|
|
if TYPE_CHECKING: # pragma: no cover
|
|
from isoduration.types import DateDuration, Duration
|
|
|
|
|
|
def check_global_sign(duration: Duration) -> int:
|
|
is_date_zero = (
|
|
duration.date.years == 0
|
|
and duration.date.months == 0
|
|
and duration.date.days == 0
|
|
and duration.date.weeks == 0
|
|
)
|
|
is_time_zero = (
|
|
duration.time.hours == 0
|
|
and duration.time.minutes == 0
|
|
and duration.time.seconds == 0
|
|
)
|
|
|
|
is_date_negative = (
|
|
duration.date.years <= 0
|
|
and duration.date.months <= 0
|
|
and duration.date.days <= 0
|
|
and duration.date.weeks <= 0
|
|
)
|
|
is_time_negative = (
|
|
duration.time.hours <= 0
|
|
and duration.time.minutes <= 0
|
|
and duration.time.seconds <= 0
|
|
)
|
|
|
|
if not is_date_zero and not is_time_zero:
|
|
if is_date_negative and is_time_negative:
|
|
return -1
|
|
elif not is_date_zero:
|
|
if is_date_negative:
|
|
return -1
|
|
elif not is_time_zero:
|
|
if is_time_negative:
|
|
return -1
|
|
|
|
return +1
|
|
|
|
|
|
def validate_date_duration(date_duration: DateDuration) -> None:
|
|
if date_duration.weeks:
|
|
if date_duration.years or date_duration.months or date_duration.days:
|
|
raise DurationFormattingException(
|
|
"Weeks are incompatible with other date designators"
|
|
)
|