Skip to content

Commit

Permalink
Support us for microsecond
Browse files Browse the repository at this point in the history
µs and us are equivalent for microsecond
  • Loading branch information
xhit committed Aug 8, 2020
1 parent d4af659 commit 18ebd3a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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)},
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions str2duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
Expand Down
2 changes: 2 additions & 0 deletions str2duration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
Expand All @@ -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)},
Expand Down

0 comments on commit 18ebd3a

Please sign in to comment.