Skip to content

Add Duration, a nanosecond-resolution timedelta - #50

Merged
jaraco merged 1 commit into
mainfrom
feature/duration
Jul 14, 2026
Merged

Add Duration, a nanosecond-resolution timedelta#50
jaraco merged 1 commit into
mainfrom
feature/duration

Conversation

@jaraco

@jaraco jaraco commented Jul 14, 2026

Copy link
Copy Markdown
Owner

What

Adds tempora.Duration: a Decimal-backed span of time with nanosecond resolution, for expressing and comparing intervals too short for datetime.timedelta (which bottoms out at one microsecond).

>>> print(tempora.Duration.parse('34.2 nsec'))
34.2 nsec
>>> print(tempora.Duration(microseconds=1.6))
1.6 µsec
>>> tempora.Duration.parse('38.1 nsec') / tempora.Duration.parse('34.2 nsec')
Decimal('1.114035087719298245614035088')

It constructs from nanoseconds or keyword units, parses textual durations (via parse_nanoseconds), and renders itself in the most natural unit (nsec/µsec/msec/sec) using a RangeMap. It supports the arithmetic these comparisons need — including division of one Duration by another to yield a dimensionless ratio, which datetime.timedelta offers but attotime does not — plus scaling, ordering, bool, and interconversion with datetime.timedelta.

Motivation

For jaraco/pytest-perf#18. pytest-perf compares timeit results; sub-microsecond timings (e.g. python/importlib_metadata#533) rounded to timedelta(0), collapsing its variance calculation. parse_nanoseconds (5.11) exposed a bare Decimal, but a scalar with implied units is a poor substitute for a value carrying its own magnitude and units — hence this richer type.

attotime was considered and set aside: attotimedelta / attotimedelta is unsupported, and it's an extra dependency; a bespoke type keeps the ratio ergonomic and the __str__ unit-aware.

Doctested; coh test green.

🤖 Generated with Claude Code

datetime.timedelta bottoms out at microsecond resolution, so very short
intervals -- the domain of timeit and of jaraco/pytest-perf#18 -- cannot
be represented, let alone compared, without rounding sub-microsecond
detail away. parse_nanoseconds addressed this by exposing a bare Decimal
count of nanoseconds, but a scalar with implied units is a poor
substitute for a value that carries its own magnitude and units.

Add Duration: a Decimal-backed span of time with nanosecond resolution.
It constructs from nanoseconds or keyword units, parses textual
durations, and renders itself in the most natural unit (nsec/µsec/msec/
sec) via a RangeMap. It supports the arithmetic these comparisons need --
notably division of one Duration by another to yield a dimensionless
ratio, which datetime.timedelta offers but attotime does not -- along
with scaling, ordering, and interconversion with datetime.timedelta.

Ref jaraco/pytest-perf#18.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@jaraco
jaraco merged commit cbe40fb into main Jul 14, 2026
28 checks passed
@jaraco
jaraco deleted the feature/duration branch July 14, 2026 01:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant