Skip to content

Commit

Permalink
Resolved #3
Browse files Browse the repository at this point in the history
  • Loading branch information
shinYeongHyeon committed Jan 29, 2021
1 parent da0633f commit c4dddc2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ $ go get -u github.com/shinYeongHyeon/go-times
- [GetDateOfFirstFridayOfMonth](#getDateOfFirstFridayOfMonth)
- [GetDateOfFirstSaturdayOfMonth](#getDateOfFirstSaturdayOfMonth)
- [GetDateOfFirstSundayOfMonth](#getDateOfFirstSundayOfMonth)
- [GetFirstWorkingTimeOfMonth](#getFirstWorkingTimeOfMonth)
- [GetNthWeekOfMonth](#getNthWeekOfMonth)
- [GetLastTimeOfMonth](#getLastTimeOfMonth)

Expand Down Expand Up @@ -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)
Expand Down
26 changes: 13 additions & 13 deletions constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
13 changes: 13 additions & 0 deletions times.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
Expand Down
20 changes: 19 additions & 1 deletion times_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

0 comments on commit c4dddc2

Please sign in to comment.