Skip to content

Fix Small Duration Formatting#15

Merged
icholy merged 3 commits intoicholy:masterfrom
ytaben:ytaben/fix-small-durations
Jul 12, 2024
Merged

Fix Small Duration Formatting#15
icholy merged 3 commits intoicholy:masterfrom
ytaben:ytaben/fix-small-durations

Conversation

@ytaben
Copy link
Copy Markdown
Contributor

@ytaben ytaben commented Jul 11, 2024

Greetings,

Currently, certain durations that end up with very small nanoseconds end up malformed such that from_str(to_str(duration)) results in an exception.

Specifically:

>>> duration.to_str(timedelta(seconds=0.000492))
'492us5.82077e-11ns'

>>> duration.from_str(duration.to_str(timedelta(seconds=0.000492))
Exception: Unknown unit e in duration 492us5.82077e-11ns

The reason is that we format a very small number with :g which uses scientific notation once it becomes shorter than :f. We also can't use :f because it will results in things like 4.00000s

We get the small number because of

>>> timedelta(seconds=0.000492).total_seconds() * durationpy.duration._second_size
492000.00000000006

I chose rounding nanoseconds because it should be the smallest unit of precision anyway but anything that fixes this is fine.

@icholy icholy merged commit 8b0a06e into icholy:master Jul 12, 2024
@icholy
Copy link
Copy Markdown
Owner

icholy commented Jul 12, 2024

Thanks for the fix! This is included in 0.7

@ytaben ytaben deleted the ytaben/fix-small-durations branch July 12, 2024 15:04
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.

2 participants