Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time duration can identify the number of days #4002

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ cmd/cmd
.hypothesis
__pycache__
/node_modules
/vendor
46 changes: 46 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package cmd
import (
"os"
"path"
"regexp"
"runtime"
"strconv"
"strings"
"time"

"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -323,7 +325,51 @@ func expandFlags(compoundFlags ...[]cli.Flag) []cli.Flag {
return flags
}

func parseDuration(input string) (time.Duration, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We can use time.ParseDuration to cover most of the cases, and handle 7d here.

re := regexp.MustCompile(`(?:(\d+)d)?(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?`)
matches := re.FindStringSubmatch(input)
if len(matches) == 5 {
var days, hours, minutes, seconds int
var err error
if matches[1] != "" {
days, err = strconv.Atoi(matches[1])
if err != nil {
return 0, err
}
}
if matches[2] != "" {
hours, err = strconv.Atoi(matches[2])
if err != nil {
return 0, err
}
}
if matches[3] != "" {
minutes, err = strconv.Atoi(matches[3])
if err != nil {
return 0, err
}
}
if matches[4] != "" {
seconds, err = strconv.Atoi(matches[4])
if err != nil {
return 0, err
}
}
durationDays := time.Duration(days*24*int(time.Hour) + hours*int(time.Hour) + minutes*int(time.Minute) + seconds*int(time.Second))
return durationDays, nil
} else {
logger.Warnf("Invalid input format of cache expire ")
return 0, nil
}
}

func duration(s string) time.Duration {
//param is *d
if strings.Contains(s, "d") {
if newDuration, err := parseDuration(s); err == nil {
return newDuration
}
}
if v, err := strconv.ParseInt(s, 10, 64); err == nil {
return time.Second * time.Duration(v)
}
Expand Down