2 回答
TA贡献1829条经验 获得超9个赞
标准库中没有 API,但是有一个 3rd 方库可以将 ISO 8601 持续时间添加到time.Time:https ://godoc.org/github.com/senseyeio/duration#Duration.Shift 。
ISO 8601 持续时间一般不能转换为 a time.Duration,因为它取决于基数time.Time。
https://play.golang.org/p/guybDGoJVrT
package main
import (
"fmt"
"time"
"github.com/senseyeio/duration"
)
func main() {
d, _ := duration.ParseISO8601("P1D")
today := time.Now()
tomorrow := d.Shift(today)
fmt.Println(today.Format("Jan _2")) // Nov 11
fmt.Println(tomorrow.Format("Jan _2")) // Nov 12
}
TA贡献1805条经验 获得超9个赞
发现现有的解决方案不太令人满意,我创建了自己的模块来解析 ISO 8601 持续时间并将它们直接转换为time.Duration. 希望对你有帮助。:)
示例用法:https ://go.dev/play/p/Nz5akjy1c6W
- 2 回答
- 0 关注
- 347 浏览
添加回答
举报
