Skip to content

Commit

Permalink
Merge branch 'rc' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
yunxuanhao authored Sep 10, 2024
2 parents e2a9342 + 7e85a0e commit cf55163
Show file tree
Hide file tree
Showing 21 changed files with 1,754 additions and 509 deletions.
61 changes: 57 additions & 4 deletions datetime/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ package datetime

import (
"fmt"
"runtime"
"strings"
"time"
)
Expand Down Expand Up @@ -382,11 +383,63 @@ func TimestampNano(timezone ...string) int64 {
return t.UnixNano()
}

// TraceFuncTime: trace the func costed time,just call it at top of the func like `defer TraceFuncTime()()`
func TraceFuncTime() func() {
pre := time.Now()
// TrackFuncTime track the time of function execution.
// call it at top of the func like `defer TrackFuncTime(time.Now())()`
// Play: todo
func TrackFuncTime(pre time.Time) func() {
callerName := getCallerName()
return func() {
elapsed := time.Since(pre)
fmt.Println("Costs Time:\t", elapsed)
fmt.Printf("Function %s execution time:\t %v", callerName, elapsed)
}
}

func getCallerName() string {
pc, _, _, ok := runtime.Caller(2)
if !ok {
return "Unknown"
}
fn := runtime.FuncForPC(pc)
if fn == nil {
return "Unknown"
}

fullName := fn.Name()
if lastDot := strings.LastIndex(fullName, "."); lastDot != -1 {
return fullName[lastDot+1:]
}

return fullName
}

// DaysBetween returns the number of days between two times.
// Play: todo
func DaysBetween(start, end time.Time) int {
duration := end.Sub(start)
days := int(duration.Hours() / 24)

return days
}

// GenerateDatetimesBetween returns a slice of strings between two times.
// layout: the format of the datetime string
// interval: the interval between two datetimes
// Play: todo
func GenerateDatetimesBetween(start, end time.Time, layout string, interval string) ([]string, error) {
var result []string

if start.After(end) {
start, end = end, start
}

duration, err := time.ParseDuration(interval)
if err != nil {
return nil, err
}

for current := start; !current.After(end); current = current.Add(duration) {
result = append(result, current.Format(layout))
}

return result, nil
}
29 changes: 29 additions & 0 deletions datetime/datetime_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,32 @@ func ExampleIsWeekend() {
// true
// false
}

func ExampleDaysBetween() {
start := time.Date(2024, time.September, 1, 0, 0, 0, 0, time.UTC)
end := time.Date(2024, time.September, 10, 0, 0, 0, 0, time.UTC)

result := DaysBetween(start, end)

fmt.Println(result)

// Output:
// 9
}

func ExampleGenerateDatetimesBetween() {
start := time.Date(2024, time.September, 1, 0, 0, 0, 0, time.UTC)
end := time.Date(2024, time.September, 1, 2, 0, 0, 0, time.UTC)

layout := "2006-01-02 15:04:05"
interval := "1h"

result, err := GenerateDatetimesBetween(start, end, layout, interval)

fmt.Println(result)
fmt.Println(err)

// Output:
// [2024-09-01 00:00:00 2024-09-01 01:00:00 2024-09-01 02:00:00]
// <nil>
}
111 changes: 111 additions & 0 deletions datetime/datetime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,114 @@ func TestTimestamp(t *testing.T) {
ts4 := TimestampNano()
t.Log(ts4)
}

func TestTrackFuncTime(t *testing.T) {
defer TrackFuncTime(time.Now())()

var n int
for i := 0; i < 5000000; i++ {
n++
}
}

func TestDaysBetween(t *testing.T) {
t.Parallel()

assert := internal.NewAssert(t, "TestDaysBetween")

tests := []struct {
start time.Time
end time.Time
expected int
}{
{
start: time.Date(2024, time.September, 1, 0, 0, 0, 0, time.UTC),
end: time.Date(2024, time.September, 10, 0, 0, 0, 0, time.UTC),
expected: 9,
},
{
start: time.Date(2024, time.September, 10, 0, 0, 0, 0, time.UTC),
end: time.Date(2024, time.September, 1, 0, 0, 0, 0, time.UTC),
expected: -9,
},
{
start: time.Date(2024, time.September, 1, 0, 0, 0, 0, time.UTC),
end: time.Date(2024, time.September, 1, 0, 0, 0, 0, time.UTC),
expected: 0,
},
{
start: time.Date(2024, time.January, 1, 0, 0, 0, 0, time.UTC),
end: time.Date(2024, time.December, 31, 0, 0, 0, 0, time.UTC),
expected: 365,
},
{
start: time.Date(2024, time.March, 1, 0, 0, 0, 0, time.UTC),
end: time.Date(2024, time.March, 31, 0, 0, 0, 0, time.UTC),
expected: 30,
},
}

for _, tt := range tests {
result := DaysBetween(tt.start, tt.end)
assert.Equal(tt.expected, result)
}
}

func TestGenerateDatetimesBetween(t *testing.T) {
t.Parallel()

assert := internal.NewAssert(t, "TestGenerateDatetimesBetween")

tests := []struct {
start time.Time
end time.Time
layout string
interval string
expected []string
}{
{
start: time.Date(2024, time.September, 1, 0, 0, 0, 0, time.UTC),
end: time.Date(2024, time.September, 1, 2, 0, 0, 0, time.UTC),
layout: "2006-01-02 15:04:05",
interval: "30m",
expected: []string{
"2024-09-01 00:00:00",
"2024-09-01 00:30:00",
"2024-09-01 01:00:00",
"2024-09-01 01:30:00",
"2024-09-01 02:00:00",
},
},
{
start: time.Date(2024, time.September, 1, 0, 0, 0, 0, time.UTC),
end: time.Date(2024, time.September, 1, 0, 0, 0, 0, time.UTC),
layout: "2006-01-02 15:04:05",
interval: "1h",
expected: []string{"2024-09-01 00:00:00"},
},
{
start: time.Date(2024, time.September, 1, 0, 0, 0, 0, time.UTC),
end: time.Date(2024, time.September, 1, 3, 0, 0, 0, time.UTC),
layout: "2006-01-02 15:04:05",
interval: "2h",
expected: []string{
"2024-09-01 00:00:00",
"2024-09-01 02:00:00",
},
},
}

for _, tt := range tests {
result, err := GenerateDatetimesBetween(tt.start, tt.end, tt.layout, tt.interval)

assert.Equal(tt.expected, result)
assert.IsNil(err)
}

t.Run("Invalid interval", func(t *testing.T) {
_, err := GenerateDatetimesBetween(time.Now(), time.Now(), "2006-01-02 15:04:05", "invalid")
if err == nil {
t.Fatal("Expected error, got nil")
}
})
}
108 changes: 107 additions & 1 deletion docs/api/packages/datetime.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ import (
- [TimestampMilli](#TimestampMilli)
- [TimestampMicro](#TimestampMicro)
- [TimestampNano](#TimestampNano)
- [TrackFuncTime](#TrackFuncTime)
- [DaysBetween](#DaysBetween)
- [GenerateDatetimesBetween](#GenerateDatetimesBetween)

<div STYLE="page-break-after: always;"></div>

Expand Down Expand Up @@ -1334,7 +1337,7 @@ import (
func main() {
result1 := datetime.NowDateOrTime("yyyy-mm-dd hh:mm:ss")

result2 := datetime.NowDateOrTime("yyyy-mm-dd hh:mm:ss", "EST")
result2 := datetime.NowDateOrTime("yyyy-mm-dd hh:mm:ss", "EST")

fmt.Println(result1)
fmt.Println(result2)
Expand Down Expand Up @@ -1464,4 +1467,107 @@ func main() {
// Output:
// 1690363051331788000
}
```

### <span id="TrackFuncTime">TrackFuncTime</span>

<p>测试函数执行时间。</p>

<b>函数签名:</b>

```go
func TrackFuncTime(pre time.Time) func()
```

<b>示例:<span style="float:right;display:inline-block;">[运行]()</span></b>

```go
package main

import (
"fmt"
"github.com/duke-git/lancet/v2/datetime"
)

func main() {
defer datetime.TrackFuncTime(time.Now())()

var n int
for i := 0; i < 5000000; i++ {
n++
}

fmt.Println(1) // Function main execution time: 1.460287ms
}
```

### <span id="DaysBetween">DaysBetween</span>

<p>返回两个日期之间的天数差。</p>

<b>函数签名:</b>

```go
func DaysBetween(start, end time.Time) int
```

<b>示例:<span style="float:right;display:inline-block;">[运行]()</span></b>

```go
package main

import (
"fmt"
"github.com/duke-git/lancet/v2/datetime"
)

func main() {
start := time.Date(2024, time.September, 1, 0, 0, 0, 0, time.UTC)
end := time.Date(2024, time.September, 10, 0, 0, 0, 0, time.UTC)

result := datetime.DaysBetween(start, end)

fmt.Println(result)

// Output:
// 9
}
```

### <span id="GenerateDatetimesBetween">GenerateDatetimesBetween</span>

<p>生成从start到end的所有日期时间的字符串列表。layout参数表示时间格式,例如"2006-01-02 15:04:05",interval参数表示时间间隔,例如"1h"表示1小时,"30m"表示30分钟。</p>

<b>函数签名:</b>

```go
func GenerateDatetimesBetween(start, end time.Time, layout string, interval string) ([]string, error)
```

<b>示例:<span style="float:right;display:inline-block;">[运行]()</span></b>

```go
package main

import (
"fmt"
"github.com/duke-git/lancet/v2/datetime"
)

func main() {
start := time.Date(2024, time.September, 1, 0, 0, 0, 0, time.UTC)
end := time.Date(2024, time.September, 1, 2, 0, 0, 0, time.UTC)

layout := "2006-01-02 15:04:05"
interval := "1h"

result, err := datetime.GenerateDatetimesBetween(start, end, layout, interval)

fmt.Println(result)
fmt.Println(err)

// Output:
// [2024-09-01 00:00:00 2024-09-01 01:00:00 2024-09-01 02:00:00]
// <nil>
}
```
2 changes: 2 additions & 0 deletions docs/api/packages/maputil.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ maputil 包包括一些操作 map 的函数。
## 源码:

- [https://github.com/duke-git/lancet/blob/main/maputil/map.go](https://github.com/duke-git/lancet/blob/main/maputil/map.go)
- [https://github.com/duke-git/lancet/blob/main/maputil/concurrentmap.go](https://github.com/duke-git/lancet/blob/main/maputil/concurrentmap.go)
- [https://github.com/duke-git/lancet/blob/main/maputil/orderedmap.go](https://github.com/duke-git/lancet/blob/main/maputil/orderedmap.go)

<div STYLE="page-break-after: always;"></div>

Expand Down
Loading

0 comments on commit cf55163

Please sign in to comment.