Commit ed1d1dad authored by Fabiano Franz's avatar Fabiano Franz

bump(github.com/docker/go-units): e30f1e79f3cd72542f2026ceec18d3bd67ab859c

parent 6c4033ee
...@@ -865,8 +865,8 @@ ...@@ -865,8 +865,8 @@
}, },
{ {
"ImportPath": "github.com/docker/go-units", "ImportPath": "github.com/docker/go-units",
"Comment": "v0.1.0-21-g0bbddae", "Comment": "v0.3.1-10-ge30f1e7",
"Rev": "0bbddae09c5a5419a8c6dcdd7ff90da3d450393b" "Rev": "e30f1e79f3cd72542f2026ceec18d3bd67ab859c"
}, },
{ {
"ImportPath": "github.com/docker/spdystream", "ImportPath": "github.com/docker/spdystream",
......
...@@ -30858,7 +30858,7 @@ Apache License ...@@ -30858,7 +30858,7 @@ Apache License
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
= vendor/github.com/docker/go-units/LICENSE.code 04424bc6f5a5be60691b9824d65c2ad8 - = vendor/github.com/docker/go-units/LICENSE 04424bc6f5a5be60691b9824d65c2ad8 -
================================================================================ ================================================================================
...@@ -10,9 +10,7 @@ See the [docs in godoc](https://godoc.org/github.com/docker/go-units) for exampl ...@@ -10,9 +10,7 @@ See the [docs in godoc](https://godoc.org/github.com/docker/go-units) for exampl
## Copyright and license ## Copyright and license
Copyright © 2015 Docker, Inc. All rights reserved, except as follows. Code Copyright © 2015 Docker, Inc.
is released under the Apache 2.0 license. The README.md file, and files in the
"docs" folder are licensed under the Creative Commons Attribution 4.0 go-units is licensed under the Apache License, Version 2.0.
International License under the terms and conditions set forth in the file See [LICENSE](LICENSE) for the full text of the license.
"LICENSE.docs". You may obtain a duplicate copy of the same license, titled
CC-BY-SA-4.0, at http://creativecommons.org/licenses/by/4.0/.
...@@ -12,19 +12,21 @@ import ( ...@@ -12,19 +12,21 @@ import (
func HumanDuration(d time.Duration) string { func HumanDuration(d time.Duration) string {
if seconds := int(d.Seconds()); seconds < 1 { if seconds := int(d.Seconds()); seconds < 1 {
return "Less than a second" return "Less than a second"
} else if seconds == 1 {
return "1 second"
} else if seconds < 60 { } else if seconds < 60 {
return fmt.Sprintf("%d seconds", seconds) return fmt.Sprintf("%d seconds", seconds)
} else if minutes := int(d.Minutes()); minutes == 1 { } else if minutes := int(d.Minutes()); minutes == 1 {
return "About a minute" return "About a minute"
} else if minutes < 60 { } else if minutes < 46 {
return fmt.Sprintf("%d minutes", minutes) return fmt.Sprintf("%d minutes", minutes)
} else if hours := int(d.Hours()); hours == 1 { } else if hours := int(d.Hours() + 0.5); hours == 1 {
return "About an hour" return "About an hour"
} else if hours < 48 { } else if hours < 48 {
return fmt.Sprintf("%d hours", hours) return fmt.Sprintf("%d hours", hours)
} else if hours < 24*7*2 { } else if hours < 24*7*2 {
return fmt.Sprintf("%d days", hours/24) return fmt.Sprintf("%d days", hours/24)
} else if hours < 24*30*3 { } else if hours < 24*30*2 {
return fmt.Sprintf("%d weeks", hours/24/7) return fmt.Sprintf("%d weeks", hours/24/7)
} else if hours < 24*365*2 { } else if hours < 24*365*2 {
return fmt.Sprintf("%d months", hours/24/30) return fmt.Sprintf("%d months", hours/24/30)
......
...@@ -31,27 +31,40 @@ type unitMap map[string]int64 ...@@ -31,27 +31,40 @@ type unitMap map[string]int64
var ( var (
decimalMap = unitMap{"k": KB, "m": MB, "g": GB, "t": TB, "p": PB} decimalMap = unitMap{"k": KB, "m": MB, "g": GB, "t": TB, "p": PB}
binaryMap = unitMap{"k": KiB, "m": MiB, "g": GiB, "t": TiB, "p": PiB} binaryMap = unitMap{"k": KiB, "m": MiB, "g": GiB, "t": TiB, "p": PiB}
sizeRegex = regexp.MustCompile(`^(\d+)([kKmMgGtTpP])?[bB]?$`) sizeRegex = regexp.MustCompile(`^(\d+(\.\d+)*) ?([kKmMgGtTpP])?[bB]?$`)
) )
var decimapAbbrs = []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"} var decimapAbbrs = []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
var binaryAbbrs = []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"} var binaryAbbrs = []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}
// CustomSize returns a human-readable approximation of a size func getSizeAndUnit(size float64, base float64, _map []string) (float64, string) {
// using custom format.
func CustomSize(format string, size float64, base float64, _map []string) string {
i := 0 i := 0
for size >= base { unitsLimit := len(_map) - 1
for size >= base && i < unitsLimit {
size = size / base size = size / base
i++ i++
} }
return fmt.Sprintf(format, size, _map[i]) return size, _map[i]
}
// CustomSize returns a human-readable approximation of a size
// using custom format.
func CustomSize(format string, size float64, base float64, _map []string) string {
size, unit := getSizeAndUnit(size, base, _map)
return fmt.Sprintf(format, size, unit)
}
// HumanSizeWithPrecision allows the size to be in any precision,
// instead of 4 digit precision used in units.HumanSize.
func HumanSizeWithPrecision(size float64, precision int) string {
size, unit := getSizeAndUnit(size, 1000.0, decimapAbbrs)
return fmt.Sprintf("%.*g %s", precision, size, unit)
} }
// HumanSize returns a human-readable approximation of a size // HumanSize returns a human-readable approximation of a size
// capped at 4 valid numbers (eg. "2.746 MB", "796 KB"). // capped at 4 valid numbers (eg. "2.746 MB", "796 KB").
func HumanSize(size float64) string { func HumanSize(size float64) string {
return CustomSize("%.4g %s", size, 1000.0, decimapAbbrs) return HumanSizeWithPrecision(size, 4)
} }
// BytesSize returns a human-readable size in bytes, kibibytes, // BytesSize returns a human-readable size in bytes, kibibytes,
...@@ -77,19 +90,19 @@ func RAMInBytes(size string) (int64, error) { ...@@ -77,19 +90,19 @@ func RAMInBytes(size string) (int64, error) {
// Parses the human-readable size string into the amount it represents. // Parses the human-readable size string into the amount it represents.
func parseSize(sizeStr string, uMap unitMap) (int64, error) { func parseSize(sizeStr string, uMap unitMap) (int64, error) {
matches := sizeRegex.FindStringSubmatch(sizeStr) matches := sizeRegex.FindStringSubmatch(sizeStr)
if len(matches) != 3 { if len(matches) != 4 {
return -1, fmt.Errorf("invalid size: '%s'", sizeStr) return -1, fmt.Errorf("invalid size: '%s'", sizeStr)
} }
size, err := strconv.ParseInt(matches[1], 10, 0) size, err := strconv.ParseFloat(matches[1], 64)
if err != nil { if err != nil {
return -1, err return -1, err
} }
unitPrefix := strings.ToLower(matches[2]) unitPrefix := strings.ToLower(matches[3])
if mul, ok := uMap[unitPrefix]; ok { if mul, ok := uMap[unitPrefix]; ok {
size *= mul size *= float64(mul)
} }
return size, nil return int64(size), nil
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment