Skip to content

Commit

Permalink
feat: Return only actual days if month and year are current (#22)
Browse files Browse the repository at this point in the history
* refactor: Move moneyutils to pkg

* feat: Implement return of only actual days in current month

* refactor: Move moneyutils to pkg

* feat: Use DaysInMonthTillDate in menu
  • Loading branch information
obalunenko committed Dec 17, 2022
1 parent f2ec21e commit 4cff75c
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 5 deletions.
4 changes: 2 additions & 2 deletions cmd/ge-tax-calc/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"github.com/AlecAivazis/survey/v2/core"
"github.com/urfave/cli/v2"

"github.com/obalunenko/georgia-tax-calculator/internal/moneyutils"
"github.com/obalunenko/georgia-tax-calculator/internal/service"
"github.com/obalunenko/georgia-tax-calculator/internal/taxes"
"github.com/obalunenko/georgia-tax-calculator/pkg/dateutils"
"github.com/obalunenko/georgia-tax-calculator/pkg/moneyutils"
"github.com/obalunenko/georgia-tax-calculator/pkg/nbggovge/currencies"
)

Expand Down Expand Up @@ -304,7 +304,7 @@ func makeDayMenu(p service.DateRequest) (survey.Prompt, error) {
return nil, fmt.Errorf("parse year: %w", err)
}

days := dateutils.DaysList(dateutils.DaysInMonth(parseMonth, parseYear))
days := dateutils.DaysList(dateutils.DaysInMonthTillDate(parseMonth, parseYear, time.Now()))

msg := "Select day of income"

Expand Down
2 changes: 1 addition & 1 deletion internal/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

"github.com/obalunenko/georgia-tax-calculator/internal/models"
"github.com/obalunenko/georgia-tax-calculator/internal/moneyutils"
"github.com/obalunenko/georgia-tax-calculator/pkg/moneyutils"
"github.com/obalunenko/georgia-tax-calculator/pkg/nbggovge"
"github.com/obalunenko/georgia-tax-calculator/pkg/nbggovge/currencies"
"github.com/obalunenko/georgia-tax-calculator/pkg/nbggovge/option"
Expand Down
2 changes: 1 addition & 1 deletion internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (

"github.com/obalunenko/georgia-tax-calculator/internal/converter"
"github.com/obalunenko/georgia-tax-calculator/internal/models"
"github.com/obalunenko/georgia-tax-calculator/internal/moneyutils"
"github.com/obalunenko/georgia-tax-calculator/internal/spinner"
"github.com/obalunenko/georgia-tax-calculator/internal/taxes"
"github.com/obalunenko/georgia-tax-calculator/pkg/dateutils"
"github.com/obalunenko/georgia-tax-calculator/pkg/moneyutils"
"github.com/obalunenko/georgia-tax-calculator/pkg/nbggovge"
"github.com/obalunenko/georgia-tax-calculator/pkg/nbggovge/currencies"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/taxes/calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

"github.com/obalunenko/georgia-tax-calculator/internal/models"
"github.com/obalunenko/georgia-tax-calculator/internal/moneyutils"
"github.com/obalunenko/georgia-tax-calculator/pkg/moneyutils"
)

var (
Expand Down
16 changes: 16 additions & 0 deletions pkg/dateutils/dateutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ func DaysInMonth(m time.Month, year int) int {
return time.Date(year, m+1, 0, 0, 0, 0, 0, time.UTC).Day()
}

// DaysInMonthTillDate return number of days in Month for specific year, and takes in attention current day.
// It will not return days in the future.
func DaysInMonthTillDate(m time.Month, year int, now time.Time) int {
days := DaysInMonth(m, year)

nowy, nowm, nowd := now.Date()

if nowy == year && nowm == m {
if days > nowd {
return nowd
}
}

return days
}

// GetMonths returns list of all month.
func GetMonths() []string {
const totalMonth = 12
Expand Down
50 changes: 50 additions & 0 deletions pkg/dateutils/dateutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,53 @@ func TestParseYear(t *testing.T) {
})
}
}

func TestDaysInMonthTillDate(t *testing.T) {
type args struct {
m time.Month
year int
now time.Time
}

tests := []struct {
name string
args args
want int
}{
{
name: "2022/10; now is 2022/12/18 - 31",
args: args{
m: time.October,
year: 2022,
now: time.Date(2022, 12, 18, 0, 0, 0, 0, time.Local),
},
want: 31,
},
{
name: "2022/12; now is 2022/12/18 - 18",
args: args{
m: time.December,
year: 2022,
now: time.Date(2022, 12, 18, 0, 0, 0, 0, time.Local),
},
want: 18,
},
{
name: "2021/12; now is 2022/12/18 - 18",
args: args{
m: time.December,
year: 2021,
now: time.Date(2022, 12, 18, 0, 0, 0, 0, time.Local),
},
want: 31,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := DaysInMonthTillDate(tt.args.m, tt.args.year, tt.args.now)

assert.Equalf(t, tt.want, got, "DaysInMonthTillDate(%v, %v, %v)", tt.args.m, tt.args.year, tt.args.now)
})
}
}
File renamed without changes.

0 comments on commit 4cff75c

Please sign in to comment.