Skip to content

Commit

Permalink
fix: Fix the ParseDuration function
Browse files Browse the repository at this point in the history
  • Loading branch information
vaerh committed Jul 12, 2023
1 parent 9b6eaf5 commit 1995d9e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
5 changes: 5 additions & 0 deletions routeros/parse_duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package routeros
import (
"errors"
"fmt"
"strings"
"time"
)

Expand All @@ -28,6 +29,10 @@ func ParseDuration(s string) (time.Duration, error) {
if s == "" {
return 0, fmt.Errorf(`time: invalid duration "%v"`, orig)
}
// hh:mm:ss format
if ss := strings.Split(s, ":"); len(ss) == 3 {
s = ss[0] + "h" + ss[1] + "m" + ss[2]
}
for s != "" {
var (
v, f int64 // integers before, after decimal point
Expand Down
1 change: 1 addition & 0 deletions routeros/parse_duration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestParseDuration(t *testing.T) {
{name: "300ms", args: args{"300ms"}, want: time.Duration(300 * time.Millisecond), wantErr: false},
{name: "300", args: args{"300"}, want: time.Duration(300 * time.Second), wantErr: false},
{name: "300s", args: args{"300s"}, want: time.Duration(300 * time.Second), wantErr: false},
{name: "00:00:10", args: args{"00:00:10"}, want: time.Duration(10 * time.Second), wantErr: false},
{name: "2h45m", args: args{"2h45m"}, want: time.Duration(2*time.Hour + 45*time.Minute), wantErr: false},
{name: "163w4d9h", args: args{"163w4d9h"}, want: time.Duration(98960400 * time.Second), wantErr: false},
{name: "27489h", args: args{"27489h"}, want: time.Duration(98960400 * time.Second), wantErr: false},
Expand Down

0 comments on commit 1995d9e

Please sign in to comment.