时间格式化和从字符串转换

我对 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)

这就是这个项目的内容。如果不是常规服务,它会执行完全相同的操作,但会跳过确定要查看的时间。


目前这并没有建立,因为我在应该使用时间的地方使用字符串,我希望得到一些指导、一些示例和更正,以了解如何正确使用时间来实现我想要做的事情。到目前为止,我相信我对这种流动方式的逻辑是正确的,但我无法编译它。


慕莱坞森
浏览 169回答 1
1回答

噜噜哒

由于time.Time是在特定位置的特定时刻,因此将其用于没有日期或时区的任意时钟时间可能会很尴尬。您真正要寻找的是自 00:00 以来的持续时间,因此 atime.Duration更有意义。例如,您甚至可以根据自午夜以来的分钟数使用自己的类型,但如果您使用 a,time.Duration您将能够time.Time更轻松地安排时间。这是一个示例函数,可将您的时钟时间和分钟解析为 time.Durationfunc ParseTime(t string) (time.Duration, error) {&nbsp; &nbsp; var mins, hours int&nbsp; &nbsp; var err error&nbsp; &nbsp; parts := strings.SplitN(t, ":", 2)&nbsp; &nbsp; switch len(parts) {&nbsp; &nbsp; case 1:&nbsp; &nbsp; &nbsp; &nbsp; mins, err = strconv.Atoi(parts[0])&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0, err&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; case 2:&nbsp; &nbsp; &nbsp; &nbsp; hours, err = strconv.Atoi(parts[0])&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0, err&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; mins, err = strconv.Atoi(parts[1])&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0, err&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; return 0, fmt.Errorf("invalid time: %s", t)&nbsp; &nbsp; }&nbsp; &nbsp; if mins > 59 || mins < 0 || hours > 23 || hours < 0 {&nbsp; &nbsp; &nbsp; &nbsp; return 0, fmt.Errorf("invalid time: %s", t)&nbsp; &nbsp; }&nbsp; &nbsp; return time.Duration(hours)*time.Hour + time.Duration(mins)*time.Minute, nil}您也可以将其与您自己的类型结合使用,使用相同的底层 int64 类型,以便您可以轻松地格式化 time.Duration 并添加您自己的方法。如果从 ParseTime返回 aTime或 atime.Duration更方便,这取决于您。这里的两个是可以直接转换的。type Time int64func (t Time) Hours() int {&nbsp; &nbsp; return int(time.Duration(t) / time.Hour)}func (t Time) Minutes() int {&nbsp; &nbsp; return int((time.Duration(t) % time.Hour) / time.Minute)}func (t Time) String() string {&nbsp; &nbsp; return fmt.Sprintf("%02d:%02d", t.Hours(), t.Minutes())}你可以像这样组合这些:http : //play.golang.org/p/E679m2wlUO
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go