Answer the question
In order to leave comments, you need to log in
Golang how to elegantly parse time.Duration?
You need to parse string into time.Duration. Format "HH:MM:SS"
Go already has a very convenient parsing of dates, you can read the string through , but from further on you need to get . It seemed to me that there is another convenient method in the standard library .
It would seem that this code should work:time.Parse("15:04:05", stringToParse)
time.Duration{}
time.Sub()
func parseDur(s string) time.Duration {
t, _ := time.Parse("15:04:05", s)
return t.Sub(time.Time{})
}
time.Duration{}
minus 1 year. You can fix it by returning it, but it looks so ugly.
I mean, the zero time value in Go is but by default it returns . This looks strange. Is this some kind of design flaw and does it need to be fixed at the standard library level?
The main question "how to parse elegantly" remains valid, maybe someone has some nice methods without strange constants. t.AddDate(1, 0, 0).Sub(time.Time{})
0001-01-01 00:00:00 +0000 UTC
time.Parse()
0000-01-01 00:00:00 +0000 UTC
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question