Skip to content

fix(duration): handle multi-digit values in string formatting#321

Merged
moshloop merged 1 commit into
masterfrom
feat/har-capture-and-logger-improvements
Jun 6, 2026
Merged

fix(duration): handle multi-digit values in string formatting#321
moshloop merged 1 commit into
masterfrom
feat/har-capture-and-logger-improvements

Conversation

@moshloop

@moshloop moshloop commented Jun 6, 2026

Copy link
Copy Markdown
Member

What

  • Fix Duration.String() method to correctly format multi-digit time values
  • Previously stripped digits from values like '30m' or '30s' incorrectly

Why

  • The stripping logic treated '0' in multi-digit numbers as standalone zero components
  • This caused malformed duration strings

Changes

  • Add helper function to check if character is a digit
  • Update stripping logic to only remove zero units when truly standalone
  • Add test cases for 10s, 30s, 60s, 90s, and 2h

Summary by CodeRabbit

  • Bug Fixes
    • Fixed duration display formatting to accurately handle zero units. The system now correctly preserves zeros that are part of compound time units (such as milliseconds) and prevents removal of zeros within larger numeric values, ensuring precise duration representation.

Fix the Duration.String() method to correctly handle zero-valued units in multi-digit time values. Previously, the code would incorrectly strip '0m' from values like '30m' or '0s' from '30s', treating the '0' as a standalone zero component when it was actually part of a larger number.

The fix adds a helper function to check if a character is a digit and updates the stripping logic to only remove zero units when they are truly standalone (preceded by a unit letter or at the start of the string, not by another digit).

Adds test cases for 10s, 30s, 60s, 90s, and 2h to ensure correct formatting of multi-digit values.
@coderabbitai

coderabbitai Bot commented Jun 6, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 882a6a57-6a6e-4ce9-bd56-d21df1d460af

📥 Commits

Reviewing files that changed from the base of the PR and between 74c1d30 and b3dd504.

⛔ Files ignored due to path filters (1)
  • cmd/hx/go.sum is excluded by !**/*.sum
📒 Files selected for processing (2)
  • duration/duration.go
  • duration/duration_test.go

Walkthrough

This PR tightens the zero-unit stripping logic in Duration.String(). The method now guards against removing "0m" when it appears inside "0ms" or when preceded by a digit, and prevents stripping "0s" when preceded by a digit. Test cases validate the fix for durations like 60 seconds and 90 seconds.

Changes

Duration zero-unit stripping precision

Layer / File(s) Summary
Zero-unit stripping with digit guards
duration/duration.go, duration/duration_test.go
Duration.String() enhances removal of standalone "0m" and "0s" by adding digit guards to avoid false positives in multi-digit or compound unit scenarios. New test cases verify correct formatting for 10s, 30s, 60s→1m, 90s→1m30s, and 2h.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and accurately describes the main fix: handling multi-digit values in the Duration.String() formatting logic, which is the core change across both modified files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/har-capture-and-logger-improvements
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch feat/har-capture-and-logger-improvements

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions

github-actions Bot commented Jun 6, 2026

Copy link
Copy Markdown

Gavel summary

Source Pass Fail Skip Duration
lint: golangci-lint 0 1 0 2ms
collections 49 0 0 3.2s
files 41 0 0 186ms
github.com/flanksource/commons/certs 4 0 0 350ms
github.com/flanksource/commons/cmd/hx 8 0 0 -
github.com/flanksource/commons/cmd/hx/parse 26 0 0 -
github.com/flanksource/commons/collections/syncmap 10 0 0 -
github.com/flanksource/commons/context 1 0 0 -
github.com/flanksource/commons/duration 2 0 0 -
github.com/flanksource/commons/files 16 0 0 -
github.com/flanksource/commons/har 28 0 0 -
github.com/flanksource/commons/hash 13 0 0 -
github.com/flanksource/commons/http 95 0 2 13.5s
github.com/flanksource/commons/logger 33 0 0 -
github.com/flanksource/commons/logger/httpretty/internal/color 15 0 0 -
github.com/flanksource/commons/logger/httpretty/internal/header 1 0 0 -
github.com/flanksource/commons/lookup 7 0 0 20ms
github.com/flanksource/commons/test 5 0 1 40ms
github.com/flanksource/commons/text 1 0 0 -
github.com/flanksource/commons/tokenizer 3 0 0 -
lint: betterleaks 0 0 1 -
logger 41 0 0 1ms
set 7 0 0 500.182µs

Totals: 406 passed · 1 failed · 4 skipped · 17.3s

Failing linters

golangci-lint — error

golangci-lint execution failed: fork/exec /home/runner/work/commons/commons/.gavel/golangci-lint: exec format error
Output:

View full results

@moshloop moshloop merged commit 340276a into master Jun 6, 2026
7 of 8 checks passed
@moshloop moshloop deleted the feat/har-capture-and-logger-improvements branch June 6, 2026 18:04
@github-actions

github-actions Bot commented Jun 6, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 1.53.1 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant