英文:
convering 00:33 to duration in golang
问题
我已经翻译好了你的代码,如下所示:
package main
import (
"fmt"
"strings"
"time"
)
func parseDuration(input string) (time.Duration, error) {
var layout string
if strings.Count(input, ":") == 1 {
layout = "04:05"
} else {
layout = "15:04:05"
}
t, err := time.Parse(layout, input)
if err != nil {
return 0, err
}
return t.Sub(time.Time{}), nil
}
func main() {
input := "00:04"
duration, err := parseDuration(input)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(int(duration.Seconds())) // 我应该得到4,但我得到的是-31622396
}
你可以在这里运行代码并查看结果:https://go.dev/play/p/A-eHc-EPTrd
英文:
I have a string "12:34" that is "MM:SS" format and I want to convert it to a time.Duration. Wasted too much time on this already. What am I doing wrong in this code:
package main
import (
"fmt"
"strings"
"time"
)
func parseDuration(input string) (time.Duration, error) {
var layout string
if strings.Count(input, ":") == 1 {
layout = "04:05"
} else {
layout = "15:04:05"
}
t, err := time.Parse(layout, input)
if err != nil {
return 0, err
}
return t.Sub(time.Time{}), nil
}
func main() {
input := "00:04"
duration, err := parseDuration(input)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(int(duration.Seconds())) // I should get 4 but I get -31622396
}
答案1
得分: 1
// type Time 的零值是 1 年 1 月 1 日 00:00:00.000000000 UTC。
func parseDuration(input string) (time.Duration, error) {
var layout string
if strings.Count(input, ":") == 1 {
layout = "04:05"
} else {
layout = "15:04:05"
}
t, err := time.Parse(layout, input)
if err != nil {
return 0, err
}
return t
}
fmt.Println(time.Time{})
// 这将打印 0001-01-01 00:00:00 +0000 UTC
fmt.Println(parseDuration("00:04"))
// 这将打印 0000-01-01 00:00:04 +0000 UTC
在你的情况下,你应该定义一个 start
对象,而不是直接使用 time.Time{}
。例如,
package main
import (
"fmt"
"strings"
"time"
)
var origin = time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC)
func parseDuration(input string) (time.Duration, error) {
var layout string
if strings.Count(input, ":") == 1 {
layout = "04:05"
} else {
layout = "15:04:05"
}
t, err := time.Parse(layout, input)
if err != nil {
return 0, err
}
return t.Sub(origin), nil
}
func main() {
input := "00:04"
duration, err := parseDuration(input)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(duration.String()) // 这将打印 4s
}
你可以在这里运行代码:https://go.dev/play/p/maGeyA0KWd3
英文:
> The zero value of type Time is January 1, year 1, 00:00:00.000000000 UTC.
func parseDuration(input string) (time.Duration, error) {
var layout string
if strings.Count(input, ":") == 1 {
layout = "04:05"
} else {
layout = "15:04:05"
}
t, err := time.Parse(layout, input)
if err != nil {
return 0, err
}
return t
}
fmt.Println(time.Time{})
// this prints 0001-01-01 00:00:00 +0000 UTC
fmt.Println(parseDuration("00:04"))
// this prints 0000-01-01 00:00:04 +0000 UTC
In your case, you should define a start
object instead of using time.Time{}
directly. For example,
package main
import (
"fmt"
"strings"
"time"
)
var origin = time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC)
func parseDuration(input string) (time.Duration, error) {
var layout string
if strings.Count(input, ":") == 1 {
layout = "04:05"
} else {
layout = "15:04:05"
}
t, err := time.Parse(layout, input)
if err != nil {
return 0, err
}
return t.Sub(origin), nil
}
func main() {
input := "00:04"
duration, err := parseDuration(input)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(duration.String()) // this prints 4s
}
答案2
得分: 0
问题出在这一行代码上:return t.Sub(time.Time{}), nil。你正在从t中减去time.Time的零值,这相当于从t中减去Unix纪元时间。
要从字符串中获取持续时间,你应该使用time.ParseDuration():
duration, err := time.ParseDuration(input)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(int(duration.Seconds()))
这将正确解析字符串中的持续时间,并且你将得到预期的输出4。
英文:
The issue is with the line return t.Sub(time.Time{}), nil. You're subtracting the zero value of time.Time from t, which is equivalent to subtracting the Unix epoch time from t.
To get the duration from the string, you should use time.ParseDuration() instead:
duration, err := time.ParseDuration(input)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(int(duration.Seconds()))
This will correctly parse the duration in the string, and you'll get the expected output of 4.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论