Skip to content

Commit

Permalink
feat(timeutil): add timeutils (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
shipengqi authored Jul 24, 2024
1 parent 1020337 commit e26630e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
15 changes: 15 additions & 0 deletions timeutil/timeutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package timeutil

import "time"

// MonthIntervalTimeFromNow return the start date and end date of the given month
// if mon = 0, indicate current month
// if mon = -1, indicate last month
// if mon = 1, indicate next month
func MonthIntervalTimeFromNow(mon int) (start, end string) {
year, month, _ := time.Now().Date()
thisMonth := time.Date(year, month, 1, 0, 0, 0, 0, time.Local)
start = thisMonth.AddDate(0, mon, 0).Format("2006-01-02")
end = thisMonth.AddDate(0, mon+1, -1).Format("2006-01-02")
return start, end
}
39 changes: 39 additions & 0 deletions timeutil/timeutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package timeutil

import (
"testing"
)

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

0 comments on commit e26630e

Please sign in to comment.