Add Duration, a nanosecond-resolution timedelta - #50
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds
tempora.Duration: a Decimal-backed span of time with nanosecond resolution, for expressing and comparing intervals too short fordatetime.timedelta(which bottoms out at one microsecond).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 aRangeMap. It supports the arithmetic these comparisons need — including division of one Duration by another to yield a dimensionless ratio, whichdatetime.timedeltaoffers butattotimedoes not — plus scaling, ordering,bool, and interconversion withdatetime.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 bareDecimal, 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 / attotimedeltais unsupported, and it's an extra dependency; a bespoke type keeps the ratio ergonomic and the__str__unit-aware.Doctested;
coh testgreen.🤖 Generated with Claude Code