opentofu/vendor/github.com/dylanmei/iso8601/duration.go
Paul Hinze 6fe2703665 Vendor all dependencies w/ Godep
* Remove `make updatedeps` from Travis build. We'll follow up with more
   specific plans around dependency updating in subsequent PRs.
 * Update all `make` targets to set `GO15VENDOREXPERIMENT=1` and to
   filter out `/vendor/` from `./...` where appropriate.
 * Temporarily remove `vet` from the `make test` target until we can
   figure out how to get it to not vet `vendor/`. (Initial
   experimentation failed to yield the proper incantation.)

Everything is pinned to current master, with the exception of:

 * Azure/azure-sdk-for-go which is pinned before the breaking change today
 * aws/aws-sdk-go which is pinned to the most recent tag

The documentation still needs to be updated, which we can do in a follow
up PR. The goal here is to unblock release.
2016-01-29 15:08:48 -06:00

97 lines
2.1 KiB
Go

package iso8601
import (
"errors"
"fmt"
"regexp"
"strconv"
"time"
)
var (
// ErrBadFormat is returned when parsing fails
ErrBadFormat = errors.New("bad format string")
// ErrNoMonth is raised when a month is in the format string
ErrNoMonth = errors.New("no months allowed")
full = regexp.MustCompile(`P((?P<year>\d+)Y)?((?P<month>\d+)M)?((?P<day>\d+)D)?(T((?P<hour>\d+)H)?((?P<minute>\d+)M)?((?P<second>\d+)S)?)?`)
week = regexp.MustCompile(`P((?P<week>\d+)W)`)
)
// adapted from https://github.com/BrianHicks/finch/duration
func ParseDuration(value string) (time.Duration, error) {
var match []string
var regex *regexp.Regexp
if week.MatchString(value) {
match = week.FindStringSubmatch(value)
regex = week
} else if full.MatchString(value) {
match = full.FindStringSubmatch(value)
regex = full
} else {
return time.Duration(0), ErrBadFormat
}
d := time.Duration(0)
day := time.Hour * 24
week := day * 7
year := day * 365
for i, name := range regex.SubexpNames() {
part := match[i]
if i == 0 || name == "" || part == "" {
continue
}
value, err := strconv.Atoi(part)
if err != nil {
return time.Duration(0), err
}
switch name {
case "year":
d += year * time.Duration(value)
case "month":
return time.Duration(0), ErrNoMonth
case "week":
d += week * time.Duration(value)
case "day":
d += day * time.Duration(value)
case "hour":
d += time.Hour * time.Duration(value)
case "minute":
d += time.Minute * time.Duration(value)
case "second":
d += time.Second * time.Duration(value)
}
}
return d, nil
}
func FormatDuration(duration time.Duration) string {
// we're not doing negative durations
if duration.Seconds() <= 0 {
return "PT0S"
}
hours := int(duration.Hours())
minutes := int(duration.Minutes()) - (hours * 60)
seconds := int(duration.Seconds()) - (hours*3600 + minutes*60)
// we're not doing Y,M,W
s := "PT"
if hours > 0 {
s = fmt.Sprintf("%s%dH", s, hours)
}
if minutes > 0 {
s = fmt.Sprintf("%s%dM", s, minutes)
}
if seconds > 0 {
s = fmt.Sprintf("%s%dS", s, seconds)
}
return s
}