diff --git a/datetime/datetime.go b/datetime/datetime.go index d973aa3..ad3391b 100644 --- a/datetime/datetime.go +++ b/datetime/datetime.go @@ -72,6 +72,12 @@ func AddDay(t time.Time, day int64) time.Time { return t.Add(24 * time.Hour * time.Duration(day)) } +// AddYear add or sub year to the time. +// Play: todo +func AddYear(t time.Time, year int64) time.Time { + return t.Add(365 * 24 * time.Hour * time.Duration(year)) +} + // GetNowDate return format yyyy-mm-dd of current date. // Play: https://go.dev/play/p/PvfkPpcpBBf func GetNowDate() string { diff --git a/datetime/datetime_example_test.go b/datetime/datetime_example_test.go index ef1ef3c..d789886 100644 --- a/datetime/datetime_example_test.go +++ b/datetime/datetime_example_test.go @@ -57,6 +57,23 @@ func ExampleAddMinute() { // -2m0s } +func ExampleAddYear() { + now := time.Now() + + after1Year := AddYear(now, 1) + diff1 := after1Year.Sub(now) + + before1Year := AddYear(now, -1) + diff2 := before1Year.Sub(now) + + fmt.Println(diff1) + fmt.Println(diff2) + + // Output: + // 8760h0m0s + // -8760h0m0s +} + func ExampleGetNowDate() { result := GetNowDate() diff --git a/datetime/datetime_test.go b/datetime/datetime_test.go index 951a680..cee993b 100644 --- a/datetime/datetime_test.go +++ b/datetime/datetime_test.go @@ -7,6 +7,19 @@ import ( "github.com/duke-git/lancet/v2/internal" ) +func TestAddYear(t *testing.T) { + assert := internal.NewAssert(t, "TestAddDay") + + now := time.Now() + after2Years := AddYear(now, 1) + diff1 := after2Years.Sub(now) + assert.Equal(float64(8760), diff1.Hours()) + + before2Years := AddYear(now, -1) + diff2 := before2Years.Sub(now) + assert.Equal(float64(-8760), diff2.Hours()) +} + func TestAddDay(t *testing.T) { assert := internal.NewAssert(t, "TestAddDay") diff --git a/docs/datetime.md b/docs/datetime.md index defa89f..8a32db5 100644 --- a/docs/datetime.md +++ b/docs/datetime.md @@ -26,6 +26,7 @@ import ( - [AddDay](#AddDay) - [AddHour](#AddHour) - [AddMinute](#AddMinute) +- [AddYear](#AddYear) - [BeginOfMinute](#BeginOfMinute) - [BeginOfHour](#BeginOfHour) - [BeginOfDay](#BeginOfDay) @@ -199,6 +200,45 @@ func main() { } ``` +### AddYear + +

Add or sub year to the time.

+ +Signature: + +```go +func AddYear(t time.Time, year int64) time.Time +``` + +Example: + +```go +package main + +import ( + "fmt" + "time" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + now := time.Now() + + after1Year := AddYear(now, 1) + diff1 := after1Year.Sub(now) + + before1Year := AddYear(now, -1) + diff2 := before1Year.Sub(now) + + fmt.Println(diff1) + fmt.Println(diff2) + + // Output: + // 8760h0m0s + // -8760h0m0s +} +``` + ### BeginOfMinute

Return beginning minute time of day.

