Skip to content

Commit

Permalink
feat(timeutils): add more time utils
Browse files Browse the repository at this point in the history
  • Loading branch information
shipengqi committed Jul 25, 2024
1 parent e26630e commit 445046c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 34 deletions.
24 changes: 22 additions & 2 deletions timeutil/timeutil.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package timeutil

import "time"
import (
"fmt"
"time"
)

// MonthIntervalTimeFromNow return the start date and end date of the given month
// MonthIntervalTimeFromNow return the start date and end date of the month
// if mon = 0, indicate current month
// if mon = -1, indicate last month
// if mon = 1, indicate next month
Expand All @@ -13,3 +16,20 @@ func MonthIntervalTimeFromNow(mon int) (start, end string) {
end = thisMonth.AddDate(0, mon+1, -1).Format("2006-01-02")
return start, end
}

// MonthIntervalTimeWithGivenMon return the start date and end date of the given month
func MonthIntervalTimeWithGivenMon(mon string, layout ...string) (start, end string) {
timeLayout := "2006-01-02"
if len(layout) > 0 && layout[0] != "" {
timeLayout = layout[0]
}
conv, err := time.ParseInLocation(timeLayout, mon, time.Local)
if err != nil {
fmt.Println(err)
}
year, month, _ := conv.Date()
givenMonth := time.Date(year, month, 1, 0, 0, 0, 0, time.Local)
start = givenMonth.AddDate(0, 0, 0).Format("2006-01-02")
end = givenMonth.AddDate(0, 1, -1).Format("2006-01-02")
return start, end
}
91 changes: 59 additions & 32 deletions timeutil/timeutil_test.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,66 @@
package timeutil

import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestMonthIntervalTimeWithGivenDate(t *testing.T) {
// tests := []struct {
// mon int
// start string
// end string
// }{
// {-12, "2023-07-01", "2023-07-31"},
// {-8, "2023-11-01", "2023-11-30"},
// {-7, "2023-12-01", "2023-12-31"},
// {-6, "2024-01-01", "2024-01-31"},
// {-5, "2024-02-01", "2024-02-29"},
// {-4, "2024-03-01", "2024-03-31"},
// {-3, "2024-04-01", "2024-04-30"},
// {-2, "2024-05-01", "2024-05-31"},
// {-1, "2024-06-01", "2024-06-30"},
// {0, "2024-07-01", "2024-07-31"},
// {1, "2024-08-01", "2024-08-31"},
// {2, "2024-09-01", "2024-09-30"},
// {3, "2024-10-01", "2024-10-31"},
// {4, "2024-11-01", "2024-11-30"},
// {5, "2024-12-01", "2024-12-31"},
// {6, "2025-01-01", "2025-01-31"},
// {7, "2025-02-01", "2025-02-28"},
// }
//
// for _, v := range tests {
// t.Run(fmt.Sprintf("mon %d", v.mon), func(t *testing.T) {
// start, end := MonthIntervalTimeFromNow(v.mon)
// assert.Equal(t, v.start, start)
// assert.Equal(t, v.end, end)
// })
// }
func TestMonthIntervalTimeFromNow(t *testing.T) {
year, month, _ := time.Now().Date()
thisMonth := time.Date(year, month, 1, 0, 0, 0, 0, time.Local).Format("2006-01-02")
start, end := MonthIntervalTimeWithGivenMon(thisMonth)
s, e := MonthIntervalTimeFromNow(0)
assert.Equal(t, start, s)
assert.Equal(t, end, e)
}

func TestMonthIntervalTimeWithGivenMon(t *testing.T) {
tests := []struct {
mon string
start string
end string
}{
{"2023-07-11", "2023-07-01", "2023-07-31"},
{"2023-07-01", "2023-07-01", "2023-07-31"},
{"2023-07-31", "2023-07-01", "2023-07-31"},
{"2023-12-01", "2023-12-01", "2023-12-31"},
{"2023-12-21", "2023-12-01", "2023-12-31"},
{"2023-12-31", "2023-12-01", "2023-12-31"},
{"2024-01-01", "2024-01-01", "2024-01-31"},
{"2024-01-11", "2024-01-01", "2024-01-31"},
{"2024-01-31", "2024-01-01", "2024-01-31"},
}

for _, v := range tests {
t.Run(fmt.Sprintf("mon %s", v.mon), func(t *testing.T) {
start, end := MonthIntervalTimeWithGivenMon(v.mon)
assert.Equal(t, v.start, start)
assert.Equal(t, v.end, end)
})
}
}

func TestMonthIntervalTimeWithGivenMonTimeParam(t *testing.T) {
tests := []struct {
mon string
start string
end string
layout string
}{
{"2023-07-11 15:04:05", "2023-07-01", "2023-07-31", "2006-01-02 15:04:05"},
{"2023-07-01 12:04:05", "2023-07-01", "2023-07-31", "2006-01-02 15:04:05"},
{"2023-07-31 00:04:05", "2023-07-01", "2023-07-31", "2006-01-02 15:04:05"},
{"2023-07-31 23:04:05", "2023-07-01", "2023-07-31", "2006-01-02 15:04:05"},
}

for _, v := range tests {
t.Run(fmt.Sprintf("mon %s", v.mon), func(t *testing.T) {
start, end := MonthIntervalTimeWithGivenMon(v.mon, v.layout)
assert.Equal(t, v.start, start)
assert.Equal(t, v.end, end)
})
}
}

0 comments on commit 445046c

Please sign in to comment.