代码拉取完成,页面将自动刷新
package utils
import (
"fmt"
"regexp"
"strconv"
"time"
)
func ParseDuration(d string) (time.Duration, error) {
duration := 0 * time.Second
if d == "" {
return duration, nil
}
pat := regexp.MustCompile(`(\d+)(d|m)`)
matches := pat.FindAllStringSubmatch(d, -1)
if len(matches) <= 0 || len(matches[0]) < 3 {
return duration, fmt.Errorf("wrong duration format")
}
now := time.Now().UTC()
end := now
switch matches[0][2] {
case "d":
{
days, err := strconv.Atoi(matches[0][1])
if err != nil {
return duration, err
}
end = now.AddDate(0, 0, days)
}
case "m":
{
months, err := strconv.Atoi(matches[0][1])
if err != nil {
return duration, err
}
end = now.AddDate(0, months, 0)
}
default:
return duration, fmt.Errorf("wrong duration format, just suport <n>d <n>m")
}
duration = end.Sub(now)
return duration, nil
}
func ParseAppendDuration(d string) (time.Duration, error) {
if d == "" {
return ParseDuration(d)
}
if d[0] != '+' {
return 0 * time.Second, fmt.Errorf("not append duration")
}
return ParseDuration(d[1:])
}
func ParseMonthDay(d string) (days, months int, err error) {
if d == "" {
return
}
pat := regexp.MustCompile(`(\d+)(d|m)`)
matches := pat.FindAllStringSubmatch(d, -1)
if len(matches) <= 0 || len(matches[0]) < 3 {
err = fmt.Errorf("wrong duration format")
return
}
switch matches[0][2] {
case "d":
{
days, err = strconv.Atoi(matches[0][1])
}
case "m":
{
months, err = strconv.Atoi(matches[0][1])
}
default:
err = fmt.Errorf("wrong duration format, just suport <n>d <n>m")
}
return
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。