checking.py 1.5 KB

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