golang 獲取AB兩個日期相差多少天

今天碰到了要求兩個日期間相差多少天,兩個不同日期的,相差一秒都算一天.
標(biāo)準(zhǔn)庫中沒有相應(yīng)的實現(xiàn),自己寫了個.

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
        }
    }

}

test,直接在main中寫的

layout := "2006-01-02 15:04:05"

    // just one second
    t1, _ := time.Parse(layout, "2007-01-02 23:59:59")
    t2, _ := time.Parse(layout, "2007-01-03 00:00:00")
    if timeSub(t2, t1) != 1 {
        panic("one second but different day should return 1")
    }

    // just one day
    t1, _ = time.Parse(layout, "2007-01-02 23:59:59")
    t2, _ = time.Parse(layout, "2007-01-03 23:59:59")
    if timeSub(t2, t1) != 1 {
        panic("just one day should return 1")
    }

    // more than one day
    t1, _ = time.Parse(layout, "2007-01-02 23:59:59")
    t2, _ = time.Parse(layout, "2007-01-04 00:00:00")
    if timeSub(t2, t1) != 2 {
        panic("just one day should return 2")
    }
    // just 3 day
    t1, _ = time.Parse(layout, "2007-01-02 00:00:00")
    t2, _ = time.Parse(layout, "2007-01-05 00:00:00")
    if timeSub(t2, t1) != 3 {
        panic("just 3 day should return 3")
    }

    // different month
    t1, _ = time.Parse(layout, "2007-01-02 00:00:00")
    t2, _ = time.Parse(layout, "2007-02-02 00:00:00")
    if timeSub(t2, t1) != 31 {
        fmt.Println(timeSub(t2, t1))
        panic("just one month:31 days should return 31")
    }

    // 29 days in 2mth
    t1, _ = time.Parse(layout, "2000-02-01 00:00:00")
    t2, _ = time.Parse(layout, "2000-03-01 00:00:00")
    if timeSub(t2, t1) != 29 {
        fmt.Println(timeSub(t2, t1))
        panic("just one month:29 days should return 29")
    }

更正修訂


經(jīng)2樓空靈一月指出,上面的代碼有邏輯錯誤,而且寫的時候是參照另外一個人代碼寫的?,F(xiàn)用time.Truncate 和 Time.Sub來做,測試追加2樓的case,如下:

package test

import (
    "fmt"
    "testing"
    "time"
)

func timeSub(t1, t2 time.Time) int {
    t1 = t1.UTC().Truncate(24 * time.Hour)
    t2 = t2.UTC().Truncate(24 * time.Hour)
    return int(t1.Sub(t2).Hours() / 24)
}

func TestTime(t *testing.T) {
    layout := "2006-01-02 15:04:05"

    // just one second
    t1, _ := time.Parse(layout, "2007-01-02 23:59:59")
    t2, _ := time.Parse(layout, "2007-01-03 00:00:00")
    if timeSub(t2, t1) != 1 {
        panic("one second but different day should return 1")
    }

    // just one day
    t1, _ = time.Parse(layout, "2007-01-02 23:59:59")
    t2, _ = time.Parse(layout, "2007-01-03 23:59:59")
    if timeSub(t2, t1) != 1 {
        panic("just one day should return 1")
    }

    t1, _ = time.Parse(layout, "2017-09-01 10:00:00")
    t2, _ = time.Parse(layout, "2017-09-02 11:00:00")
    if timeSub(t2, t1) != 1 {
        panic("just one day should return 1")
    }

    // more than one day
    t1, _ = time.Parse(layout, "2007-01-02 23:59:59")
    t2, _ = time.Parse(layout, "2007-01-04 00:00:00")
    if timeSub(t2, t1) != 2 {
        panic("just one day should return 2")
    }
    // just 3 day
    t1, _ = time.Parse(layout, "2007-01-02 00:00:00")
    t2, _ = time.Parse(layout, "2007-01-05 00:00:00")
    if timeSub(t2, t1) != 3 {
        panic("just 3 day should return 3")
    }

    // different month
    t1, _ = time.Parse(layout, "2007-01-02 00:00:00")
    t2, _ = time.Parse(layout, "2007-02-02 00:00:00")
    if timeSub(t2, t1) != 31 {
        fmt.Println(timeSub(t2, t1))
        panic("just one month:31 days should return 31")
    }

    // 29 days in 2mth
    t1, _ = time.Parse(layout, "2000-02-01 00:00:00")
    t2, _ = time.Parse(layout, "2000-03-01 00:00:00")
    if timeSub(t2, t1) != 29 {
        fmt.Println(timeSub(t2, t1))
        panic("just one month:29 days should return 29")
    }
}

再次更正修訂
frfluter 指出個bug,Truncate方法用的是絕對時間,如果給出的時間為本地時間,會存在時區(qū),會出現(xiàn)不同時間化為同一天的情況

func timeSub(t1, t2 time.Time) int {
    t1 = time.Date(t1.Year(), t1.Month(), t1.Day(), 0, 0, 0, 0, time.Local)
    t2 = time.Date(t2.Year(), t2.Month(), t2.Day(), 0, 0, 0, 0, time.Local)

    return int(t1.Sub(t2).Hours() / 24)
}

新增測試case:
    t1 = time.Date(2018, 1, 10, 0, 0, 1, 100, time.Local)
    t2 = time.Date(2018, 1, 9, 23, 59, 22, 100, time.Local)
    if timeSub(t1, t2) != 1 {
        panic(fmt.Sprintf("just one day: should return 1 but got %v", timeSub(t1, t2)))
    }

    t1 = time.Date(2018, 1, 10, 0, 0, 1, 100, time.UTC)
    t2 = time.Date(2018, 1, 9, 23, 59, 22, 100, time.UTC)
    if timeSub(t1, t2) != 1 {
        panic(fmt.Sprintf("just one day: should return 1 but got %v", timeSub(t1, t2)))
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容