Skip to content

Consolidate duration formatting functions in report.py (#243)#267

Merged
microsasa merged 1 commit intomainfrom
consolidate-duration-formatting-243-8210fdbed6ee6b1c
Mar 22, 2026
Merged

Consolidate duration formatting functions in report.py (#243)#267
microsasa merged 1 commit intomainfrom
consolidate-duration-formatting-243-8210fdbed6ee6b1c

Conversation

@microsasa
Copy link
Owner

Closes #243

Changes

Extracted a single _format_timedelta(td: timedelta) -> str core helper that encapsulates the hours/minutes/seconds decomposition and formatting logic. The three existing duration-formatting functions now delegate to it:

  • format_duration(ms)_format_timedelta(timedelta(milliseconds=ms))
  • _format_elapsed_since(start)_format_timedelta(now - ensure_aware(start))
  • _format_detail_duration(start, end) → guards for None, then _format_timedelta(end - start)

The format_duration public API signature is unchanged.

Behavior change

The unified helper omits zero-value units (consistent with how format_duration already worked):

  • 60s"1m" (was "1m 0s" in _format_detail_duration)
  • 3600s"1h" (was "1h 0m" in _format_detail_duration)
  • Zero elapsed → "0s" (was "0m 0s" in _format_elapsed_since)

Tests

  • Added 9 direct unit tests for _format_timedelta covering zero, negative, seconds-only, exact minute, exact hour, full h/m/s, and large durations
  • Updated existing test expectations to match the now-consistent formatting
  • All 522 tests pass, 98.74% coverage

Generated by Issue Implementer ·

Extract a single _format_timedelta(td: timedelta) core helper and
rewrite format_duration, _format_elapsed_since, and
_format_detail_duration to delegate to it, eliminating duplicated
hours/minutes/seconds decomposition logic.

Add direct unit tests for _format_timedelta covering zero, negative,
seconds-only, minutes, hours, and mixed durations. Update existing
test expectations where the unified formatting now omits zero-value
units (e.g. '1m 0s' → '1m', '1h 0m' → '1h').

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@microsasa microsasa added the aw Created by agentic workflow label Mar 22, 2026
@microsasa microsasa enabled auto-merge March 22, 2026 22:44
Copilot AI review requested due to automatic review settings March 22, 2026 22:44
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR consolidates the duration formatting logic in copilot_usage.report by introducing a single core _format_timedelta() helper and having existing duration-formatting helpers delegate to it, ensuring consistent output across the CLI report renderers.

Changes:

  • Added _format_timedelta(td: timedelta) -> str as the shared duration formatter (clamps negatives to 0s, omits zero-value units).
  • Updated format_duration(), _format_elapsed_since(), and _format_detail_duration() to delegate to _format_timedelta().
  • Updated boundary expectations and added direct unit tests for _format_timedelta.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/copilot_usage/report.py Introduces _format_timedelta() and routes existing duration formatting helpers through it for consistent formatting.
tests/copilot_usage/test_report.py Updates expectations for zero/minute/hour boundaries and adds focused unit coverage for _format_timedelta().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@microsasa microsasa added the aw-quality-gate-approved Quality gate approved the PR label Mar 22, 2026
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quality Gate: AUTO-APPROVED

Low-impact refactoring that consolidates three duration-formatting functions into a single _format_timedelta helper. No public API changes, no data model changes, no security concerns. 9 new unit tests with good edge-case coverage. All 8 CI checks green. Auto-approving for merge.

@microsasa microsasa merged commit aa2a456 into main Mar 22, 2026
8 checks passed
@microsasa microsasa deleted the consolidate-duration-formatting-243-8210fdbed6ee6b1c branch March 22, 2026 22:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

aw Created by agentic workflow aw-quality-gate-approved Quality gate approved the PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consolidate duration formatting functions in report.py

2 participants