support 1w, 1d for loop

This commit is contained in:
bel
2021-07-17 22:49:10 -06:00
parent b8f0efc01c
commit d5ec073f75
2 changed files with 55 additions and 2 deletions

View File

@@ -7,6 +7,7 @@ import (
"io"
"io/ioutil"
"net/http"
"regexp"
"strconv"
"strings"
"time"
@@ -61,8 +62,34 @@ func ToStrArr(k string) []string {
}
func ToDuration(s string) time.Duration {
d, _ := time.ParseDuration(s)
return d
var d time.Duration
for _, c := range []struct {
key string
d time.Duration
}{
{"d", time.Hour * 24},
{"w", time.Hour * 24 * 7},
} {
daysPattern := regexp.MustCompile(`(^|[a-z])[0-9]+` + c.key + `($|[0-9])`)
idxes := daysPattern.FindAllStringIndex(s, -1)
if len(idxes) > 1 {
return 0
}
for _, idx := range idxes {
substr := s[idx[0]:idx[1]]
for len(substr) > 0 && (substr[0] < '0' || substr[0] > '9') {
substr = substr[1:]
}
for len(substr) > 0 && (substr[len(substr)-1] >= '0' && substr[len(substr)-1] <= '9') {
substr = substr[:len(substr)-1]
}
substr = strings.TrimSuffix(substr, c.key)
n, _ := strconv.Atoi(substr)
d += c.d * time.Duration(n)
}
}
d2, _ := time.ParseDuration(s)
return d2 + d
}
func ToTime(s string) time.Time {