diff options
author | 6543 <6543@obermui.de> | 2020-10-16 07:06:27 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-16 01:06:27 -0400 |
commit | 12a1f914f443cc31af4bc54ab43802a75742cd57 (patch) | |
tree | 998e159281cf41de8b6d7bb1ab5075b3286ce5a9 /vendor/github.com/prometheus/common/model/time.go | |
parent | 91f2afdb546364195ff909186983b94a61ab3181 (diff) | |
download | gitea-12a1f914f443cc31af4bc54ab43802a75742cd57.tar.gz gitea-12a1f914f443cc31af4bc54ab43802a75742cd57.zip |
Vendor Update Go Libs (#13166)
* update github.com/alecthomas/chroma v0.8.0 -> v0.8.1
* github.com/blevesearch/bleve v1.0.10 -> v1.0.12
* editorconfig-core-go v2.1.1 -> v2.3.7
* github.com/gliderlabs/ssh v0.2.2 -> v0.3.1
* migrate editorconfig.ParseBytes to Parse
* github.com/shurcooL/vfsgen to 0d455de96546
* github.com/go-git/go-git/v5 v5.1.0 -> v5.2.0
* github.com/google/uuid v1.1.1 -> v1.1.2
* github.com/huandu/xstrings v1.3.0 -> v1.3.2
* github.com/klauspost/compress v1.10.11 -> v1.11.1
* github.com/markbates/goth v1.61.2 -> v1.65.0
* github.com/mattn/go-sqlite3 v1.14.0 -> v1.14.4
* github.com/mholt/archiver v3.3.0 -> v3.3.2
* github.com/microcosm-cc/bluemonday 4f7140c49acb -> v1.0.4
* github.com/minio/minio-go v7.0.4 -> v7.0.5
* github.com/olivere/elastic v7.0.9 -> v7.0.20
* github.com/urfave/cli v1.20.0 -> v1.22.4
* github.com/prometheus/client_golang v1.1.0 -> v1.8.0
* github.com/xanzy/go-gitlab v0.37.0 -> v0.38.1
* mvdan.cc/xurls v2.1.0 -> v2.2.0
Co-authored-by: Lauris BH <lauris@nix.lv>
Diffstat (limited to 'vendor/github.com/prometheus/common/model/time.go')
-rw-r--r-- | vendor/github.com/prometheus/common/model/time.go | 100 |
1 files changed, 52 insertions, 48 deletions
diff --git a/vendor/github.com/prometheus/common/model/time.go b/vendor/github.com/prometheus/common/model/time.go index 7b0064fdb2..c40e6403ca 100644 --- a/vendor/github.com/prometheus/common/model/time.go +++ b/vendor/github.com/prometheus/common/model/time.go @@ -181,73 +181,77 @@ func (d *Duration) Type() string { return "duration" } -var durationRE = regexp.MustCompile("^([0-9]+)(y|w|d|h|m|s|ms)$") +var durationRE = regexp.MustCompile("^(([0-9]+)y)?(([0-9]+)w)?(([0-9]+)d)?(([0-9]+)h)?(([0-9]+)m)?(([0-9]+)s)?(([0-9]+)ms)?$") // ParseDuration parses a string into a time.Duration, assuming that a year // always has 365d, a week always has 7d, and a day always has 24h. func ParseDuration(durationStr string) (Duration, error) { + switch durationStr { + case "0": + // Allow 0 without a unit. + return 0, nil + case "": + return 0, fmt.Errorf("empty duration string") + } matches := durationRE.FindStringSubmatch(durationStr) - if len(matches) != 3 { + if matches == nil { return 0, fmt.Errorf("not a valid duration string: %q", durationStr) } - var ( - n, _ = strconv.Atoi(matches[1]) - dur = time.Duration(n) * time.Millisecond - ) - switch unit := matches[2]; unit { - case "y": - dur *= 1000 * 60 * 60 * 24 * 365 - case "w": - dur *= 1000 * 60 * 60 * 24 * 7 - case "d": - dur *= 1000 * 60 * 60 * 24 - case "h": - dur *= 1000 * 60 * 60 - case "m": - dur *= 1000 * 60 - case "s": - dur *= 1000 - case "ms": - // Value already correct - default: - return 0, fmt.Errorf("invalid time unit in duration string: %q", unit) + var dur time.Duration + + // Parse the match at pos `pos` in the regex and use `mult` to turn that + // into ms, then add that value to the total parsed duration. + m := func(pos int, mult time.Duration) { + if matches[pos] == "" { + return + } + n, _ := strconv.Atoi(matches[pos]) + d := time.Duration(n) * time.Millisecond + dur += d * mult } + + m(2, 1000*60*60*24*365) // y + m(4, 1000*60*60*24*7) // w + m(6, 1000*60*60*24) // d + m(8, 1000*60*60) // h + m(10, 1000*60) // m + m(12, 1000) // s + m(14, 1) // ms + return Duration(dur), nil } func (d Duration) String() string { var ( - ms = int64(time.Duration(d) / time.Millisecond) - unit = "ms" + ms = int64(time.Duration(d) / time.Millisecond) + r = "" ) if ms == 0 { return "0s" } - factors := map[string]int64{ - "y": 1000 * 60 * 60 * 24 * 365, - "w": 1000 * 60 * 60 * 24 * 7, - "d": 1000 * 60 * 60 * 24, - "h": 1000 * 60 * 60, - "m": 1000 * 60, - "s": 1000, - "ms": 1, - } - switch int64(0) { - case ms % factors["y"]: - unit = "y" - case ms % factors["w"]: - unit = "w" - case ms % factors["d"]: - unit = "d" - case ms % factors["h"]: - unit = "h" - case ms % factors["m"]: - unit = "m" - case ms % factors["s"]: - unit = "s" + f := func(unit string, mult int64, exact bool) { + if exact && ms%mult != 0 { + return + } + if v := ms / mult; v > 0 { + r += fmt.Sprintf("%d%s", v, unit) + ms -= v * mult + } } - return fmt.Sprintf("%v%v", ms/factors[unit], unit) + + // Only format years and weeks if the remainder is zero, as it is often + // easier to read 90d than 12w6d. + f("y", 1000*60*60*24*365, true) + f("w", 1000*60*60*24*7, true) + + f("d", 1000*60*60*24, false) + f("h", 1000*60*60, false) + f("m", 1000*60, false) + f("s", 1000, false) + f("ms", 1, false) + + return r } // MarshalYAML implements the yaml.Marshaler interface. |