BeginOfWeek,EndOfWeek 的beginFrom和endWith改为必传 (#300)

This commit is contained in:
CyJaySong
2025-03-31 10:18:50 +08:00
committed by GitHub
parent 0ef45b533b
commit f4427b9fbc
4 changed files with 19 additions and 27 deletions

View File

@@ -281,12 +281,8 @@ func EndOfDay(t time.Time) time.Time {
// BeginOfWeek return beginning week, default week begin from Sunday.
// Play: https://go.dev/play/p/ynjoJPz7VNV
func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time {
var beginFromWeekday = time.Sunday
if len(beginFrom) > 0 {
beginFromWeekday = beginFrom[0]
}
y, m, d := t.AddDate(0, 0, int(beginFromWeekday-t.Weekday())).Date()
func BeginOfWeek(t time.Time, beginFrom time.Weekday) time.Time {
y, m, d := t.AddDate(0, 0, int(beginFrom-t.Weekday())).Date()
beginOfWeek := time.Date(y, m, d, 0, 0, 0, 0, t.Location())
if beginOfWeek.After(t) {
return beginOfWeek.AddDate(0, 0, -7)
@@ -296,12 +292,8 @@ func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time {
// EndOfWeek return end week time, default week end with Saturday.
// Play: https://go.dev/play/p/i08qKXD9flf
func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time {
var endWithWeekday = time.Saturday
if len(endWith) > 0 {
endWithWeekday = endWith[0]
}
y, m, d := t.AddDate(0, 0, int(endWithWeekday-t.Weekday())).Date()
func EndOfWeek(t time.Time, endWith time.Weekday) time.Time {
y, m, d := t.AddDate(0, 0, int(endWith-t.Weekday())).Date()
var endWithWeek = time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location())
if endWithWeek.Before(t) {
endWithWeek = endWithWeek.AddDate(0, 0, 7)

View File

@@ -299,23 +299,23 @@ func ExampleEndOfDay() {
func ExampleBeginOfWeek() {
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
result := BeginOfWeek(input)
result := BeginOfWeek(input, time.Monday)
fmt.Println(result)
// Output:
// 2023-01-08 00:00:00 +0000 UTC
// 2023-01-02 00:00:00 +0000 UTC
}
func ExampleEndOfWeek() {
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
result := EndOfWeek(input)
result := EndOfWeek(input, time.Sunday)
fmt.Println(result)
// Output:
// 2023-01-14 23:59:59.999999999 +0000 UTC
// 2023-01-08 23:59:59.999999999 +0000 UTC
}
func ExampleBeginOfMonth() {

View File

@@ -611,9 +611,9 @@ func TestBeginOfWeek(t *testing.T) {
assert := internal.NewAssert(t, "TestBeginOfWeek")
expected := time.Date(2022, 2, 13, 0, 0, 0, 0, time.Local)
expected := time.Date(2022, 2, 14, 0, 0, 0, 0, time.Local)
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
actual := BeginOfWeek(td)
actual := BeginOfWeek(td, time.Monday)
assert.Equal(expected, actual)
}
@@ -623,9 +623,9 @@ func TestEndOfWeek(t *testing.T) {
assert := internal.NewAssert(t, "TestEndOfWeek")
expected := time.Date(2022, 2, 19, 23, 59, 59, 999999999, time.Local)
expected := time.Date(2022, 2, 20, 23, 59, 59, 999999999, time.Local)
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
actual := EndOfWeek(td)
actual := EndOfWeek(td, time.Sunday)
assert.Equal(expected, actual)
}

View File

@@ -546,7 +546,7 @@ func main() {
<b>函数签名:</b>
```go
func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time
func BeginOfWeek(t time.Time, beginFrom time.Weekday) time.Time
```
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/ynjoJPz7VNV)</span></b>
@@ -562,12 +562,12 @@ import (
func main() {
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
result := datetime.BeginOfWeek(input)
result := datetime.BeginOfWeek(input, time.Monday)
fmt.Println(result)
// Output:
// 2023-01-08 00:00:00 +0000 UTC
// 2023-01-09 00:00:00 +0000 UTC
}
```
@@ -727,7 +727,7 @@ func main() {
fmt.Println(result)
// Output:
// 2023-01-08 23:59:59.999999999 +0000 UTC
// 2023-01-02 00:00:00 +0000 UTC
}
```
@@ -738,7 +738,7 @@ func main() {
<b>函数签名:</b>
```go
func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time
func EndOfWeek(t time.Time, endWith time.Weekday) time.Time
```
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/i08qKXD9flf)</span></b>
@@ -754,12 +754,12 @@ import (
func main() {
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
result := datetime.EndOfWeek(input)
result := datetime.EndOfWeek(input, time.Sunday)
fmt.Println(result)
// Output:
// 2023-01-14 23:59:59.999999999 +0000 UTC
// 2023-01-08 23:59:59.999999999 +0000 UTC
}
```