{"id":7441,"library":"nanotime","title":"nanotime (jbenet's library)","description":"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.","status":"abandoned","version":"0.5.2","language":"en","source_language":"en","source_url":"http://github.com/jbenet/nanotime/tree/master/python","tags":["time","nanosecond","precision","timestamp"],"install":[{"cmd":"pip install nanotime","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"Nanotime","correct":"from nanotime import Nanotime"}],"quickstart":{"code":"from nanotime import Nanotime\nimport datetime\n\n# Get current time with nanosecond precision\nnow = Nanotime.now()\nprint(f\"Current nanotime: {now}\")\n\n# Convert to nanoseconds (integer)\nns_since_epoch = int(now)\nprint(f\"Nanoseconds since epoch: {ns_since_epoch}\")\n\n# Convert to datetime object (loses nanosecond precision beyond microseconds)\ndt_obj = now.datetime()\nprint(f\"Converted to datetime: {dt_obj}\")\n\n# Create from a datetime object\nsome_dt = datetime.datetime(2023, 1, 1, 12, 30, 0, 123456)\nnt_from_dt = Nanotime.from_datetime(some_dt)\nprint(f\"Nanotime from datetime: {nt_from_dt}\")\n\n# Calculate duration\nimport time\nstart_nt = Nanotime.now()\ntime.sleep(0.001) # Simulate some work\nend_nt = Nanotime.now()\ndelta_ns = int(end_nt) - int(start_nt)\nprint(f\"Time elapsed (nanoseconds): {delta_ns}\")","lang":"python","description":"Demonstrates how to create a `Nanotime` object, convert it to an integer nanosecond timestamp, convert it to and from a standard `datetime` object, and measure elapsed time."},"warnings":[{"fix":"Store time as a `Nanotime` object or as a raw nanosecond integer if full precision is required. Convert to `datetime` only when microsecond precision is acceptable for display or interoperability with other libraries.","message":"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`.","severity":"breaking","affected_versions":"All versions"},{"fix":"For new projects on Python 3.7 or later, consider using `time.time_ns()` and related functions from the standard `time` module for nanosecond-level timestamps, rather than relying on this unmaintained third-party library.","message":"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.","severity":"deprecated","affected_versions":"All versions of nanotime, for Python 3.7+"},{"fix":"Understand the intended range and representation. For extremely broad date ranges, other time representations might be more suitable or robustly supported.","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"To convert a `datetime` object to a `Nanotime` object, use `Nanotime.from_datetime(your_datetime_obj)`.","cause":"Attempting to call `nanotime` methods directly on a standard `datetime.datetime` object.","error":"AttributeError: 'datetime.datetime' object has no attribute 'nanotime'"},{"fix":"Ensure 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)`).","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.","error":"TypeError: 'float' object cannot be interpreted as an integer"},{"fix":"This 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.","cause":"`datetime` objects internally store time with microsecond precision (6 decimal places for seconds), while `Nanotime` stores nanosecond precision (9 decimal places for seconds).","error":"Precision loss when converting Nanotime to datetime object."}]}