neotime: Nanosecond Temporal Types
neotime provides nanosecond-precision temporal types for Python, offering classes like Duration, Date, Time, and DateTime, similar to the standard library's datetime module but with enhanced precision. The library is currently abandoned, as its core temporal type implementations have been rolled into the Neo4j Python driver (version 4.0 and later). The last release was 1.7.4 in December 2018.
Common errors
-
TypeError: 'neotime.DateTime' object is not JSON serializable
cause neotime objects are custom types and do not have a default JSON serialization method, which is common when trying to send them via APIs or save them to JSON.fixImplement a custom JSON encoder or convert neotime objects to standard ISO-8601 strings (e.g., `str(my_datetime)`) or Python `datetime` objects before serialization. -
AttributeError: module 'neotime' has no attribute 'xyz'
cause This error often occurs when users expect specific functionality (e.g., a constant or a utility function) that is either not part of the `neotime` API, or they are mistakenly trying to access a `neo4j.time` attribute from `neotime` (or vice-versa) after migrating.fixRefer to the `neotime` documentation (or the `neo4j.time` documentation if you are in a Neo4j 4.0+ context) for available attributes and methods. Ensure you are importing and using the correct module for the desired types.
Warnings
- breaking The `neotime` library has been officially abandoned. Its temporal type implementations were integrated directly into the `neo4j-driver` project starting with version 4.0. Users of `neo4j-driver` should use `neo4j.time` types instead of `neotime`.
- gotcha This library is no longer maintained. There will be no further updates, bug fixes, or new feature development for the standalone `neotime` package.
- gotcha Mixing `neotime` objects with `datetime` module objects or `neo4j.time` objects can lead to `TypeError` or unexpected behavior due to differing internal representations and precision.
Install
-
pip install neotime
Imports
- Date
from neotime import Date
- Time
from neotime import Time
- DateTime
from neotime import DateTime
- Duration
from neotime import Duration
Quickstart
from neotime import DateTime, Duration
# Create a DateTime object
now = DateTime.now()
print(f"Current nanosecond-precision time: {now}")
# Create a Duration object
delta = Duration(days=1, seconds=3600, nanoseconds=500_000_000)
print(f"A duration: {delta}")
# Perform arithmetic
future_time = now + delta
print(f"Time in the future: {future_time}")
# Access components
print(f"Year: {now.year}, Nanosecond: {now.nanosecond}")