mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
doc: add doc for DayOfYear
This commit is contained in:
@@ -238,9 +238,12 @@ func BetweenSeconds(t1 time.Time, t2 time.Time) int64 {
|
|||||||
return index
|
return index
|
||||||
}
|
}
|
||||||
|
|
||||||
func DayOfYear(t1 time.Time) int {
|
// DayOfYear returns which day of the year the parameter date `t` is.
|
||||||
y, m, d := t1.Date()
|
// Play: todo
|
||||||
firstDay := time.Date(y, 1, 1, 0, 0, 0, 0, t1.Location())
|
func DayOfYear(t time.Time) int {
|
||||||
nowDate := time.Date(y, m, d, 0, 0, 0, 0, t1.Location())
|
y, m, d := t.Date()
|
||||||
|
firstDay := time.Date(y, 1, 1, 0, 0, 0, 0, t.Location())
|
||||||
|
nowDate := time.Date(y, m, d, 0, 0, 0, 0, t.Location())
|
||||||
|
|
||||||
return int(nowDate.Sub(firstDay).Hours() / 24)
|
return int(nowDate.Sub(firstDay).Hours() / 24)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -369,3 +369,23 @@ func ExampleBetweenSeconds() {
|
|||||||
// 86400
|
// 86400
|
||||||
// -86400
|
// -86400
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleDayOfYear() {
|
||||||
|
date1 := time.Date(2023, 02, 01, 1, 1, 1, 0, time.Local)
|
||||||
|
result1 := DayOfYear(date1)
|
||||||
|
|
||||||
|
date2 := time.Date(2023, 01, 02, 1, 1, 1, 0, time.Local)
|
||||||
|
result2 := DayOfYear(date2)
|
||||||
|
|
||||||
|
date3 := time.Date(2023, 01, 01, 1, 1, 1, 0, time.Local)
|
||||||
|
result3 := DayOfYear(date3)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 31
|
||||||
|
// 1
|
||||||
|
// 0
|
||||||
|
}
|
||||||
|
|||||||
@@ -274,15 +274,15 @@ func TestIsLeapYear(t *testing.T) {
|
|||||||
|
|
||||||
func TestDayOfYear(t *testing.T) {
|
func TestDayOfYear(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestDayOfYear")
|
assert := internal.NewAssert(t, "TestDayOfYear")
|
||||||
date := time.Date(2023, 02, 01, 1, 1, 1, 0, time.Local)
|
date1 := 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)
|
result1 := DayOfYear(date1)
|
||||||
assert.Equal(1, result1)
|
assert.Equal(31, result1)
|
||||||
|
|
||||||
|
date2 := time.Date(2023, 01, 02, 1, 1, 1, 0, time.Local)
|
||||||
|
result2 := DayOfYear(date2)
|
||||||
|
assert.Equal(1, result2)
|
||||||
|
|
||||||
date3 := time.Date(2023, 01, 01, 1, 1, 1, 0, time.Local)
|
date3 := time.Date(2023, 01, 01, 1, 1, 1, 0, time.Local)
|
||||||
result2 := DayOfYear(date3)
|
result3 := DayOfYear(date3)
|
||||||
assert.Equal(0, result2)
|
assert.Equal(0, result3)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ import (
|
|||||||
- [ToIso8601](#ToIso8601)
|
- [ToIso8601](#ToIso8601)
|
||||||
- [IsLeapYear](#IsLeapYear)
|
- [IsLeapYear](#IsLeapYear)
|
||||||
- [BetweenSeconds](#BetweenSeconds)
|
- [BetweenSeconds](#BetweenSeconds)
|
||||||
|
- [DayOfYear](#DayOfYear)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -1175,3 +1176,45 @@ func main() {
|
|||||||
// -86400
|
// -86400
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="DayOfYear">DayOfYear</span>
|
||||||
|
|
||||||
|
<p>Returns which day of the year the parameter date `t` is.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func DayOfYear(t time.Time) int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
date1 := time.Date(2023, 02, 01, 1, 1, 1, 0, time.Local)
|
||||||
|
result1 := datetime.DayOfYear(date1)
|
||||||
|
|
||||||
|
date2 := time.Date(2023, 01, 02, 1, 1, 1, 0, time.Local)
|
||||||
|
result2 := datetime.DayOfYear(date2)
|
||||||
|
|
||||||
|
date3 := time.Date(2023, 01, 01, 1, 1, 1, 0, time.Local)
|
||||||
|
result3 := datetime.DayOfYear(date3)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 31
|
||||||
|
// 1
|
||||||
|
// 0
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ import (
|
|||||||
- [ToIso8601](#ToIso8601)
|
- [ToIso8601](#ToIso8601)
|
||||||
- [IsLeapYear](#IsLeapYear)
|
- [IsLeapYear](#IsLeapYear)
|
||||||
- [BetweenSeconds](#BetweenSeconds)
|
- [BetweenSeconds](#BetweenSeconds)
|
||||||
|
- [DayOfYear](#DayOfYear)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -1174,3 +1175,44 @@ func main() {
|
|||||||
// -86400
|
// -86400
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="DayOfYear">DayOfYear</span>
|
||||||
|
|
||||||
|
<p>返回参数日期是一年中的第几天。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func DayOfYear(t time.Time) int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
date1 := time.Date(2023, 02, 01, 1, 1, 1, 0, time.Local)
|
||||||
|
result1 := datetime.DayOfYear(date1)
|
||||||
|
|
||||||
|
date2 := time.Date(2023, 01, 02, 1, 1, 1, 0, time.Local)
|
||||||
|
result2 := datetime.DayOfYear(date2)
|
||||||
|
|
||||||
|
date3 := time.Date(2023, 01, 01, 1, 1, 1, 0, time.Local)
|
||||||
|
result3 := datetime.DayOfYear(date3)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 31
|
||||||
|
// 1
|
||||||
|
// 0
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user