Skip to content

Implement __format__ for temporal types #853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions neo4j/time/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from __future__ import annotations

import re
import typing as t
from datetime import (
date,
Expand Down Expand Up @@ -102,6 +103,8 @@
AVERAGE_SECONDS_IN_MONTH = 2629746
AVERAGE_SECONDS_IN_DAY = 86400

FORMAT_F_REPLACE = re.compile(r"(?<!%)%f")


def _is_leap_year(year):
if year % 4 != 0:
Expand Down Expand Up @@ -1307,7 +1310,10 @@ 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("000000000", format_spec)
return self.to_native().__format__(format_spec)

# INSTANCE METHOD ALIASES #

Expand Down Expand Up @@ -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 #

Expand Down Expand Up @@ -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 #

Expand Down
18 changes: 13 additions & 5 deletions tests/unit/common/time/test_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"
11 changes: 11 additions & 0 deletions tests/unit/common/time/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
13 changes: 13 additions & 0 deletions tests/unit/common/time/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")