Last active
August 7, 2025 09:30
-
-
Save wjkoh/ebc6e8fb852608dd4df31af773bb39d2 to your computer and use it in GitHub Desktop.
Go: How to Parse HH:MM:SS and HH:MM:SS.ttt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func ParseHMS(hms string) (time.Duration, error) { | |
| fields := strings.Split(strings.TrimSpace(hms), ":") | |
| var s string | |
| switch len(fields) { | |
| case 2: | |
| s = fields[0] + "m" + fields[1] + "s" | |
| case 3: | |
| s = fields[0] + "h" + fields[1] + "m" + fields[2] + "s" | |
| default: | |
| return time.Duration(0), fmt.Errorf("parse h:m:s: %s", hms) | |
| } | |
| return time.ParseDuration(s) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment