diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-12-17 14:32:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-12-17 14:32:20 +0000 |
commit | db51f7f103bbbd6c91c8f47d75b3482ef8939691 (patch) | |
tree | ab59b1147bd0cd39f31a48073cff236ede4ec1df /src/pendulum/mixins | |
parent | Adding upstream version 3.0.0~a1. (diff) | |
download | pendulum-upstream.tar.xz pendulum-upstream.zip |
Adding upstream version 3.0.0.upstream/3.0.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/pendulum/mixins')
-rw-r--r-- | src/pendulum/mixins/__init__.py | 0 | ||||
-rw-r--r-- | src/pendulum/mixins/default.py | 37 |
2 files changed, 37 insertions, 0 deletions
diff --git a/src/pendulum/mixins/__init__.py b/src/pendulum/mixins/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/pendulum/mixins/__init__.py diff --git a/src/pendulum/mixins/default.py b/src/pendulum/mixins/default.py new file mode 100644 index 0000000..f2531f3 --- /dev/null +++ b/src/pendulum/mixins/default.py @@ -0,0 +1,37 @@ +from __future__ import annotations + +from pendulum.formatting import Formatter + + +_formatter = Formatter() + + +class FormattableMixin: + _formatter: Formatter = _formatter + + def format(self, fmt: str, locale: str | None = None) -> str: + """ + Formats the instance using the given format. + + :param fmt: The format to use + :param locale: The locale to use + """ + return self._formatter.format(self, fmt, locale) + + def for_json(self) -> str: + """ + Methods for automatic json serialization by simplejson. + """ + return self.isoformat() + + def __format__(self, format_spec: str) -> str: + if len(format_spec) > 0: + if "%" in format_spec: + return self.strftime(format_spec) + + return self.format(format_spec) + + return str(self) + + def __str__(self) -> str: + return self.isoformat() |