diff --git a/README.md b/README.md index 5010f77..71f9189 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,8 @@ go get github.com/xhit/go-str2duration Go String To Duration supports this strings conversions to duration: - All strings returned in time.Duration String. -- A string more readable like 1w2d6h3ns (1 week 2 days 6 hours and 3 nanoseconds) +- A string more readable like 1w2d6h3ns (1 week 2 days 6 hours and 3 nanoseconds). +- `µs` and `us` are microsecond. **Note**: a day is 24 hour. @@ -58,6 +59,7 @@ func main() { {"1s", time.Duration(time.Second)}, {"1ms", time.Duration(time.Millisecond)}, {"1µs", time.Duration(time.Microsecond)}, + {"1us", time.Duration(time.Microsecond)}, {"1ns", time.Duration(time.Nanosecond)}, {"4.000000001s", time.Duration(4*time.Second + time.Nanosecond)}, {"1h0m4.000000001s", time.Duration(time.Hour + 4*time.Second + time.Nanosecond)}, @@ -80,6 +82,8 @@ func main() { //And can be case insensitive {"2D3S96NS", time.Duration(48*time.Hour + 3*time.Second + 96*time.Nanosecond)}, + {"10s1us693ns", time.Duration(10*time.Second + time.Microsecond + 693*time.Nanosecond)}, + } { durationFromString, err := str2duration.Str2Duration(tt.dur) if err != nil { diff --git a/str2duration.go b/str2duration.go index 8c013ff..96cbcb1 100644 --- a/str2duration.go +++ b/str2duration.go @@ -25,7 +25,7 @@ var reDuration *regexp.Regexp func init() { reTimeDecimal = regexp.MustCompile(`(?i)(\d+)(?:(?:\.)(\d+))?((?:[mµn])?s)$`) - reDuration = regexp.MustCompile(`(?i)^(?:(\d+)(?:w))?(?:(\d+)(?:d))?(?:(\d+)(?:h))?(?:(\d{1,2})(?:m))?(?:(\d+)(?:s))?(?:(\d+)(?:ms))?(?:(\d+)(?:µs))?(?:(\d+)(?:ns))?$`) + reDuration = regexp.MustCompile(`(?i)^(?:(\d+)(?:w))?(?:(\d+)(?:d))?(?:(\d+)(?:h))?(?:(\d{1,2})(?:m))?(?:(\d+)(?:s))?(?:(\d+)(?:ms))?(?:(\d+)(?:(?:µ|u)s))?(?:(\d+)(?:ns))?$`) } //Str2Duration returns time.Duration from string input @@ -132,7 +132,7 @@ func decimalTimeToNano(str string) (string, error) { case "ms": nanoSeconds = 1000000 dotTimeDecimal += strings.Repeat("0", 6-len(dotTimeDecimal)) - case "µs": + case "µs", "us": nanoSeconds = 1000 dotTimeDecimal += strings.Repeat("0", 3-len(dotTimeDecimal)) } diff --git a/str2duration_test.go b/str2duration_test.go index 9fe1ae4..27665d5 100644 --- a/str2duration_test.go +++ b/str2duration_test.go @@ -19,6 +19,7 @@ func TestParseString(t *testing.T) { {"1s", time.Duration(time.Second)}, {"1ms", time.Duration(time.Millisecond)}, {"1µs", time.Duration(time.Microsecond)}, + {"1us", time.Duration(time.Microsecond)}, {"1ns", time.Duration(time.Nanosecond)}, {"4.000000001s", time.Duration(4*time.Second + time.Nanosecond)}, {"1h0m4.000000001s", time.Duration(time.Hour + 4*time.Second + time.Nanosecond)}, @@ -27,6 +28,7 @@ func TestParseString(t *testing.T) { {"1.00002ms", time.Duration(time.Millisecond + 20*time.Nanosecond)}, {"1.00000002s", time.Duration(time.Second + 20*time.Nanosecond)}, {"693ns", time.Duration(693 * time.Nanosecond)}, + {"10s1us693ns", time.Duration(10*time.Second + time.Microsecond + 693*time.Nanosecond)}, //This times aren't returned with time.Duration string, but are easily readable and can be parsed too! {"1ms1ns", time.Duration(time.Millisecond + 1*time.Nanosecond)},