docker / go-units

Parse and print size and time units in human-readable format
https://godoc.org/github.com/docker/go-units
Apache License 2.0
218 stars 38 forks source link

Add FromSize method #37

Open cfergeau opened 3 years ago

cfergeau commented 3 years ago

go-units API is missing a method which would parse "32kB" as 32000 bytes, and "32kiB" as 32768 bytes. FromHumanSize() parses both as 32000 bytes, while RAMInBytes() parses both as 32768 bytes.

This commit introduces a FromSize method a more litteral parsing of the unit is needed. Modifiers without a unit ('32k') will be assumed to be using a decimal unit, so they'll equivalent to 32 kB/32000 bytes.

This fixes https://github.com/docker/go-units/issues/31

kolyshkin commented 2 years ago

This functionality would be very nice to have indeed.