Compare commits

...
2 Commits
7 changed files with 33 additions and 40 deletions
+2 -2
View File
@@ -535,7 +535,7 @@ import "github.com/duke-git/lancet/v2/datetime"
[[play](https://go.dev/play/p/94m_UT6cWs9)] [[play](https://go.dev/play/p/94m_UT6cWs9)]
- **<big>BeginOfWeek</big>** : return the date time at the begin of week of specific date. - **<big>BeginOfWeek</big>** : return the date time at the begin of week of specific date.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/datetime.md#BeginOfWeek)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/datetime.md#BeginOfWeek)]
[[play](https://go.dev/play/p/ynjoJPz7VNV)] [[play](https://go.dev/play/p/DCHdcL6gnfV)]
- **<big>BeginOfMonth</big>** : return the date time at the begin of month of specific date. - **<big>BeginOfMonth</big>** : return the date time at the begin of month of specific date.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/datetime.md#BeginOfMonth)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/datetime.md#BeginOfMonth)]
[[play](https://go.dev/play/p/bWXVFsmmzwL)] [[play](https://go.dev/play/p/bWXVFsmmzwL)]
@@ -553,7 +553,7 @@ import "github.com/duke-git/lancet/v2/datetime"
[[play](https://go.dev/play/p/eMBOvmq5Ih1)] [[play](https://go.dev/play/p/eMBOvmq5Ih1)]
- **<big>EndOfWeek</big>** : return the date time at the end of week of specific date. - **<big>EndOfWeek</big>** : return the date time at the end of week of specific date.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/datetime.md#EndOfWeek)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/datetime.md#EndOfWeek)]
[[play](https://go.dev/play/p/i08qKXD9flf)] [[play](https://go.dev/play/p/mGSA162YgX9)]
- **<big>EndOfMonth</big>** : return the date time at the end of month of specific date. - **<big>EndOfMonth</big>** : return the date time at the end of month of specific date.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/datetime.md#EndOfMonth)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/datetime.md#EndOfMonth)]
[[play](https://go.dev/play/p/_GWh10B3Nqi)] [[play](https://go.dev/play/p/_GWh10B3Nqi)]
+2 -2
View File
@@ -536,7 +536,7 @@ import "github.com/duke-git/lancet/v2/datetime"
[[play](https://go.dev/play/p/94m_UT6cWs9)] [[play](https://go.dev/play/p/94m_UT6cWs9)]
- **<big>BeginOfWeek</big>** : 返回指定时间的每周开始时间,默认开始时间星期日。 - **<big>BeginOfWeek</big>** : 返回指定时间的每周开始时间,默认开始时间星期日。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/datetime.md#BeginOfWeek)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/datetime.md#BeginOfWeek)]
[[play](https://go.dev/play/p/ynjoJPz7VNV)] [[play](https://go.dev/play/p/DCHdcL6gnfV)]
- **<big>BeginOfMonth</big>** : 返回指定时间的当月开始时间。 - **<big>BeginOfMonth</big>** : 返回指定时间的当月开始时间。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/datetime.md#BeginOfMonth)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/datetime.md#BeginOfMonth)]
[[play](https://go.dev/play/p/bWXVFsmmzwL)] [[play](https://go.dev/play/p/bWXVFsmmzwL)]
@@ -554,7 +554,7 @@ import "github.com/duke-git/lancet/v2/datetime"
[[play](https://go.dev/play/p/eMBOvmq5Ih1)] [[play](https://go.dev/play/p/eMBOvmq5Ih1)]
- **<big>EndOfWeek</big>** : 返回指定时间的星期结束时间,默认结束时间星期六。 - **<big>EndOfWeek</big>** : 返回指定时间的星期结束时间,默认结束时间星期六。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/datetime.md#EndOfWeek)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/datetime.md#EndOfWeek)]
[[play](https://go.dev/play/p/i08qKXD9flf)] [[play](https://go.dev/play/p/mGSA162YgX9)]
- **<big>EndOfMonth</big>** : 返回指定时间的月份结束时间。 - **<big>EndOfMonth</big>** : 返回指定时间的月份结束时间。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/datetime.md#EndOfMonth)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/datetime.md#EndOfMonth)]
[[play](https://go.dev/play/p/_GWh10B3Nqi)] [[play](https://go.dev/play/p/_GWh10B3Nqi)]
+6 -14
View File
@@ -280,13 +280,9 @@ 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/DCHdcL6gnfV
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)
@@ -295,13 +291,9 @@ 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/mGSA162YgX9
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)
+4 -4
View File
@@ -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() {
+4 -4
View File
@@ -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)
} }
+8 -8
View File
@@ -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,10 +738,10 @@ 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/mGSA162YgX9)</span></b>
```go ```go
package main package main
@@ -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
} }
``` ```
+7 -6
View File
@@ -551,7 +551,7 @@ func main() {
func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time
``` ```
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/ynjoJPz7VNV)</span></b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/DCHdcL6gnfV)</span></b>
```go ```go
package main package main
@@ -564,12 +564,13 @@ 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-02 00:00:00 +0000 UTC
} }
``` ```
@@ -743,7 +744,7 @@ func main() {
func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time
``` ```
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/i08qKXD9flf)</span></b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/mGSA162YgX9)</span></b>
```go ```go
package main package main
@@ -756,12 +757,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
} }
``` ```