Ai
2 Star 2 Fork 0

onpromise/license manage service

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
duration.go 1.57 KB
一键复制 编辑 原始数据 按行查看 历史
wangyuchang 提交于 2022-07-31 17:50 +08:00 . 1. 添加了删除/更新/查询接口
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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/onpromise/lms.git
git@gitee.com:onpromise/lms.git
onpromise
lms
license manage service
3a4157380cad

搜索帮助