Improve duration humanization. (#4071)

This commit is contained in:
Bala FA
2017-04-26 16:08:35 +05:30
committed by Harshavardhana
parent 64c1c0f37d
commit cf1fc45142
13 changed files with 167 additions and 260 deletions

View File

@@ -113,7 +113,7 @@ func humanateBigBytes(s, base *big.Int, sizes []string) string {
//
// See also: ParseBigBytes.
//
// BigBytes(82854982) -> 83MB
// BigBytes(82854982) -> 83 MB
func BigBytes(s *big.Int) string {
sizes := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
return humanateBigBytes(s, bigSIExp, sizes)
@@ -123,7 +123,7 @@ func BigBytes(s *big.Int) string {
//
// See also: ParseBigBytes.
//
// BigIBytes(82854982) -> 79MiB
// BigIBytes(82854982) -> 79 MiB
func BigIBytes(s *big.Int) string {
sizes := []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}
return humanateBigBytes(s, bigIECExp, sizes)
@@ -134,19 +134,28 @@ func BigIBytes(s *big.Int) string {
//
// See also: BigBytes, BigIBytes.
//
// ParseBigBytes("42MB") -> 42000000, nil
// ParseBigBytes("42mib") -> 44040192, nil
// ParseBigBytes("42 MB") -> 42000000, nil
// ParseBigBytes("42 mib") -> 44040192, nil
func ParseBigBytes(s string) (*big.Int, error) {
lastDigit := 0
hasComma := false
for _, r := range s {
if !(unicode.IsDigit(r) || r == '.') {
if !(unicode.IsDigit(r) || r == '.' || r == ',') {
break
}
if r == ',' {
hasComma = true
}
lastDigit++
}
num := s[:lastDigit]
if hasComma {
num = strings.Replace(num, ",", "", -1)
}
val := &big.Rat{}
_, err := fmt.Sscanf(s[:lastDigit], "%f", val)
_, err := fmt.Sscanf(num, "%f", val)
if err != nil {
return nil, err
}