Skip to content

Commit 18ebd3a

Browse files
committed
Support us for microsecond
µs and us are equivalent for microsecond
1 parent d4af659 commit 18ebd3a

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ go get github.com/xhit/go-str2duration
1515

1616
Go String To Duration supports this strings conversions to duration:
1717
- All strings returned in time.Duration String.
18-
- A string more readable like 1w2d6h3ns (1 week 2 days 6 hours and 3 nanoseconds)
18+
- A string more readable like 1w2d6h3ns (1 week 2 days 6 hours and 3 nanoseconds).
19+
- `µs` and `us` are microsecond.
1920

2021
**Note**: a day is 24 hour.
2122

@@ -58,6 +59,7 @@ func main() {
5859
{"1s", time.Duration(time.Second)},
5960
{"1ms", time.Duration(time.Millisecond)},
6061
{"1µs", time.Duration(time.Microsecond)},
62+
{"1us", time.Duration(time.Microsecond)},
6163
{"1ns", time.Duration(time.Nanosecond)},
6264
{"4.000000001s", time.Duration(4*time.Second + time.Nanosecond)},
6365
{"1h0m4.000000001s", time.Duration(time.Hour + 4*time.Second + time.Nanosecond)},
@@ -80,6 +82,8 @@ func main() {
8082
//And can be case insensitive
8183
{"2D3S96NS", time.Duration(48*time.Hour + 3*time.Second + 96*time.Nanosecond)},
8284

85+
{"10s1us693ns", time.Duration(10*time.Second + time.Microsecond + 693*time.Nanosecond)},
86+
8387
} {
8488
durationFromString, err := str2duration.Str2Duration(tt.dur)
8589
if err != nil {

str2duration.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var reDuration *regexp.Regexp
2525

2626
func init() {
2727
reTimeDecimal = regexp.MustCompile(`(?i)(\d+)(?:(?:\.)(\d+))?((?:[mµn])?s)$`)
28-
reDuration = regexp.MustCompile(`(?i)^(?:(\d+)(?:w))?(?:(\d+)(?:d))?(?:(\d+)(?:h))?(?:(\d{1,2})(?:m))?(?:(\d+)(?:s))?(?:(\d+)(?:ms))?(?:(\d+)(?:µs))?(?:(\d+)(?:ns))?$`)
28+
reDuration = regexp.MustCompile(`(?i)^(?:(\d+)(?:w))?(?:(\d+)(?:d))?(?:(\d+)(?:h))?(?:(\d{1,2})(?:m))?(?:(\d+)(?:s))?(?:(\d+)(?:ms))?(?:(\d+)(?:(?:µ|u)s))?(?:(\d+)(?:ns))?$`)
2929
}
3030

3131
//Str2Duration returns time.Duration from string input
@@ -132,7 +132,7 @@ func decimalTimeToNano(str string) (string, error) {
132132
case "ms":
133133
nanoSeconds = 1000000
134134
dotTimeDecimal += strings.Repeat("0", 6-len(dotTimeDecimal))
135-
case "µs":
135+
case "µs", "us":
136136
nanoSeconds = 1000
137137
dotTimeDecimal += strings.Repeat("0", 3-len(dotTimeDecimal))
138138
}

str2duration_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func TestParseString(t *testing.T) {
1919
{"1s", time.Duration(time.Second)},
2020
{"1ms", time.Duration(time.Millisecond)},
2121
{"1µs", time.Duration(time.Microsecond)},
22+
{"1us", time.Duration(time.Microsecond)},
2223
{"1ns", time.Duration(time.Nanosecond)},
2324
{"4.000000001s", time.Duration(4*time.Second + time.Nanosecond)},
2425
{"1h0m4.000000001s", time.Duration(time.Hour + 4*time.Second + time.Nanosecond)},
@@ -27,6 +28,7 @@ func TestParseString(t *testing.T) {
2728
{"1.00002ms", time.Duration(time.Millisecond + 20*time.Nanosecond)},
2829
{"1.00000002s", time.Duration(time.Second + 20*time.Nanosecond)},
2930
{"693ns", time.Duration(693 * time.Nanosecond)},
31+
{"10s1us693ns", time.Duration(10*time.Second + time.Microsecond + 693*time.Nanosecond)},
3032

3133
//This times aren't returned with time.Duration string, but are easily readable and can be parsed too!
3234
{"1ms1ns", time.Duration(time.Millisecond + 1*time.Nanosecond)},

0 commit comments

Comments
 (0)