Skip to content

Commit

Permalink
Fix diff in months and added tests (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
asantos00 authored and Hugo Correia committed Jun 20, 2018
1 parent d1deb18 commit 57f1331
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion carbon.go
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ func (c *Carbon) DiffInMonths(carb *Carbon, abs bool) int64 {
}

diffYr := c.Year() - carb.Year()
if diffYr > 1 {
if math.Abs(float64(diffYr)) > 1 {
diff := c.DiffInYears(carb, abs)*monthsPerYear + m

return absValue(abs, diff)
Expand Down
7 changes: 7 additions & 0 deletions carbon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1958,6 +1958,13 @@ func TestDiffInMonthsMoreThanOneYear(t *testing.T) {
assert.EqualValues(t, 13, t1.DiffInMonths(t2, true))
}

func TestDiffInMonthsMoreThanTwoYears(t *testing.T) {
t1, _ := Create(2018, time.August, 4, 1, 0, 0, 0, "UTC")
t2, _ := Create(2025, time.July, 3, 1, 0, 0, 0, "UTC")

assert.EqualValues(t, 82, t1.DiffInMonths(t2, true))
}

func TestDiffInMonthsOneDayDifference(t *testing.T) {
t1, _ := Create(2016, time.September, 1, 10, 0, 0, 0, "UTC")
t2, _ := Create(2016, time.August, 31, 10, 0, 0, 0, "UTC")
Expand Down

0 comments on commit 57f1331

Please sign in to comment.