default.py 907 B

12345678910111213141516171819202122232425262728293031323334353637
  1. from __future__ import annotations
  2. from pendulum.formatting import Formatter
  3. _formatter = Formatter()
  4. class FormattableMixin:
  5. _formatter: Formatter = _formatter
  6. def format(self, fmt: str, locale: str | None = None) -> str:
  7. """
  8. Formats the instance using the given format.
  9. :param fmt: The format to use
  10. :param locale: The locale to use
  11. """
  12. return self._formatter.format(self, fmt, locale)
  13. def for_json(self) -> str:
  14. """
  15. Methods for automatic json serialization by simplejson.
  16. """
  17. return self.isoformat()
  18. def __format__(self, format_spec: str) -> str:
  19. if len(format_spec) > 0:
  20. if "%" in format_spec:
  21. return self.strftime(format_spec)
  22. return self.format(format_spec)
  23. return str(self)
  24. def __str__(self) -> str:
  25. return self.isoformat()