Files
Finance-Control/app/utils/formatters.py

26 lines
856 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from datetime import date, datetime
def format_money(value: float | int | None) -> str:
"""Return money value in a readable Russian format."""
amount = 0 if value is None else float(value)
return f"{amount:,.2f}".replace(",", " ")
def current_month_key() -> str:
"""Return current month in SQLite-friendly format YYYY-MM."""
return date.today().strftime("%Y-%m")
def today_text() -> str:
"""Return current date in ISO format for entry fields."""
return date.today().strftime("%Y-%m-%d")
def validate_date(value: str) -> str:
"""Validate user date and return normalized ISO date."""
try:
return datetime.strptime(value.strip(), "%Y-%m-%d").strftime("%Y-%m-%d")
except ValueError as error:
raise ValueError("Дата должна быть в формате ГГГГ-ММ-ДД.") from error