diff --git a/docs/datetime_zh-CN.md b/docs/datetime_zh-CN.md index c016855..c7b892c 100644 --- a/docs/datetime_zh-CN.md +++ b/docs/datetime_zh-CN.md @@ -25,6 +25,7 @@ import ( - [AddDay](#AddDay) - [AddHour](#AddHour) - [AddMinute](#AddMinute) +- [AddYear](#AddYear) - [BeginOfMinute](#BeginOfMinute) - [BeginOfHour](#BeginOfHour) - [BeginOfDay](#BeginOfDay) @@ -198,6 +199,45 @@ func main() { } ``` +### AddYear + +

将日期加/减年数。

+ +函数签名: + +```go +func AddYear(t time.Time, year int64) time.Time +``` + +示例: + +```go +package main + +import ( + "fmt" + "time" + "github.com/duke-git/lancet/v2/datetime" +) + +func main() { + now := time.Now() + + after1Year := AddYear(now, 1) + diff1 := after1Year.Sub(now) + + before1Year := AddYear(now, -1) + diff2 := before1Year.Sub(now) + + fmt.Println(diff1) + fmt.Println(diff2) + + // Output: + // 8760h0m0s + // -8760h0m0s +} +``` + ### BeginOfMinute

返回指定时间的分钟开始时间。

@@ -1095,4 +1135,4 @@ func main() { // true // false } -``` \ No newline at end of file +``` diff --git a/docs/stream.md b/docs/stream.md index c993bb3..20ee721 100644 --- a/docs/stream.md +++ b/docs/stream.md @@ -40,6 +40,7 @@ import ( - [ForEach](#ForEach) - [Reduce](#Reduce) - [FindFirst](#FindFirst) +- [FindLast](#FindLast) - [Max](#Max) - [Min](#Min) - [AllMatch](#AllMatch) @@ -667,6 +668,38 @@ func main() { } ``` +### FindLast + +

Returns the last element of this stream and true, or zero value and false if the stream is empty.

+ +Signature: + +```go +func (s stream[T]) FindLast() (T, bool) +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/stream" +) + +func main() { + original := FromSlice([]int{3, 2, 1}) + + result, ok := original.FindLast() + + fmt.Println(result) + fmt.Println(ok) + + // Output: + // 1 + // true +} +``` + ### Max

Returns the maximum element of this stream according to the provided less function. less fuction: a > b

diff --git a/docs/stream_zh-CN.md b/docs/stream_zh-CN.md index f227704..ad68b5e 100644 --- a/docs/stream_zh-CN.md +++ b/docs/stream_zh-CN.md @@ -40,6 +40,7 @@ import ( - [ForEach](#ForEach) - [Reduce](#Reduce) - [FindFirst](#FindFirst) +- [FindLast](#FindLast) - [Max](#Max) - [Min](#Min) - [AllMatch](#AllMatch) @@ -667,6 +668,38 @@ func main() { } ``` +### FindLast + +

返回此stream最后一个元素和true,如果stream为空,则返回零值和false。

+ +函数签名: + +```go +func (s stream[T]) FindLast() (T, bool) +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/stream" +) + +func main() { + original := FromSlice([]int{3, 2, 1}) + + result, ok := original.FindLast() + + fmt.Println(result) + fmt.Println(ok) + + // Output: + // 1 + // true +} +``` + ### Max

根据提供的less函数返回stream的最大元素。less 函数: a > b

diff --git a/stream/stream.go b/stream/stream.go index 23734a3..cc6b471 100644 --- a/stream/stream.go +++ b/stream/stream.go @@ -295,6 +295,18 @@ func (s stream[T]) FindFirst() (T, bool) { return s.source[0], true } +// FindLast returns the last element of this stream and true, or zero value and false if the stream is empty. +// Play: https://go.dev/play/p/9xEf0-6C1e3 +func (s stream[T]) FindLast() (T, bool) { + var result T + + if s.source == nil || len(s.source) == 0 { + return result, false + } + + return s.source[len(s.source)-1], true +} + // Reverse returns a stream whose elements are reverse order of given stream. // Play: https://go.dev/play/p/A8_zkJnLHm4 func (s stream[T]) Reverse() stream[T] { diff --git a/stream/stream_example_test.go b/stream/stream_example_test.go index 4842071..b854dfd 100644 --- a/stream/stream_example_test.go +++ b/stream/stream_example_test.go @@ -290,6 +290,19 @@ func ExampleStream_FindFirst() { // true } +func ExampleStream_FindLast() { + original := FromSlice([]int{3, 2, 1}) + + result, ok := original.FindLast() + + fmt.Println(result) + fmt.Println(ok) + + // Output: + // 1 + // true +} + func ExampleStream_Reverse() { original := FromSlice([]int{1, 2, 3}) diff --git a/stream/stream_test.go b/stream/stream_test.go index 6523cce..b749f5d 100644 --- a/stream/stream_test.go +++ b/stream/stream_test.go @@ -273,6 +273,24 @@ func TestStream_FindFirst(t *testing.T) { assert.Equal(true, ok) } +func TestStream_FindLast(t *testing.T) { + assert := internal.NewAssert(t, "TestStream_FindLast") + + stream := FromSlice([]int{3, 2, 1}) + + result, ok := stream.FindLast() + + assert.Equal(1, result) + assert.Equal(true, ok) + + stream2 := FromSlice([]int{}) + + result, ok = stream2.FindLast() + + assert.Equal(0, result) + assert.Equal(false, ok) +} + func TestStream_Reverse(t *testing.T) { assert := internal.NewAssert(t, "TestStream_Reverse")