Skip to content

Commit

Permalink
[FIX] Between dates computation
Browse files Browse the repository at this point in the history
Fix the number months and years between two dates.
  • Loading branch information
arobase-che committed Jun 1, 2023
1 parent 9a28f36 commit 5334cfd
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,10 @@ func getStartDate(date time.Time, freq frequency.Type, index int) (time.Time, er

func getMonthsBetweenDates(start time.Time, end time.Time) (*int, error) {
count := 0
for start.Before(end) {
start = start.AddDate(0, 1, 0)
for start.AddDate(0, count, 0).Before(end) {
count++
}
finalDate := start.AddDate(0, 0, -1)
finalDate := start.AddDate(0, count, -1)
if !finalDate.Equal(end) {
return nil, ErrUnevenEndDate
}
Expand All @@ -121,11 +120,10 @@ func getMonthsBetweenDates(start time.Time, end time.Time) (*int, error) {

func getYearsBetweenDates(start time.Time, end time.Time) (*int, error) {
count := 0
for start.Before(end) {
start = start.AddDate(1, 0, 0)
for start.AddDate(count, 0, 0).Before(end) {
count++
}
finalDate := start.AddDate(0, 0, -1)
finalDate := start.AddDate(0, count, -1)
if !finalDate.Equal(end) {
return nil, ErrUnevenEndDate
}
Expand Down

0 comments on commit 5334cfd

Please sign in to comment.