Skip to main content

GeoSol Research Logo

NsEpoch (Nanosecond Epoch Time)

Nanosecond-accurate time representation with embedded epochs, arithmetic operations, and flexible string parsing.

Overview

gri-nsepoch provides three classes -- Time, Delta, and Epoch -- for working with time at nanosecond precision. Unlike datetime (microsecond resolution) or floating-point seconds (which resolve only ~240 ns at current dates), gri-nsepoch stores all durations as integer nanoseconds internally, eliminating floating-point accumulation errors.

Times are always relative to an Epoch (defaulting to Unix epoch, 1970-01-01). Subtracting two Time objects produces a Delta. Adding a Delta to a Time produces a new Time. The library interoperates with datetime, timedelta, and raw numeric types.

The package also installs ssep, a command-line tool for converting between time formats.

Requires Python 3.12+.

Mathematical Background

Standard IEEE 754 double-precision floats provide ~15-17 significant digits. A Unix timestamp for the year 2025 is approximately 1.74 x 10^9 seconds. Representing this with nanosecond precision requires 18+ digits -- exceeding float64 capacity. By storing nanoseconds as arbitrary-precision Python integers, gri-nsepoch avoids this limitation entirely:

internal storage: int nanoseconds (no precision loss)
float seconds:    ~240 ns resolution at current dates
datetime:         limited to microsecond resolution

Documentation

The wheel ships its documentation inside the package, in gri_nsepoch/docs/, so it is available wherever the package is installed:

  • overview.md -- concepts, construction, arithmetic, and gotchas
  • time_formats.md -- every string Time.from_str parses, and every formatted output
  • api_summary.md -- every public class, method, and signature (generated)

Print the directory with python -c "import gri_nsepoch, pathlib; print(pathlib.Path(gri_nsepoch.__file__).parent / 'docs')". Every example in those files is run by the test suite.

Installation

pip install gri-nsepoch

For development:

git clone https://gitlab.com/geosol-foss/python/gri-nsepoch.git
cd gri-nsepoch
uv sync

Quick Start

from gri_nsepoch import Time, Delta, Epoch

# Current time
now = Time()
print(now.iso)  # 2025-06-15T14:30:22.123+00:00

# From a string
t = Time.from_str("2025-01-15 08:30:00")
print(t.date_pp())  # 2025-01-15
print(t.time_pp(d=3))  # 08:30:00.000

# Arithmetic
t2 = t + Delta(s=3600)  # Add one hour
delta = t2 - t
print(delta.hms_pp())  # 01:00:00

# Nanosecond precision
t3 = Time(ns=1_000_000_123)
print(t3.time_pp(d=9))  # 00:00:01.000000123

Time Class

Time represents an absolute point in time as nanoseconds since an epoch.

Constructors:

t = Time()  # Current time (UTC)
t = Time(ns=0)  # Unix epoch exactly
t = Time(s=1e9)  # From seconds (some ns precision loss)
t = Time(ns=0, epoch=86400)  # Midnight Jan 2, 1970 as epoch
t = Time.from_str("2025-01-15")  # Parse from string
t = Time.from_str("2025-01-15 08:30:00", pytz_tz="America/Denver")

Properties and formatting:

t = Time.from_str("2025-06-15T14:30:22.123")

t.ns  # Integer nanoseconds since epoch (no precision loss)
t.secs  # Float seconds since epoch (some precision loss)
t.dt  # datetime object (microsecond resolution)
t.epoch  # The Epoch object
t.jd  # Julian Date as (jd, fractional_day) tuple

t.iso  # "2025-06-15T14:30:22.123+00:00"
t.date_pp()  # "2025-06-15"
t.time_pp(d=6)  # "14:30:22.123000"
t.dt_pp(d=3)  # "2025-06-15 14:30:22.123"
t.ssep_pp(s=True)  # "1,749,997,822" (with thousands separator)
t.mjd_pp(d=3)  # "60841-14:30:22.123"
t.tns_pp()  # "2025-06-15 522221230000000" (tenths of ns into the day)

Arithmetic:

  • Time + Delta -> Time
  • Time - Time -> Delta
  • Time - Delta -> Time
  • Time + Time -> TypeError

Timer usage:

start = Time()
# ... do work ...
elapsed = start.delta_now()
print(elapsed.hms_pp(d=3))  # "00:00:01.234"

Delta Class

Delta represents a time duration with nanosecond precision.

d = Delta(ns=5_500_000_000)  # 5.5 seconds
d = Delta(s=5.5)  # Same, with possible ns precision loss

d.ns  # 5500000000
d.secs  # 5.5
d.timedelta  # datetime.timedelta(seconds=5, microseconds=500000)
d.hms_pp(d=3)  # "00:00:05.500"
d.secs_pp(s=True)  # "5"

# Component access
d.delta_ints.SS  # 5 (seconds component)
d.delta_ints.MS  # 500 (milliseconds component)

Epoch Class

Epoch is a named reference point in time, stored as seconds since Unix epoch (1970-01-01). It subclasses int, so it works directly in arithmetic.

unix = Epoch()  # Unix epoch (default)
gps = Epoch(315964800, "GPS")  # GPS epoch (Jan 6, 1980)
custom = Epoch(946684800, "Y2K")  # Year 2000

print(gps)  # "GPS"
print(int(gps))  # 315964800

# Use with Time
t = Time(ns=0, epoch=gps)  # GPS epoch start

String Parsing

Time.from_str() recognizes many common formats without requiring a format string:

  • ISO 8601: "2025-01-15T08:30:00", "2025-01-15T08:30:00Z", "2025-01-15T08:30:00+05:00"
  • UTC offsets on any date-time: Z, z, UTC, GMT, +HH, +HHMM, +HH:MM (optionally after one space). Named abbreviations like EST are ambiguous and not accepted; use pytz_tz for a named zone. If both an offset and pytz_tz are given and they disagree at that instant, ValueError is raised.
  • Date-time: "2025-01-15 08:30:00", "2025/01/15 08:30:00"
  • Date only: "2025-01-15", "2025/01/15"
  • Seconds since the epoch: "1750000000", "1750000000.123456789". They count from epoch= when it is given, so ssep_pp() reads back.
  • Modified Julian Day as days-seconds or days-HH:MM:SS: "60841-52222.5", "60841-14:30:22.5"
  • Date and tenths of nanoseconds into the day: "2025-06-15 522221230000000"

A Julian Date, split or not, goes through Time.from_jd(jd, fr), the inverse of t.jd.

For non-standard formats, pass a strftime pattern:

t = Time.from_str("15-Jun-2025 14:30", "%d-%b-%Y %H:%M")

CLI Tool (ssep)

The ssep command converts any recognized time format to ISO and seconds-since-epoch:

$ ssep "2025-01-15 08:30:00"
ISO:   2025-01-15T08:30:00+00:00
Epoch: 1970-01-01
SSEP:  1736929800

$ ssep "2025-01-15 08:30:00" -e "2025-01-01"
ISO:   2025-01-15T08:30:00+00:00
Epoch: 2025-01-01
SSEP:  1240200

$ ssep  # No argument prints current time
ISO:   2025-06-15T14:30:22+00:00
Epoch: 1970-01-01
SSEP:  1750000222

Dependencies

  • gri-memoize: Per-instance caching of computed properties
  • pytz: Timezone-aware string parsing

Other Projects

Current list of other GRI FOSS Projects we are building and maintaining.

License

MIT License. See LICENSE for details.

Metadata

Release files for gri-nsepoch 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gri-nsepoch 0.3.0
File Size Uploaded Provenance Actions
gri_nsepoch-0.3.0.tar.gz 46.4 kB Without provenance Details for gri_nsepoch-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for gri-nsepoch 0.3.0
File Interpreter ABI Platform Actions
gri_nsepoch-0.3.0-py3-none-any.whl Python 3 none any Details for gri_nsepoch-0.3.0-py3-none-any.whl

Total release size: 77.9 kB

Release files / gri_nsepoch-0.3.0.tar.gz

Download URL gri_nsepoch-0.3.0.tar.gz
Size 46.4 kB
Tags Source
SHA-256 checksum
How to use checksums
1479e5c95a2b40ca789cd4c6fffaa25b12fbecf26db2297c3174bf6bbeb519d8
BLAKE2b-256 checksum
How to use checksums
355ec1a7c8f5f5adf2a91ccc18844a1f249337c447f6e054c7b3acbd5d2f4a17
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / gri_nsepoch-0.3.0-py3-none-any.whl

Download URL gri_nsepoch-0.3.0-py3-none-any.whl
Size 31.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
78ede4a57f6ca5cb7cdbb3fcb345889283af86d48626de9435456056eb20ae68
BLAKE2b-256 checksum
How to use checksums
4de67044cc99f4d6457a8ab8602912c87d96137213c3d4acfe5282ac964cf35c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for gri-nsepoch 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page