You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
Code generation
I have a time in format 1days2hours3minutes4seconds which means 1 day 2 hours 3 minutes 4 seconds start from now, I want to convert it to a time.Time
Result
import (
"errors""fmt""regexp""strconv""time"
)
funcparseDuration(sstring) (time.Time, error) {
// Use regular expressions to extract the duration componentsr:=regexp.MustCompile(`(\d+)days(\d+)hours(\d+)minutes(\d+)seconds`)
matches:=r.FindStringSubmatch(s)
iflen(matches) !=5 {
return time.Time{}, errors.New("invalid duration format")
}
// Parse the duration components into integersdays, _:=strconv.Atoi(matches[1])
hours, _:=strconv.Atoi(matches[2])
minutes, _:=strconv.Atoi(matches[3])
seconds, _:=strconv.Atoi(matches[4])
// Calculate the total duration in secondstotalSeconds:= (days*24*60*60) + (hours*60*60) + (minutes*60) +seconds// Get the current time and add the duration to itnow:=time.Now()
duration:=time.Duration(totalSeconds) *time.Secondfuture:=now.Add(duration)
// Return the future timereturnfuture, nil
}
The text was updated successfully, but these errors were encountered:
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Case 1 解析字符串,转为time.Time
Prompt
Result
The text was updated successfully, but these errors were encountered: