我对 Go 很陌生,主要在 C# 中工作。我目前正在研究一些将用作获取下一辆巴士的命令的东西。目前它无法编译,因为我正在努力理解如何time在 Go 中使用该包。
我有一个字符串数组,格式化为预定时间,另一个格式化为常规服务时的分钟数(还有很多次,这些只是一个例子:
var ScheduledTimes = []string{"06:34", "06:54", "17:09", "17:19"}
var RegularTimes = []string{"05", "50"}
所以我目前正在做的是获取当前时间并通过执行以下操作检查它是否在常规服务中:
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 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])
}
在我将该数组传递给另一个 func 之前给我检查的时间
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)
最后,我检查提供的时间是否在要比较的时间之间:
check.After(start) && check.Before(end)
这就是这个项目的内容。如果不是常规服务,它会执行完全相同的操作,但会跳过确定要查看的时间。
目前这并没有建立,因为我在应该使用时间的地方使用字符串,我希望得到一些指导、一些示例和更正,以了解如何正确使用时间来实现我想要做的事情。到目前为止,我相信我对这种流动方式的逻辑是正确的,但我无法编译它。
噜噜哒
相关分类