datetime: add compact timedelta formatting for zero-day durations - #1185
datetime: add compact timedelta formatting for zero-day durations#118514NGiestas wants to merge 8 commits into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1185 +/- ##
==========================================
+ Coverage 68.20% 68.43% +0.22%
==========================================
Files 19 19
Lines 2378 2395 +17
==========================================
+ Hits 1622 1639 +17
Misses 756 756 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
a700b84 to
244ffba
Compare
jvdp1
left a comment
There was a problem hiding this comment.
Thank you @14NGiestas . Overall LGTM. O wonder if the compact format could be optional.
| #### Description | ||
|
|
||
| Formats a `timedelta_type` as a human-readable string. | ||
| For zero-day durations, compact forms are used: |
There was a problem hiding this comment.
Should this format be optional, e.g.,
str = format_timedelta(compact=.true.)
That's a good idea, so it keeps backwards compatible just in case someone's workflow already depends on it (I can think of tests that might break if comparing the strings). It would be nicer in the future a way of controlling it globally but just to streamline this PR, I will default Relevant XKCD: 1172 |
Clarify conditions for preserving verbose duration format.
There was a problem hiding this comment.
Pull request overview
This PR extends stdlib_datetime’s format_timedelta to support an opt-in compact formatting mode for zero-day durations, while preserving the existing verbose representation by default.
Changes:
- Added optional
compactargument toformat_timedeltato enable compact formatting for zero-day timedeltas. - Updated unit tests to cover both legacy formatting and the new compact mode.
- Updated documentation and example usage to describe/demonstrate the new behavior.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/datetime/stdlib_datetime.f90 |
Adds compact optional argument and implements compact formatting branch for days==0. |
test/datetime/test_datetime.f90 |
Adds compact-format test coverage and expands legacy-format assertions. |
example/datetime/example_datetime_usage.f90 |
Adds a “Compact Duration Formatting” example section (currently missing compact=.true. in calls). |
doc/specs/stdlib_datetime.md |
Documents compact formatting option and updates syntax/return examples (currently missing .mmm note for MM:SS[.mmm]). |
Suppressed comments (1)
example/datetime/example_datetime_usage.f90:78
- These example printouts are labeled as compact formatting but still use the default legacy formatting because compact=.true. is not passed.
duration = timedelta(seconds=65)
print '(A,T40,A)', 'timedelta(s=65):', format_timedelta(duration)
duration = timedelta(hours=1, minutes=2, seconds=3)
print '(A,T40,A)', 'timedelta(1h 2m 3s):', format_timedelta(duration)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Summary
This PR improves duration formatting in the datetime module by adding compact, human-friendly output for zero-day timedeltas while preserving the existing verbose format for nonzero-day values.
What changed
format_timedeltainsrc/datetime/stdlib_datetime.f90to use compact zero-day formatting:5msfor sub-second durations1.500sor42sfor sub-minute durationsMM:SSwhen hours are zeroHH:MM:SS[.mmm]otherwiseX days, HH:MM:SS[.mmm].doc/specs/stdlib_datetime.md.example/datetime/example_datetime_usage.f90.test/datetime/test_datetime.f90.compact, passcompact=.true.to the new behavior, the default is the old verbose.Why
The prior formatting was always verbose, which made short durations harder to scan. Compact formatting improves readability for common short intervals while preserving the legacy representation where day-level context is meaningful.
Validation
test_datetimeincludes new compact-format coverage and legacy-format checks.Compatibility