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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user