-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
// }) | ||
// } | ||
} |