Update dependencies. (#13778)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
1a82e3e575
Коммит
d7206d2ede
20
vendor/github.com/hako/durafmt/README.md
сгенерированный
поставляемый
20
vendor/github.com/hako/durafmt/README.md
сгенерированный
поставляемый
@@ -5,7 +5,7 @@
|
||||
|
||||
|
||||
|
||||
durafmt is a tiny Go library that formats `time.Duration` strings into a human readable format.
|
||||
durafmt is a tiny Go library that formats `time.Duration` strings (and types) into a human readable format.
|
||||
|
||||
```
|
||||
go get github.com/hako/durafmt
|
||||
@@ -34,6 +34,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hako/durafmt"
|
||||
)
|
||||
|
||||
@@ -56,6 +57,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hako/durafmt"
|
||||
)
|
||||
|
||||
@@ -77,6 +79,7 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/hako/durafmt"
|
||||
)
|
||||
|
||||
@@ -87,9 +90,9 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
### durafmt.ParseShort()
|
||||
#### LimitFirstN()
|
||||
|
||||
Version of `durafmt.Parse()` that only returns the first part of the duration string.
|
||||
Like `durafmt.ParseStringShort()` but for limiting the first N parts of the duration string.
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -97,25 +100,26 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/hako/durafmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
timeduration := (354 * time.Hour) + (22 * time.Minute) + (3 * time.Second)
|
||||
duration := durafmt.ParseShort(timeduration).String()
|
||||
fmt.Println(duration) // 2 weeks
|
||||
duration := durafmt.Parse(timeduration).LimitFirstN(2) // // limit first two parts.
|
||||
fmt.Println(duration) // 2 weeks 18 hours
|
||||
}
|
||||
```
|
||||
|
||||
# Contributing
|
||||
|
||||
Contributions are welcome! Fork this repo and add your changes and submit a PR.
|
||||
Contributions are welcome! Fork this repo, add your changes and submit a PR.
|
||||
|
||||
If you would like to fix a bug, add a feature or provide feedback you can do so in the issues section.
|
||||
|
||||
You can run tests by runnning `go test`. Running `go test; go vet; golint` is recommended.
|
||||
durafmt is tested against `golangci-lint` and you can run tests with `go test`.
|
||||
|
||||
durafmt is also tested against `gometalinter`.
|
||||
When contributing, running `go test; go vet; golint` or `golangci-lint` is recommended.
|
||||
|
||||
# License
|
||||
|
||||
|
||||
62
vendor/github.com/hako/durafmt/durafmt.go
сгенерированный
поставляемый
62
vendor/github.com/hako/durafmt/durafmt.go
сгенерированный
поставляемый
@@ -3,32 +3,42 @@ package durafmt
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
units = []string{"years", "weeks", "days", "hours", "minutes", "seconds", "milliseconds"}
|
||||
units = []string{"years", "weeks", "days", "hours", "minutes", "seconds", "milliseconds", "microseconds"}
|
||||
unitsShort = []string{"y", "w", "d", "h", "m", "s", "ms", "µs"}
|
||||
)
|
||||
|
||||
// Durafmt holds the parsed duration and the original input duration.
|
||||
type Durafmt struct {
|
||||
duration time.Duration
|
||||
input string // Used as reference.
|
||||
short bool
|
||||
limitN int // Non-zero to limit only first N elements to output.
|
||||
}
|
||||
|
||||
// LimitFirstN sets the output format, outputing only first N elements. n == 0 means no limit.
|
||||
func (d *Durafmt) LimitFirstN(n int) *Durafmt {
|
||||
d.limitN = n
|
||||
return d
|
||||
}
|
||||
|
||||
// Parse creates a new *Durafmt struct, returns error if input is invalid.
|
||||
func Parse(dinput time.Duration) *Durafmt {
|
||||
input := dinput.String()
|
||||
return &Durafmt{dinput, input, false}
|
||||
return &Durafmt{dinput, input, 0}
|
||||
}
|
||||
|
||||
// ParseShort creates a new *Durafmt struct, short form, returns error if input is invalid.
|
||||
// It's shortcut for `Parse(dur).LimitFirstN(1)`
|
||||
func ParseShort(dinput time.Duration) *Durafmt {
|
||||
input := dinput.String()
|
||||
return &Durafmt{dinput, input, true}
|
||||
return &Durafmt{dinput, input, 1}
|
||||
}
|
||||
|
||||
// ParseString creates a new *Durafmt struct from a string.
|
||||
@@ -41,11 +51,12 @@ func ParseString(input string) (*Durafmt, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Durafmt{duration, input, false}, nil
|
||||
return &Durafmt{duration, input, 0}, nil
|
||||
}
|
||||
|
||||
// ParseStringShort creates a new *Durafmt struct from a string, short form
|
||||
// returns an error if input is invalid.
|
||||
// It's shortcut for `ParseString(durStr)` and then calling `LimitFirstN(1)`
|
||||
func ParseStringShort(input string) (*Durafmt, error) {
|
||||
if input == "0" || input == "-0" {
|
||||
return nil, errors.New("durafmt: missing unit in duration " + input)
|
||||
@@ -54,7 +65,7 @@ func ParseStringShort(input string) (*Durafmt, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Durafmt{duration, input, true}, nil
|
||||
return &Durafmt{duration, input, 1}, nil
|
||||
}
|
||||
|
||||
// String parses d *Durafmt into a human readable duration.
|
||||
@@ -86,8 +97,13 @@ func (d *Durafmt) String() string {
|
||||
(seconds * 1000) - (minutes * 60000) - (hours * 3600000) -
|
||||
(days * 86400000) - (weeks * 604800000) - (years * 31536000000)
|
||||
|
||||
microseconds := int64(d.duration/time.Microsecond) -
|
||||
(milliseconds * 1000) - (seconds * 1000000) - (minutes * 60000000) - (hours * 3600000000) -
|
||||
(days * 86400000000) - (weeks * 604800000000) - (years * 31536000000000)
|
||||
|
||||
// Create a map of the converted duration time.
|
||||
durationMap := map[string]int64{
|
||||
"microseconds": microseconds,
|
||||
"milliseconds": milliseconds,
|
||||
"seconds": seconds,
|
||||
"minutes": minutes,
|
||||
@@ -98,7 +114,8 @@ func (d *Durafmt) String() string {
|
||||
}
|
||||
|
||||
// Construct duration string.
|
||||
for _, u := range units {
|
||||
for i := range units {
|
||||
u := units[i]
|
||||
v := durationMap[u]
|
||||
strval := strconv.FormatInt(v, 10)
|
||||
switch {
|
||||
@@ -110,25 +127,15 @@ func (d *Durafmt) String() string {
|
||||
duration += strval + " " + strings.TrimRight(u, "s") + " "
|
||||
// omit any value with 0s or 0.
|
||||
case d.duration.String() == "0" || d.duration.String() == "0s":
|
||||
// note: milliseconds and minutes have the same suffix (m)
|
||||
// so we have to check if the units match with the suffix.
|
||||
|
||||
// check for a suffix that is NOT the milliseconds suffix.
|
||||
if strings.HasSuffix(d.input, string(u[0])) && !strings.Contains(d.input, "ms") {
|
||||
// if it happens that the units are milliseconds, skip.
|
||||
if u == "milliseconds" {
|
||||
continue
|
||||
}
|
||||
pattern := fmt.Sprintf("^-?0%s$", unitsShort[i])
|
||||
isMatch, err := regexp.MatchString(pattern, d.input)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
if isMatch {
|
||||
duration += strval + " " + u
|
||||
}
|
||||
// process milliseconds here.
|
||||
if u == "milliseconds" {
|
||||
if strings.Contains(d.input, "ms") {
|
||||
duration += strval + " " + u
|
||||
break
|
||||
}
|
||||
}
|
||||
break
|
||||
|
||||
// omit any value with 0.
|
||||
case v == 0:
|
||||
continue
|
||||
@@ -139,8 +146,11 @@ func (d *Durafmt) String() string {
|
||||
|
||||
// if more than 2 spaces present return the first 2 strings
|
||||
// if short version is requested
|
||||
if d.short {
|
||||
duration = strings.Join(strings.Split(duration, " ")[:2], " ")
|
||||
if d.limitN > 0 {
|
||||
parts := strings.Split(duration, " ")
|
||||
if len(parts) > d.limitN*2 {
|
||||
duration = strings.Join(parts[:d.limitN*2], " ")
|
||||
}
|
||||
}
|
||||
|
||||
return duration
|
||||
|
||||
Ссылка в новой задаче
Block a user