1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

merge main

This commit is contained in:
dudaodong
2023-05-18 10:32:36 +08:00
10 changed files with 226 additions and 1 deletions

View File

@@ -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 {

View File

@@ -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()

View File

@@ -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")

View File

@@ -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() {
}
```
### <span id="AddYear">AddYear</span>
<p>Add or sub year to the time.</p>
<b>Signature:</b>
```go
func AddYear(t time.Time, year int64) time.Time
```
<b>Example:</b>
```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
}
```
### <span id="BeginOfMinute">BeginOfMinute</span>
<p>Return beginning minute time of day.</p>

View File

@@ -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() {
}
```
### <span id="AddYear">AddYear</span>
<p>将日期加/减年数。</p>
<b>函数签名:</b>
```go
func AddYear(t time.Time, year int64) time.Time
```
<b>示例:</b>
```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
}
```
### <span id="BeginOfMinute">BeginOfMinute</span>
<p>返回指定时间的分钟开始时间。</p>
@@ -1095,4 +1135,4 @@ func main() {
// true
// false
}
```
```

View File

@@ -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() {
}
```
### <span id="FindLast">FindLast</span>
<p>Returns the last element of this stream and true, or zero value and false if the stream is empty.</p>
<b>Signature:</b>
```go
func (s stream[T]) FindLast() (T, bool)
```
<b>Example:</b>
```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
}
```
### <span id="Max">Max</span>
<p>Returns the maximum element of this stream according to the provided less function. less fuction: a > b</p>

View File

@@ -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() {
}
```
### <span id="FindLast">FindLast</span>
<p>返回此stream最后一个元素和true如果stream为空则返回零值和false。</p>
<b>函数签名:</b>
```go
func (s stream[T]) FindLast() (T, bool)
```
<b>示例:</b>
```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
}
```
### <span id="Max">Max</span>
<p>根据提供的less函数返回stream的最大元素。less 函数: a > b</p>

View File

@@ -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] {

View File

@@ -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})

View File

@@ -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")