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