nanotime (jbenet's library)
nanotime is a Python library that provides a time object with nanosecond precision, storing time as a 64-bit UNIX timestamp (nanoseconds since epoch). It aims to offer a portable, easy-to-process time type with higher precision than standard Unix timestamps. This library was last updated in 2011 and provides a distinct approach to high-precision time compared to the `time.time_ns()` function introduced in Python 3.7.
Common errors
-
AttributeError: 'datetime.datetime' object has no attribute 'nanotime'
cause Attempting to call `nanotime` methods directly on a standard `datetime.datetime` object.fixTo convert a `datetime` object to a `Nanotime` object, use `Nanotime.from_datetime(your_datetime_obj)`. -
TypeError: 'float' object cannot be interpreted as an integer
cause Passing a floating-point number (e.g., from `time.time()`) directly to a `Nanotime` constructor or method expecting an integer nanosecond value, or when converting `Nanotime` to an imprecise float.fixEnsure you are using integer nanosecond values when working with `Nanotime` or explicitly convert from seconds to nanoseconds (e.g., `int(time.time() * 1_000_000_000)`). -
Precision loss when converting Nanotime to datetime object.
cause `datetime` objects internally store time with microsecond precision (6 decimal places for seconds), while `Nanotime` stores nanosecond precision (9 decimal places for seconds).fixThis is expected behavior. If nanosecond precision is critical, retain the `Nanotime` object or the raw nanosecond integer. Only convert to `datetime` when the loss of nanosecond detail is acceptable.
Warnings
- breaking The `nanotime` library provides its own `Nanotime` object. Direct integration with standard Python `datetime` objects will result in loss of nanosecond precision as `datetime` objects only support microseconds. This is a fundamental limitation of `datetime`.
- deprecated The `nanotime` library has not been updated since 2011. Python 3.7+ introduced `time.time_ns()`, `monotonic_ns()`, `perf_counter_ns()`, and `process_time_ns()` into the standard library, providing native nanosecond precision integers. These standard library functions are generally preferred for new development requiring nanosecond resolution.
- gotcha The internal representation of `Nanotime` is a 64-bit unsigned integer representing nanoseconds since the Unix epoch. While this provides a theoretical range of approximately 584 years, the library's design assumes an epoch of 1970 and may behave unexpectedly or have practical limits for dates far outside the 1970-2554 range.
Install
-
pip install nanotime
Imports
- Nanotime
from nanotime import Nanotime
Quickstart
from nanotime import Nanotime
import datetime
# Get current time with nanosecond precision
now = Nanotime.now()
print(f"Current nanotime: {now}")
# Convert to nanoseconds (integer)
ns_since_epoch = int(now)
print(f"Nanoseconds since epoch: {ns_since_epoch}")
# Convert to datetime object (loses nanosecond precision beyond microseconds)
dt_obj = now.datetime()
print(f"Converted to datetime: {dt_obj}")
# Create from a datetime object
some_dt = datetime.datetime(2023, 1, 1, 12, 30, 0, 123456)
nt_from_dt = Nanotime.from_datetime(some_dt)
print(f"Nanotime from datetime: {nt_from_dt}")
# Calculate duration
import time
start_nt = Nanotime.now()
time.sleep(0.001) # Simulate some work
end_nt = Nanotime.now()
delta_ns = int(end_nt) - int(start_nt)
print(f"Time elapsed (nanoseconds): {delta_ns}")