Skip to content

Commit

Permalink
Fixed DiffInYears、DiffInMonths、DiffAbsInYears、DiffAbsInMonths methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gouguoyin committed Oct 26, 2022
1 parent 774f8af commit f3fc6fc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 37 deletions.
44 changes: 11 additions & 33 deletions difference.go
Original file line number Diff line number Diff line change
@@ -1,72 +1,50 @@
package carbon

import (
"math"
"strings"
)

// DiffInYears gets the difference in years.
// 相差多少年
func (c Carbon) DiffInYears(carbon ...Carbon) int64 {
end := c.Now()
start, end := c, c.Now()
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
}
return c.DiffInMonths(end) / MonthsPerYear
return int64(math.Floor(float64((end.Timestamp() - start.Timestamp()) / (365 * 24 * 3600))))
}

// DiffAbsInYears gets the difference in years with absolute value.
// 相差多少年(绝对值)
func (c Carbon) DiffAbsInYears(carbon ...Carbon) int64 {
end := c.Now()
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
}
return getAbsValue(c.DiffInYears(end))
return getAbsValue(c.DiffInYears(carbon...))
}

// DiffInMonths gets the difference in months.
// 相差多少月
func (c Carbon) DiffInMonths(carbon ...Carbon) int64 {
end := c.Now()
start, end := c, c.Now()
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
}
startYear, startMonth, startDay := c.Date()
endYear, endMonth, endDay := end.Date()
diffYear, diffMonth, diffDay := endYear-startYear, endMonth-startMonth, endDay-startDay
if diffDay < 0 {
diffMonth = diffMonth - 1
}
if diffYear == 0 && diffMonth == 0 {
return int64(0)
}
if diffYear == 0 && diffMonth != 0 && diffDay != 0 {
if int(end.DiffAbsInHours(c)) < c.DaysInMonth()*HoursPerDay {
return int64(0)
}
return int64(diffMonth)
}
return int64(diffYear*MonthsPerYear + diffMonth)
return int64(math.Floor(float64((end.Timestamp() - start.Timestamp()) / (30 * 24 * 3600))))
}

// DiffAbsInMonths gets the difference in months with absolute value.
// 相差多少月(绝对值)
func (c Carbon) DiffAbsInMonths(carbon ...Carbon) int64 {
end := c.Now()
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
}
return getAbsValue(c.DiffInMonths(end))
return getAbsValue(c.DiffInMonths(carbon...))
}

// DiffInWeeks gets the difference in weeks.
// 相差多少周
func (c Carbon) DiffInWeeks(carbon ...Carbon) int64 {
end := c.Now()
start, end := c, c.Now()
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
}
return c.DiffInDays(end) / DaysPerWeek
return int64(math.Floor(float64((end.Timestamp() - start.Timestamp()) / (7 * 24 * 3600))))
}

// DiffAbsInWeeks gets the difference in weeks with absolute value.
Expand All @@ -82,11 +60,11 @@ func (c Carbon) DiffAbsInWeeks(carbon ...Carbon) int64 {
// DiffInDays gets the difference in days.
// 相差多少天
func (c Carbon) DiffInDays(carbon ...Carbon) int64 {
end := c.Now()
start, end := c, c.Now()
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
}
return c.DiffInSeconds(end) / SecondsPerDay
return int64(math.Floor(float64((end.Timestamp() - start.Timestamp()) / (24 * 3600))))
}

// DiffAbsInDays gets the difference in days with absolute value.
Expand Down
8 changes: 4 additions & 4 deletions difference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestCarbon_DiffInYears(t *testing.T) {
{"2020-08-05 13:14:15", "2020-07-28 13:14:00", 0},
{"2020-12-31 13:14:15", "2021-01-01 13:14:15", 0},
{"2020-08-05 13:14:15", "2021-08-28 13:14:59", 1},
{"2020-08-05 13:14:15", "2018-08-28 13:14:59", -2},
{"2020-08-05 13:14:15", "2018-08-28 13:14:59", -1},
}

for index, test := range tests {
Expand All @@ -40,7 +40,7 @@ func TestCarbon_DiffAbsInYears(t *testing.T) {
{"2020-08-05 13:14:15", "2020-07-28 13:14:00", 0},
{"2020-12-31 13:14:15", "2021-01-01 13:14:15", 0},
{"2020-08-05 13:14:15", "2021-08-28 13:14:59", 1},
{"2020-08-05 13:14:15", "2018-08-28 13:14:59", 2},
{"2020-08-05 13:14:15", "2018-08-28 13:14:59", 1},
}

for index, test := range tests {
Expand All @@ -63,7 +63,7 @@ func TestCarbon_DiffInMonths(t *testing.T) {
{"2020-08-05 13:14:15", "2020-09-06 13:14:59", 1},
{"2020-12-31 13:14:15", "2021-01-31 13:14:15", 1},
{"2020-08-05 13:14:15", "2021-08-28 13:14:59", 12},
{"2020-08-05 13:14:15", "2018-08-28 13:14:59", -24},
{"2020-08-05 13:14:15", "2018-08-28 13:14:59", -23},
}

for index, test := range tests {
Expand All @@ -85,7 +85,7 @@ func TestCarbon_DiffAbsInMonths(t *testing.T) {
{"2020-08-05 13:14:15", "2020-07-28 13:14:00", 0},
{"2020-12-31 13:14:15", "2021-01-01 13:14:15", 0},
{"2020-08-05 13:14:15", "2021-08-28 13:14:59", 12},
{"2020-08-05 13:14:15", "2018-08-28 13:14:59", 24},
{"2020-08-05 13:14:15", "2018-08-28 13:14:59", 23},
}

for index, test := range tests {
Expand Down

0 comments on commit f3fc6fc

Please sign in to comment.