mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
BeginOfWeek,EndOfWeek 的beginFrom和endWith改为必传 (#300)
This commit is contained in:
@@ -281,12 +281,8 @@ func EndOfDay(t time.Time) time.Time {
|
|||||||
|
|
||||||
// BeginOfWeek return beginning week, default week begin from Sunday.
|
// BeginOfWeek return beginning week, default week begin from Sunday.
|
||||||
// Play: https://go.dev/play/p/ynjoJPz7VNV
|
// Play: https://go.dev/play/p/ynjoJPz7VNV
|
||||||
func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time {
|
func BeginOfWeek(t time.Time, beginFrom time.Weekday) time.Time {
|
||||||
var beginFromWeekday = time.Sunday
|
y, m, d := t.AddDate(0, 0, int(beginFrom-t.Weekday())).Date()
|
||||||
if len(beginFrom) > 0 {
|
|
||||||
beginFromWeekday = beginFrom[0]
|
|
||||||
}
|
|
||||||
y, m, d := t.AddDate(0, 0, int(beginFromWeekday-t.Weekday())).Date()
|
|
||||||
beginOfWeek := time.Date(y, m, d, 0, 0, 0, 0, t.Location())
|
beginOfWeek := time.Date(y, m, d, 0, 0, 0, 0, t.Location())
|
||||||
if beginOfWeek.After(t) {
|
if beginOfWeek.After(t) {
|
||||||
return beginOfWeek.AddDate(0, 0, -7)
|
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.
|
// EndOfWeek return end week time, default week end with Saturday.
|
||||||
// Play: https://go.dev/play/p/i08qKXD9flf
|
// Play: https://go.dev/play/p/i08qKXD9flf
|
||||||
func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time {
|
func EndOfWeek(t time.Time, endWith time.Weekday) time.Time {
|
||||||
var endWithWeekday = time.Saturday
|
y, m, d := t.AddDate(0, 0, int(endWith-t.Weekday())).Date()
|
||||||
if len(endWith) > 0 {
|
|
||||||
endWithWeekday = endWith[0]
|
|
||||||
}
|
|
||||||
y, m, d := t.AddDate(0, 0, int(endWithWeekday-t.Weekday())).Date()
|
|
||||||
var endWithWeek = time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location())
|
var endWithWeek = time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location())
|
||||||
if endWithWeek.Before(t) {
|
if endWithWeek.Before(t) {
|
||||||
endWithWeek = endWithWeek.AddDate(0, 0, 7)
|
endWithWeek = endWithWeek.AddDate(0, 0, 7)
|
||||||
|
|||||||
@@ -299,23 +299,23 @@ func ExampleEndOfDay() {
|
|||||||
func ExampleBeginOfWeek() {
|
func ExampleBeginOfWeek() {
|
||||||
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
|
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
|
||||||
|
|
||||||
result := BeginOfWeek(input)
|
result := BeginOfWeek(input, time.Monday)
|
||||||
|
|
||||||
fmt.Println(result)
|
fmt.Println(result)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 2023-01-08 00:00:00 +0000 UTC
|
// 2023-01-02 00:00:00 +0000 UTC
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleEndOfWeek() {
|
func ExampleEndOfWeek() {
|
||||||
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
|
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
|
||||||
|
|
||||||
result := EndOfWeek(input)
|
result := EndOfWeek(input, time.Sunday)
|
||||||
|
|
||||||
fmt.Println(result)
|
fmt.Println(result)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 2023-01-14 23:59:59.999999999 +0000 UTC
|
// 2023-01-08 23:59:59.999999999 +0000 UTC
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleBeginOfMonth() {
|
func ExampleBeginOfMonth() {
|
||||||
|
|||||||
@@ -611,9 +611,9 @@ func TestBeginOfWeek(t *testing.T) {
|
|||||||
|
|
||||||
assert := internal.NewAssert(t, "TestBeginOfWeek")
|
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)
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
actual := BeginOfWeek(td)
|
actual := BeginOfWeek(td, time.Monday)
|
||||||
|
|
||||||
assert.Equal(expected, actual)
|
assert.Equal(expected, actual)
|
||||||
}
|
}
|
||||||
@@ -623,9 +623,9 @@ func TestEndOfWeek(t *testing.T) {
|
|||||||
|
|
||||||
assert := internal.NewAssert(t, "TestEndOfWeek")
|
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)
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
actual := EndOfWeek(td)
|
actual := EndOfWeek(td, time.Sunday)
|
||||||
|
|
||||||
assert.Equal(expected, actual)
|
assert.Equal(expected, actual)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -546,7 +546,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```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>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/ynjoJPz7VNV)</span></b>
|
||||||
@@ -562,12 +562,12 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
|
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)
|
fmt.Println(result)
|
||||||
|
|
||||||
// Output:
|
// 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)
|
fmt.Println(result)
|
||||||
|
|
||||||
// Output:
|
// 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>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```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>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/i08qKXD9flf)</span></b>
|
||||||
@@ -754,12 +754,12 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.UTC)
|
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)
|
fmt.Println(result)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 2023-01-14 23:59:59.999999999 +0000 UTC
|
// 2023-01-08 23:59:59.999999999 +0000 UTC
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user