1
0
mirror of https://github.com/rancher/steve.git synced 2025-09-16 15:29:04 +00:00

changing ParseHumanDuration to support timestamp values too (#684)

This commit is contained in:
Felipe Gehrke
2025-06-18 14:57:49 -03:00
committed by GitHub
parent b3539616e0
commit 4212386e13
4 changed files with 34 additions and 10 deletions

View File

@@ -6,11 +6,20 @@ import (
"time"
)
func ParseHumanReadableDuration(s string) (time.Duration, error) {
// ParseTimestampOrHumanReadableDuration can do one of three things with an incoming string:
// 1. Recognize it's an absolute timestamp and calculate a relative `time.Duration`
// 2. Recognize it's a human-readable duration (like 3m) and convert to a relative `time.Duration`
// 3. Return an error because it doesn't recognize the input
func ParseTimestampOrHumanReadableDuration(s string) (time.Duration, error) {
var total time.Duration
var val int
var unit byte
parsedTime, err := time.Parse(time.RFC3339, s)
if err == nil {
return time.Since(parsedTime), nil
}
r := strings.NewReader(s)
for r.Len() > 0 {
if _, err := fmt.Fscanf(r, "%d%c", &val, &unit); err != nil {