mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-12-01 09:26:04 +03:00
36 lines
892 B
Go
36 lines
892 B
Go
|
package cron
|
||
|
|
||
|
import (
|
||
|
"regexp"
|
||
|
"strconv"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func ParseDuration(str string) time.Duration {
|
||
|
durationRegex := regexp.MustCompile(`P(?P<years>\d+Y)?(?P<months>\d+M)?(?P<days>\d+D)?T?(?P<hours>\d+H)?(?P<minutes>\d+M)?(?P<seconds>\d+S)?`)
|
||
|
matches := durationRegex.FindStringSubmatch(str)
|
||
|
|
||
|
years := ParseInt64(matches[1])
|
||
|
months := ParseInt64(matches[2])
|
||
|
days := ParseInt64(matches[3])
|
||
|
hours := ParseInt64(matches[4])
|
||
|
minutes := ParseInt64(matches[5])
|
||
|
seconds := ParseInt64(matches[6])
|
||
|
|
||
|
hour := int64(time.Hour)
|
||
|
minute := int64(time.Minute)
|
||
|
second := int64(time.Second)
|
||
|
return time.Duration(years*24*365*hour + months*30*24*hour + days*24*hour + hours*hour + minutes*minute + seconds*second)
|
||
|
}
|
||
|
|
||
|
func ParseInt64(value string) int64 {
|
||
|
if len(value) == 0 {
|
||
|
return 0
|
||
|
}
|
||
|
parsed, err := strconv.Atoi(value[:len(value)-1])
|
||
|
if err != nil {
|
||
|
return 0
|
||
|
}
|
||
|
return int64(parsed)
|
||
|
}
|