From c4dddc261776ff6783d164b7557080d0b55a387a Mon Sep 17 00:00:00 2001 From: ShinYeongHyeon Date: Fri, 29 Jan 2021 23:08:02 +0900 Subject: [PATCH] Resolved #3 --- README.md | 7 +++++++ constant.go | 26 +++++++++++++------------- times.go | 13 +++++++++++++ times_test.go | 20 +++++++++++++++++++- 4 files changed, 52 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 1915142..61c6d4a 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ $ go get -u github.com/shinYeongHyeon/go-times - [GetDateOfFirstFridayOfMonth](#getDateOfFirstFridayOfMonth) - [GetDateOfFirstSaturdayOfMonth](#getDateOfFirstSaturdayOfMonth) - [GetDateOfFirstSundayOfMonth](#getDateOfFirstSundayOfMonth) + - [GetFirstWorkingTimeOfMonth](#getFirstWorkingTimeOfMonth) - [GetNthWeekOfMonth](#getNthWeekOfMonth) - [GetLastTimeOfMonth](#getLastTimeOfMonth) @@ -74,6 +75,12 @@ This will get date of first sunday of month func GetDateOfFirstSundayOfMonth(t time.Time) int ``` +#### GetFirstWorkingTimeOfMonth +This will get First Working TimeObject of request year, month. +```go +func GetFirstWorkingTimeOfMonth(year, month int) time.Time +``` + #### GetNthWeekOfMonth This will get `n`th Week of Month (Regard the start of the week as Sunday) diff --git a/constant.go b/constant.go index 0369f9d..56accce 100644 --- a/constant.go +++ b/constant.go @@ -14,17 +14,17 @@ var DaysMap = map[time.Weekday]int{ } // MonthMap is dependent for time package -var MonthMap = map[time.Month]int{ - time.January: 1, - time.February: 2, - time.March: 3, - time.April: 4, - time.May: 5, - time.June: 6, - time.July: 7, - time.August: 8, - time.September: 9, - time.October: 10, - time.November: 11, - time.December: 12, +var MonthMapByInt = map[int]time.Month{ + 1: time.January, + 2: time.February, + 3: time.March, + 4: time.April, + 5: time.May, + 6: time.June, + 7: time.July, + 8: time.August, + 9: time.September, + 10: time.October, + 11: time.November, + 12: time.December, } diff --git a/times.go b/times.go index c361c9f..75dc671 100644 --- a/times.go +++ b/times.go @@ -94,6 +94,19 @@ func GetLastTimeOfMonth(t time.Time) time.Time { return getFirstTimeOfMonth(t).AddDate(0, 1, -1) } +// GetFirstWorkingTimeOfMonth : get First Working TimeObject Of Request Month +func GetFirstWorkingTimeOfMonth(year, month int) time.Time { + firstDateOfMonth := time.Date(year, MonthMapByInt[month], 1, 0, 0, 0, 0, time.UTC) + + if firstDateOfMonth.Weekday() == 0 { + return firstDateOfMonth.AddDate(0, 0, 1) + } else if firstDateOfMonth.Weekday() == 6 { + return firstDateOfMonth.AddDate(0, 0, 2) + } + + return firstDateOfMonth +} + // getDayOfFirstDateOfMonth : get day of first date func getDayOfFirstDateOfMonth(t time.Time) int { return DaysMap[(getFirstTimeOfMonth(t)).Weekday()] diff --git a/times_test.go b/times_test.go index f65bf79..656c83e 100644 --- a/times_test.go +++ b/times_test.go @@ -7,10 +7,11 @@ import ( var startedMondayMonth time.Time = time.Date(2021, 2, 3, 0, 0, 0, 0, time.UTC) var startedSaturdayMonth time.Time = time.Date(2021, 5, 3, 0, 0, 0, 0, time.UTC) -var startedSundayMonth time.Time = time.Date(2021, 8, 3, 0, 0, 0, 0, time.UTC) +var startedSundayMonth time.Time = time.Date(2021, 8, 2, 0, 0, 0, 0, time.UTC) var lastTimeOf202108 time.Time = time.Date(2021, 8, 31, 0, 0, 0, 0, time.UTC) var lastTimeOf202002 time.Time = time.Date(2020, 2, 29, 0, 0, 0, 0, time.UTC) var timeOf20200206 time.Time = time.Date(2020, 2, 6, 0, 0, 0, 0, time.UTC) +var timeOf20210201 time.Time = time.Date(2021, 6, 1, 0, 0, 0, 0, time.UTC) func TestGetDateOfFirstMondayOfMonth(t *testing.T) { want := 1 @@ -113,4 +114,21 @@ func TestGetLastTimeOfMonth(t *testing.T) { if got := GetLastTimeOfMonth(timeOf20200206); got != want { t.Errorf("GetLastTimeOfMonth() = %v, want %v", got, want) } +} + +func TestGetFirstWorkingTimeOfMonth(t *testing.T) { + want := startedSaturdayMonth + if got := GetFirstWorkingTimeOfMonth(2021, 5); got != want { + t.Errorf("GetLastTimeOfMonth() = %v, want %v", got, want) + } + + want = timeOf20210201 + if got := GetFirstWorkingTimeOfMonth(2021, 6); got != want { + t.Errorf("GetLastTimeOfMonth() = %v, want %v", got, want) + } + + want = startedSundayMonth + if got := GetFirstWorkingTimeOfMonth(2021, 8); got != want { + t.Errorf("GetLastTimeOfMonth() = %v, want %v", got, want) + } } \ No newline at end of file