Closed
Description
Hi, I'm running into the following bug where neo4j.time.DateTime.now() isn't accepting a tzinfo argument:
>>> neo4j.time.DateTime.now(datetime.UTC)
Traceback (most recent call last):
File "/home/jack/Code/chance-backend/venv/lib/python3.12/site-packages/neo4j/time/__init__.py", line 2206, in now
return tz.fromutc( # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: fromutc: argument must be a datetime
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/jack/Code/chance-backend/venv/lib/python3.12/site-packages/neo4j/time/__init__.py", line 2216, in now
now_native = tz.fromutc(utc_now_native)
^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: fromutc: dt.tzinfo is not self
whereas ...Date.today() and ...Time.now() work fine:
>>> neo4j.time.Date.today(datetime.UTC)
neo4j.time.Date(2024, 10, 10)
>>> neo4j.time.Time.now(datetime.UTC)
neo4j.time.Time(12, 45, 8, 60610215, tzinfo=datetime.timezone.utc)
I'm currently using a workaround like this:
neo4j.time.DateTime.utc_now().replace(tzinfo=datetime.UTC)
It might just be caused by some legacy code in ...time/__init__.py
, and perhaps some changes like this would make it work and match the other now() and today() functions:
2205,2224c2205,2207
< try:
< return tz.fromutc( # type: ignore
< cls.from_clock_time( # type: ignore
< Clock().utc_time(), UnixEpoch
< ).replace(tzinfo=tz)
< )
< except TypeError:
< # For timezone implementations not compatible with the custom
< # datetime implementations, we can't do better than this.
< utc_now = cls.from_clock_time(Clock().utc_time(), UnixEpoch)
< utc_now_native = utc_now.to_native()
< now_native = tz.fromutc(utc_now_native)
< now = cls.from_native(now_native)
< return now.replace(
< nanosecond=(
< now.nanosecond
< + utc_now.nanosecond
< - utc_now_native.microsecond * 1000
< )
< )
---
> return cls.from_clock_time(Clock().local_time(), UnixEpoch)
> .replace(tzinfo=timezone.utc)
> .astimezone(tz)
I would make a pull request but I've just never worked on this project like this before, thanks.
My Environment
Python Version: 3.
Driver Version: 5.25.0
Server Version and Edition: Neo4j 5
Operating System: Arch Linux