busgo / forest

分布式任务调度平台,分布式,任务调度,schedule,scheduler
http://122.51.106.217:6579
Apache License 2.0
279 stars 54 forks source link

关于util.go TimeSubDays方法的问题 #8

Closed reggiepy closed 3 years ago

reggiepy commented 3 years ago

你好,看了下源码,有个问题想请教下你。这里的 if t1.Location().String() != t2.Location().String() 只有时间相同的时候才能走到下面, 时间相同的时候if hours <= 0 条件和会生效,返回-1. 程序本意就是这样的吗?不太明白这个函数的作用了。后面也有用到这个工具,希望能解答下,非常感谢!

func TimeSubDays(t1, t2 time.Time) int {

    if t1.Location().String() != t2.Location().String() {
        return -1
    }
    hours := t1.Sub(t2).Hours()

    if hours <= 0 {
        return -1
    }
    // sub hours less than 24
    if hours < 24 {
        // may same day
        t1y, t1m, t1d := t1.Date()
        t2y, t2m, t2d := t2.Date()
        isSameDay := (t1y == t2y && t1m == t2m && t1d == t2d)

        if isSameDay {

            return 0
        } else {
            return 1
        }

    } else { // equal or more than 24

        if (hours/24)-float64(int(hours/24)) == 0 { // just 24's times
            return int(hours / 24)
        } else { // more than 24 hours
            return int(hours/24) + 1
        }
    }

}
reggiepy commented 3 years ago

明白了明白了