Skip to content

Commit 285160b

Browse files
committed
refactor: Remove unnecessary calls to Date() method
1 parent 11b2946 commit 285160b

1 file changed

Lines changed: 6 additions & 10 deletions

File tree

difference.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -332,15 +332,11 @@ func getDiffInMonths(start, end *Carbon) int64 {
332332
if start.IsInvalid() || end.IsInvalid() {
333333
return 0
334334
}
335-
y, m, d, h, i, s, ns := start.DateTimeNano()
336-
endYear, endMonth, _ := end.Date()
337-
338-
yearDiff := endYear - y
339-
monthDiff := endMonth - m
340-
totalMonths := yearDiff*12 + monthDiff
341-
342-
if time.Date(y, time.Month(m+totalMonths), d, h, i, s, ns, start.StdTime().Location()).After(end.StdTime()) {
343-
return int64(totalMonths - 1)
335+
sy, sm, d, h, i, s, ns := start.DateTimeNano()
336+
ey, em, _ := end.Date()
337+
dm := (ey-sy)*12 + (em - sm)
338+
if time.Date(sy, time.Month(sm+dm), d, h, i, s, ns, start.StdTime().Location()).After(end.StdTime()) {
339+
return int64(dm - 1)
344340
}
345-
return int64(totalMonths)
341+
return int64(dm)
346342
}

0 commit comments

Comments
 (0)