summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--time/time.go11
-rw-r--r--time/time_test.go36
2 files changed, 26 insertions, 21 deletions
diff --git a/time/time.go b/time/time.go
index a7821d9..37ecdbb 100644
--- a/time/time.go
+++ b/time/time.go
@@ -8,17 +8,18 @@ import (
"fmt"
"regexp"
"strconv"
+ "time"
)
// ISO 8601 Duration regex for matching duration in PT3H4M60S format.
var durationRegex = regexp.MustCompile(`PT(([0-9]{0,2})?H)?(([0-9]{0,2})?M)?(([0-9]{0,2}?)S)?`)
-// Converts ISO 8601 duration[1] to seconds.
+// Converts ISO 8601 duration[1] to time.Duration
//
// Recognizes durations in this format: PT3H4M60S
//
// [1]: https://en.wikipedia.org/wiki/ISO_8601#Durations
-func Duration(duration string) (int, error) {
+func Duration(duration string) (time.Duration, error) {
m := durationRegex.FindStringSubmatch(duration)
if m == nil || len(m) == 0 {
return 0, fmt.Errorf("duration invalid: %v", duration)
@@ -40,5 +41,9 @@ func Duration(duration string) (int, error) {
secs += hours * 3600
secs += mins * 60
- return secs, nil
+ // Convert seconds to time.Duration.
+ d, err := time.ParseDuration(fmt.Sprintf("%ds", secs))
+
+ return d, nil
+}
}
diff --git a/time/time_test.go b/time/time_test.go
index 48ef75e..5bb6cb2 100644
--- a/time/time_test.go
+++ b/time/time_test.go
@@ -6,63 +6,63 @@ package time
import "testing"
func TestDurationToSeconds(t *testing.T) {
- secs, err := Duration("PT3H4M60S")
+ d, err := Duration("PT3H4M60S")
if err != nil {
t.Errorf("failed: %v", err)
return
}
- if secs != 11100 {
- t.Errorf("duration in seconds incorrect: %v", secs)
+ if d.Seconds() != 11100 {
+ t.Errorf("duration in seconds incorrect: %v", d)
return
}
- secs, err = Duration("PT4M60S")
+ d, err = Duration("PT4M60S")
if err != nil {
t.Errorf("failed: %v", err)
return
}
- if secs != 300 {
- t.Errorf("duration in seconds incorrect: %v", secs)
+ if d.Seconds() != 300 {
+ t.Errorf("duration in seconds incorrect: %v", d)
return
}
- secs, err = Duration("PT12H")
+ d, err = Duration("PT12H")
if err != nil {
t.Errorf("failed: %v", err)
return
}
- if secs != 43200 {
- t.Errorf("duration in seconds incorrect: %v", secs)
+ if d.Seconds() != 43200 {
+ t.Errorf("duration in seconds incorrect: %v", d)
return
}
- secs, err = Duration("PT1H")
+ d, err = Duration("PT1H")
if err != nil {
t.Errorf("failed: %v", err)
return
}
- if secs != 3600 {
- t.Errorf("duration in seconds incorrect: %v", secs)
+ if d.Seconds() != 3600 {
+ t.Errorf("duration in seconds incorrect: %v", d)
return
}
- secs, err = Duration("PT2H")
+ d, err = Duration("PT2H")
if err != nil {
t.Errorf("failed: %v", err)
return
}
- if secs != 7200 {
- t.Errorf("duration in seconds incorrect: %v", secs)
+ if d.Seconds() != 7200 {
+ t.Errorf("duration in seconds incorrect: %v", d)
return
}
- secs, err = Duration("PT45M")
+ d, err = Duration("PT45M")
if err != nil {
t.Errorf("failed: %v", err)
return
}
- if secs != 2700 {
- t.Errorf("duration in seconds incorrect: %v", secs)
+ if d.Seconds() != 2700 {
+ t.Errorf("duration in seconds incorrect: %v", d)
return
}
}