Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
handle nanoseconds, ..., milliseconds
Signed-off-by: iTrooz <hey@itrooz.fr>
  • Loading branch information
iTrooz committed Oct 23, 2025
commit 1dd35eb0ec9832c14934ecb33c995351de872ab5
27 changes: 16 additions & 11 deletions pkg/cmd/util/flag/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@ type Duration struct {
}

// unit map: symbol -> seconds
var unitMap = map[string]int64{
"yr": 365 * 24 * 3600,
"mo": 30 * 24 * 3600,
"w": 7 * 24 * 3600,
"d": 24 * 3600,
"h": 3600,
"m": 60,
"s": 1,
var unitMap = map[string]uint64{
"ns": uint64(time.Nanosecond),
"us": uint64(time.Microsecond),
"µs": uint64(time.Microsecond), // U+00B5 = micro symbol
"μs": uint64(time.Microsecond), // U+03BC = Greek letter mu
"ms": uint64(time.Millisecond),
"s": uint64(time.Second),
"m": uint64(time.Minute),
"h": uint64(time.Hour),
"d": uint64(24 * time.Hour),
"w": uint64(7 * 24 * time.Hour),
"mo": uint64(30 * 24 * time.Hour),
"yr": uint64(365 * 24 * time.Hour),
Comment thread
iTrooz marked this conversation as resolved.
Outdated
}

// ParseDuration parses strings like "2d5h10m"
Comment thread
iTrooz marked this conversation as resolved.
Outdated
Expand All @@ -33,7 +38,7 @@ func ParseDuration(s string) (Duration, error) {
return Duration{Duration: 0}, nil
}

var total int64
var total uint64
i := 0
n := len(s)

Expand All @@ -47,7 +52,7 @@ func ParseDuration(s string) (Duration, error) {
return Duration{}, fmt.Errorf("expected number at pos %d", i)
}
numStr := s[i:j]
num, err := strconv.ParseInt(numStr, 10, 64)
num, err := strconv.ParseUint(numStr, 10, 64)
if err != nil {
return Duration{}, err
}
Expand Down Expand Up @@ -77,7 +82,7 @@ func ParseDuration(s string) (Duration, error) {
}

// Convert integer seconds into time.Duration
return Duration{Duration: time.Duration(total) * time.Second}, nil
return Duration{Duration: time.Duration(total)}, nil
}

func (d *Duration) String() string { return duration.ShortHumanDuration(d.Duration) }
Expand Down