mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-13 09:12:28 +08:00
feat: add DaysBetween in datetime package
This commit is contained in:
@@ -411,3 +411,12 @@ func getCallerName() string {
|
|||||||
|
|
||||||
return fullName
|
return fullName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DaysBetween returns the number of days between two times.
|
||||||
|
// Play: todo
|
||||||
|
func DaysBetween(start, end time.Time) int {
|
||||||
|
duration := end.Sub(start)
|
||||||
|
days := int(duration.Hours() / 24)
|
||||||
|
|
||||||
|
return days
|
||||||
|
}
|
||||||
|
|||||||
@@ -409,16 +409,14 @@ func ExampleIsWeekend() {
|
|||||||
// false
|
// false
|
||||||
}
|
}
|
||||||
|
|
||||||
// func ExampleTrackFuncTime() {
|
func ExampleDaysBetween() {
|
||||||
// defer TrackFuncTime(time.Now())()
|
start := time.Date(2024, time.September, 1, 0, 0, 0, 0, time.UTC)
|
||||||
|
end := time.Date(2024, time.September, 10, 0, 0, 0, 0, time.UTC)
|
||||||
|
|
||||||
// var n int
|
result := DaysBetween(start, end)
|
||||||
// for i := 0; i < 5000000; i++ {
|
|
||||||
// n++
|
|
||||||
// }
|
|
||||||
|
|
||||||
// fmt.Println(1)
|
fmt.Println(result)
|
||||||
|
|
||||||
// // Output:
|
// Output:
|
||||||
// // 1
|
// 9
|
||||||
// }
|
}
|
||||||
|
|||||||
@@ -419,3 +419,46 @@ func TestTrackFuncTime(t *testing.T) {
|
|||||||
n++
|
n++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDaysBetween(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestDaysBetween")
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
start time.Time
|
||||||
|
end time.Time
|
||||||
|
expected int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
start: time.Date(2024, time.September, 1, 0, 0, 0, 0, time.UTC),
|
||||||
|
end: time.Date(2024, time.September, 10, 0, 0, 0, 0, time.UTC),
|
||||||
|
expected: 9,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
start: time.Date(2024, time.September, 10, 0, 0, 0, 0, time.UTC),
|
||||||
|
end: time.Date(2024, time.September, 1, 0, 0, 0, 0, time.UTC),
|
||||||
|
expected: -9,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
start: time.Date(2024, time.September, 1, 0, 0, 0, 0, time.UTC),
|
||||||
|
end: time.Date(2024, time.September, 1, 0, 0, 0, 0, time.UTC),
|
||||||
|
expected: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
start: time.Date(2024, time.January, 1, 0, 0, 0, 0, time.UTC),
|
||||||
|
end: time.Date(2024, time.December, 31, 0, 0, 0, 0, time.UTC),
|
||||||
|
expected: 365,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
start: time.Date(2024, time.March, 1, 0, 0, 0, 0, time.UTC),
|
||||||
|
end: time.Date(2024, time.March, 31, 0, 0, 0, 0, time.UTC),
|
||||||
|
expected: 30,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
result := DaysBetween(tt.start, tt.end)
|
||||||
|
assert.Equal(tt.expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ import (
|
|||||||
- [TimestampMicro](#TimestampMicro)
|
- [TimestampMicro](#TimestampMicro)
|
||||||
- [TimestampNano](#TimestampNano)
|
- [TimestampNano](#TimestampNano)
|
||||||
- [TrackFuncTime](#TrackFuncTime)
|
- [TrackFuncTime](#TrackFuncTime)
|
||||||
|
- [DaysBetween](#DaysBetween)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -1497,4 +1498,37 @@ func main() {
|
|||||||
|
|
||||||
fmt.Println(1) // Function main execution time: 1.460287ms
|
fmt.Println(1) // Function main execution time: 1.460287ms
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="DaysBetween">DaysBetween</span>
|
||||||
|
|
||||||
|
<p>返回两个日期之间的天数差。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func DaysBetween(start, end time.Time) int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="float:right;display:inline-block;">[运行]()</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
start := time.Date(2024, time.September, 1, 0, 0, 0, 0, time.UTC)
|
||||||
|
end := time.Date(2024, time.September, 10, 0, 0, 0, 0, time.UTC)
|
||||||
|
|
||||||
|
result := datetime.DaysBetween(start, end)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 9
|
||||||
|
}
|
||||||
```
|
```
|
||||||
@@ -1498,3 +1498,36 @@ func main() {
|
|||||||
fmt.Println(1) // Function main execution time: 1.460287ms
|
fmt.Println(1) // Function main execution time: 1.460287ms
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="DaysBetween">DaysBetween</span>
|
||||||
|
|
||||||
|
<p>Returns the number of days between two times.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func DaysBetween(start, end time.Time) int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:<span style="float:right;display:inline-block;">[Run]()</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
start := time.Date(2024, time.September, 1, 0, 0, 0, 0, time.UTC)
|
||||||
|
end := time.Date(2024, time.September, 10, 0, 0, 0, 0, time.UTC)
|
||||||
|
|
||||||
|
result := datetime.DaysBetween(start, end)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 9
|
||||||
|
}
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user