I'm quite new to Go and mainly work in C#. I am currently working on something that will be used as a command to get the next bus. Currently it does not compile because I am struggling with understanding how to use the time
package in Go.
I have an array of strings formatted as times when it is scheduled and another for the minutes when it is a regular service like so (there are many more times these are just an example:
var ScheduledTimes = []string{"06:34", "06:54", "17:09", "17:19"}
var RegularTimes = []string{"05", "50"}
So what I am currently doing is getting the current time and checking if it is in regular service by doing this:
func isWithinRegularService(check time.Time) bool {
RegularStart, err := time.Parse(time.Kitchen, "10:05")
RegularEnd, err := time.Parse(time.Kitchen, "13:52")
return check.After(RegularStart) && check.Before(RegularEnd)
}
time := time.Now().UTC().Format("15:04")
if isWithinRegularService(time) { }
If it is in regular service, I will then determine what hour needs looking at (i.e this one or is the next service within the next hour)
if time.Minutes < 5 && time.Minutes >= 0 {
RegularTimes[0] = fmt.Sprintf("%s%s", time.Hour+1, RegularTimes[0])
RegularTimes[1] = fmt.Sprintf("%s%s", time.Hour+1, RegularTimes[1])
RegularTimes[2] = fmt.Sprintf("%s%s", time.Hour+1, RegularTimes[2])
RegularTimes[3] = fmt.Sprintf("%s%s", time.Hour+1, RegularTimes[3])
} else {
RegularTimes[0] = fmt.Sprintf("%s%s", time.Hour, RegularTimes[0])
RegularTimes[1] = fmt.Sprintf("%s%s", time.Hour, RegularTimes[1])
RegularTimes[2] = fmt.Sprintf("%s%s", time.Hour, RegularTimes[2])
RegularTimes[3] = fmt.Sprintf("%s%s", time.Hour, RegularTimes[3])
}
Before I pass that array to another func to give me the times to check between
type BusTime struct {
betweenStart string
betweenEnd string
}
func getBusTime(busTimes []array, iteration int) BusTime {
var timesToReturn BusTime
if iteration == busTimes.Count()-1 {
timesToReturn.betweenStart = busTimes[iteration]
timesToReturn.betweenEnd = busTimes[0]
} else {
timesToReturn.betweenStart = busTimes[iteration]
timesToReturn.betweenEnd = busTimes[iteration+1]
}
return timesToReturn
}
busTimes := getBusTime(quaylinkRegularTimes, i)
And lastly I then check if the time provided is between the times to compare:
check.After(start) && check.Before(end)
That is about it for this project. If it is not with regular service it will do the exact same but skip out determining what hour to look at.
Currently this does not build because I am using strings where I should be using times, I was hoping to get some guidance, some examples, and corrections on how to use times correctly to achieve what I'm looking to do. So far I believe my logic to the way this will flow is correct, but I'm stuck getting it to compile.