Skip to main content

Nanosecond-accurate time representation with embedded epochs and flexible parsing

Project description

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 lose precision beyond ~100 ns at typical epoch distances), 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:    limited to ~100 ns precision at current epoch distances
datetime:         limited to microsecond resolution

Installation

pip install gri-nsepoch

For development:

git clone https://gitlab.com/geosol-foss/python/gri-nsepoch.git
cd gri-nsepoch
. .init_venv.sh

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,750,000,222" (with thousands separator)
t.mjd_pp(d=3)       # "60840-14:30:22.123"

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"
  • Date-time: "2025-01-15 08:30:00", "2025/01/15 08:30:00"
  • Date only: "2025-01-15", "2025/01/15"
  • Epoch seconds: "1750000000", "1750000000.123456789"
  • Modified Julian Day: "MJD60840"

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:  1236600 (unix)

$ 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gri_nsepoch-0.2.2.tar.gz (44.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gri_nsepoch-0.2.2-py3-none-any.whl (18.9 kB view details)

Uploaded Python 3

File details

Details for the file gri_nsepoch-0.2.2.tar.gz.

File metadata

  • Download URL: gri_nsepoch-0.2.2.tar.gz
  • Upload date:
  • Size: 44.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for gri_nsepoch-0.2.2.tar.gz
Algorithm Hash digest
SHA256 24d83e9861b0b416e58fd32a28c11e4a9324654d12f85168c922d82bb25606ba
MD5 561a88e5b37a0c7dc09af3b5b164a39e
BLAKE2b-256 e481651ba8c552ff23e6eb96e7b8de95d27055959b9ef7ce20e3ffd0656c226d

See more details on using hashes here.

File details

Details for the file gri_nsepoch-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: gri_nsepoch-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 18.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for gri_nsepoch-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 faf2b831354f974b35de19fb45cba25ad7f5c20df746bdd0c8f25d4e4b7724bb
MD5 5db8f5d57551ac798389057b40f1e47c
BLAKE2b-256 fc3b58006f60230d22e01b1dd73a7ec430a2df539244ebadc6c9a104de9dbede

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page