diff --git a/neo4j/time/__init__.py b/neo4j/time/__init__.py index 96b6aac1..ebca1d7f 100644 --- a/neo4j/time/__init__.py +++ b/neo4j/time/__init__.py @@ -24,6 +24,7 @@ from __future__ import annotations +import re import typing as t from datetime import ( date, @@ -102,6 +103,8 @@ AVERAGE_SECONDS_IN_MONTH = 2629746 AVERAGE_SECONDS_IN_DAY = 86400 +FORMAT_F_REPLACE = re.compile(r"(? str: def __format__(self, format_spec): """""" - raise NotImplementedError() + if not format_spec: + return self.iso_format() + format_spec = FORMAT_F_REPLACE.sub("000000000", format_spec) + return self.to_native().__format__(format_spec) # INSTANCE METHOD ALIASES # @@ -1905,7 +1911,11 @@ def __str__(self) -> str: def __format__(self, format_spec): """""" - raise NotImplementedError() + if not format_spec: + return self.iso_format() + format_spec = FORMAT_F_REPLACE.sub(f"{self.__nanosecond:09}", + format_spec) + return self.to_native().__format__(format_spec) # INSTANCE METHOD ALIASES # @@ -2663,7 +2673,11 @@ def __str__(self) -> str: def __format__(self, format_spec): """""" - raise NotImplementedError() + if not format_spec: + return self.iso_format() + format_spec = FORMAT_F_REPLACE.sub(f"{self.__time.nanosecond:09}", + format_spec) + return self.to_native().__format__(format_spec) # INSTANCE METHOD ALIASES # diff --git a/tests/unit/common/time/test_date.py b/tests/unit/common/time/test_date.py index d826d016..8f857e6b 100644 --- a/tests/unit/common/time/test_date.py +++ b/tests/unit/common/time/test_date.py @@ -514,11 +514,6 @@ def test_repr(self) -> None: assert repr(Date(2018, 4, 30)) == "neo4j.time.Date(2018, 4, 30)" assert repr(Date(0, 0, 0)) == "neo4j.time.ZeroDate" - def test_format(self) -> None: - d = Date(2018, 4, 30) - with pytest.raises(NotImplementedError): - _ = d.__format__("") - def test_from_native(self) -> None: native = date(2018, 10, 1) d = Date.from_native(native) @@ -573,3 +568,16 @@ def test_today(tz, expected) -> None: d = Date.today(tz=tz) assert isinstance(d, Date) assert d.year_month_day == expected + + +def test_str() -> None: + d = Date(2018, 4, 26) + assert str(d) == "2018-04-26" + + +def test_format() -> None: + d = Date(2018, 4, 26) + assert f"{d}" == "2018-04-26" + assert f"{d:%Y-%m-%d}" == "2018-04-26" + assert f"{d:%H:%M:%S}" == f"{date(2018, 4, 26):%H:%M:%S}" + assert f"{d:%Y-%m-%d %H:%M:%S.%f}" == "2018-04-26 00:00:00.000000000" diff --git a/tests/unit/common/time/test_datetime.py b/tests/unit/common/time/test_datetime.py index 462e6931..2a0577b8 100644 --- a/tests/unit/common/time/test_datetime.py +++ b/tests/unit/common/time/test_datetime.py @@ -870,3 +870,14 @@ def test_comparison(dt1, dt2) -> None: assert not dt1 > dt2 assert dt2 >= dt1 assert not dt1 >= dt2 + + +def test_str() -> None: + dt = DateTime(2018, 4, 26, 23, 0, 17, 914390409) + assert str(dt) == "2018-04-26T23:00:17.914390409" + + +def test_format() -> None: + dt = DateTime(2018, 4, 26, 23, 0, 17, 914390409) + assert f"{dt}" == "2018-04-26T23:00:17.914390409" + assert f"{dt:%Y-%m-%d %H:%M:%S.%f}" == "2018-04-26 23:00:17.914390409" diff --git a/tests/unit/common/time/test_time.py b/tests/unit/common/time/test_time.py index e94dd3b7..dbe48aa7 100644 --- a/tests/unit/common/time/test_time.py +++ b/tests/unit/common/time/test_time.py @@ -517,3 +517,16 @@ def test_comparison(self, t1, t2) -> None: assert not t1 > t2 assert t2 >= t1 assert not t1 >= t2 + + +def test_str() -> None: + t = Time(12, 34, 56, 789123001) + assert str(t) == "12:34:56.789123001" + + +def test_format() -> None: + t = Time(12, 34, 56, 789123001) + assert f"{t}" == "12:34:56.789123001" + assert f"{t:%Y-%m-%d}" == f"{time():%Y-%m-%d}" + assert (f"{t:%Y-%m-%d %H:%M:%S.%f}" + == f"{time():%Y-%m-%d} 12:34:56.789123001")