2 回答
TA贡献1884条经验 获得超4个赞
它并不完全是“开箱即用”,而是正则表达式可以完成这项工作:
package main
import "fmt"
import "regexp"
import "strconv"
import "time"
func main() {
fmt.Println(ParseDuration("PT15M"))
fmt.Println(ParseDuration("P12Y4MT15M"))
}
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)
}
TA贡献1712条经验 获得超3个赞
这是处理分数时间单位的代码,如PT3.001S.
var durationRegex = regexp.MustCompile(`P([\d\.]+Y)?([\d\.]+M)?([\d\.]+D)?T?([\d\.]+H)?([\d\.]+M)?([\d\.]+?S)?`)
// ParseDuration converts a ISO8601 duration into a time.Duration
func ParseDuration(str string) time.Duration {
matches := durationRegex.FindStringSubmatch(str)
years := parseDurationPart(matches[1], time.Hour*24*365)
months := parseDurationPart(matches[2], time.Hour*24*30)
days := parseDurationPart(matches[3], time.Hour*24)
hours := parseDurationPart(matches[4], time.Hour)
minutes := parseDurationPart(matches[5], time.Second*60)
seconds := parseDurationPart(matches[6], time.Second)
return time.Duration(years + months + days + hours + minutes + seconds)
}
func parseDurationPart(value string, unit time.Duration) time.Duration {
if len(value) != 0 {
if parsed, err := strconv.ParseFloat(value[:len(value)-1], 64); err == nil {
return time.Duration(float64(unit) * parsed)
}
}
return 0
}
- 2 回答
- 0 关注
- 433 浏览
添加回答
举报