mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
增加判断某个日期是一年当中的第几天的方法 (#103)
* 增加判断某个日期是一年当中的第几天的方法 * 修改下函数名字 --------- Co-authored-by: huangxingming <huangxingming@kezaihui.com>
This commit is contained in:
@@ -237,3 +237,10 @@ func BetweenSeconds(t1 time.Time, t2 time.Time) int64 {
|
||||
index := t2.Unix() - t1.Unix()
|
||||
return index
|
||||
}
|
||||
|
||||
func DayOfYear(t1 time.Time) int {
|
||||
y, m, d := t1.Date()
|
||||
firstDay := time.Date(y, 1, 1, 0, 0, 0, 0, t1.Location())
|
||||
nowDate := time.Date(y, m, d, 0, 0, 0, 0, t1.Location())
|
||||
return int(nowDate.Sub(firstDay).Hours() / 24)
|
||||
}
|
||||
|
||||
@@ -271,3 +271,18 @@ func TestIsLeapYear(t *testing.T) {
|
||||
assert.Equal(true, result1)
|
||||
assert.Equal(false, result2)
|
||||
}
|
||||
|
||||
func TestDayOfYear(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestDayOfYear")
|
||||
date := time.Date(2023, 02, 01, 1, 1, 1, 0, time.Local)
|
||||
result := DayOfYear(date)
|
||||
assert.Equal(31, result)
|
||||
|
||||
date1 := time.Date(2023, 01, 02, 1, 1, 1, 0, time.Local)
|
||||
result1 := DayOfYear(date1)
|
||||
assert.Equal(1, result1)
|
||||
|
||||
date3 := time.Date(2023, 01, 01, 1, 1, 1, 0, time.Local)
|
||||
result2 := DayOfYear(date3)
|
||||
assert.Equal(0, result2)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user