carbon/difference.go

253 lines
6.5 KiB
Go
Raw Normal View History

2021-02-18 14:32:31 +08:00
package carbon
import (
"strings"
)
2021-02-18 14:32:31 +08:00
2021-08-10 10:57:22 +08:00
// DiffInYears gets the difference in years.
2021-07-28 15:18:05 +08:00
// 相差多少年
2021-07-23 10:57:18 +08:00
func (c Carbon) DiffInYears(carbon ...Carbon) int64 {
2021-02-18 14:32:31 +08:00
end := c.Now()
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
2021-02-18 14:32:31 +08:00
}
2021-04-05 16:30:48 +08:00
return c.DiffInMonths(end) / MonthsPerYear
2021-02-18 14:32:31 +08:00
}
2021-08-10 10:57:22 +08:00
// DiffInYearsWithAbs gets the difference in years with absolute value.
2021-07-28 15:18:05 +08:00
// 相差多少年(绝对值)
2021-07-23 10:57:18 +08:00
func (c Carbon) DiffInYearsWithAbs(carbon ...Carbon) int64 {
2021-02-18 14:32:31 +08:00
end := c.Now()
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
2021-02-18 14:32:31 +08:00
}
return getAbsValue(c.DiffInYears(end))
}
2021-08-10 10:57:22 +08:00
// DiffInMonths gets the difference in months.
2021-07-28 15:18:05 +08:00
// 相差多少月
2021-07-23 10:57:18 +08:00
func (c Carbon) DiffInMonths(carbon ...Carbon) int64 {
2021-02-18 14:32:31 +08:00
end := c.Now()
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
2021-02-18 14:32:31 +08:00
}
2022-04-12 17:32:03 +08:00
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
2021-02-18 14:32:31 +08:00
}
2022-04-12 17:32:03 +08:00
if diffYear == 0 && diffMonth == 0 {
2021-08-10 10:57:22 +08:00
return int64(0)
2021-02-18 14:32:31 +08:00
}
2022-04-12 17:32:03 +08:00
if diffYear == 0 && diffMonth != 0 && diffDay != 0 {
2021-02-18 14:32:31 +08:00
if int(end.DiffInHoursWithAbs(c)) < c.DaysInMonth()*HoursPerDay {
2021-08-10 10:57:22 +08:00
return int64(0)
2021-02-18 14:32:31 +08:00
}
2022-04-12 17:32:03 +08:00
return int64(diffMonth)
2021-02-18 14:32:31 +08:00
}
2022-04-12 17:32:03 +08:00
return int64(diffYear*MonthsPerYear + diffMonth)
2021-02-18 14:32:31 +08:00
}
2021-08-10 10:57:22 +08:00
// DiffInMonthsWithAbs gets the difference in months with absolute value.
2021-07-28 15:18:05 +08:00
// 相差多少月(绝对值)
2021-07-23 10:57:18 +08:00
func (c Carbon) DiffInMonthsWithAbs(carbon ...Carbon) int64 {
2021-02-18 14:32:31 +08:00
end := c.Now()
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
2021-02-18 14:32:31 +08:00
}
return getAbsValue(c.DiffInMonths(end))
}
2021-08-10 10:57:22 +08:00
// DiffInWeeks gets the difference in weeks.
2021-07-28 15:18:05 +08:00
// 相差多少周
2021-07-23 10:57:18 +08:00
func (c Carbon) DiffInWeeks(carbon ...Carbon) int64 {
2021-02-18 14:32:31 +08:00
end := c.Now()
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
2021-02-18 14:32:31 +08:00
}
return c.DiffInDays(end) / DaysPerWeek
}
2021-08-10 10:57:22 +08:00
// DiffInWeeksWithAbs gets the difference in weeks with absolute value.
2021-07-28 15:18:05 +08:00
// 相差多少周(绝对值)
2021-07-23 10:57:18 +08:00
func (c Carbon) DiffInWeeksWithAbs(carbon ...Carbon) int64 {
2021-02-18 14:32:31 +08:00
end := c.Now()
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
2021-02-18 14:32:31 +08:00
}
return getAbsValue(c.DiffInWeeks(end))
}
2021-08-10 10:57:22 +08:00
// DiffInDays gets the difference in days.
2021-07-28 15:18:05 +08:00
// 相差多少天
2021-07-23 10:57:18 +08:00
func (c Carbon) DiffInDays(carbon ...Carbon) int64 {
2021-02-18 14:32:31 +08:00
end := c.Now()
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
2021-02-18 14:32:31 +08:00
}
return c.DiffInSeconds(end) / SecondsPerDay
}
2021-08-10 10:57:22 +08:00
// DiffInDaysWithAbs gets the difference in days with absolute value.
2021-07-28 15:18:05 +08:00
// 相差多少天(绝对值)
2021-07-23 10:57:18 +08:00
func (c Carbon) DiffInDaysWithAbs(carbon ...Carbon) int64 {
2021-02-18 14:32:31 +08:00
end := c.Now()
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
2021-02-18 14:32:31 +08:00
}
return getAbsValue(c.DiffInDays(end))
}
2021-08-10 10:57:22 +08:00
// DiffInHours gets the difference in hours.
2021-07-28 15:18:05 +08:00
// 相差多少小时
2021-07-23 10:57:18 +08:00
func (c Carbon) DiffInHours(carbon ...Carbon) int64 {
2021-02-18 14:32:31 +08:00
end := c.Now()
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
2021-02-18 14:32:31 +08:00
}
return c.DiffInSeconds(end) / SecondsPerHour
}
2021-08-10 10:57:22 +08:00
// DiffInHoursWithAbs gets the difference in hours with absolute value.
2021-07-28 15:18:05 +08:00
// 相差多少小时(绝对值)
2021-07-23 10:57:18 +08:00
func (c Carbon) DiffInHoursWithAbs(carbon ...Carbon) int64 {
2021-02-18 14:32:31 +08:00
end := c.Now()
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
2021-02-18 14:32:31 +08:00
}
return getAbsValue(c.DiffInHours(end))
}
2021-08-10 10:57:22 +08:00
// DiffInMinutes gets the difference in minutes.
2021-07-28 15:18:05 +08:00
// 相差多少分钟
2021-07-23 10:57:18 +08:00
func (c Carbon) DiffInMinutes(carbon ...Carbon) int64 {
2021-02-18 14:32:31 +08:00
end := c.Now()
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
2021-02-18 14:32:31 +08:00
}
return c.DiffInSeconds(end) / SecondsPerMinute
}
2021-08-10 10:57:22 +08:00
// DiffInMinutesWithAbs gets the difference in minutes with absolute value.
2021-07-28 15:18:05 +08:00
// 相差多少分钟(绝对值)
2021-07-23 10:57:18 +08:00
func (c Carbon) DiffInMinutesWithAbs(carbon ...Carbon) int64 {
2021-02-18 14:32:31 +08:00
end := c.Now()
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
2021-02-18 14:32:31 +08:00
}
return getAbsValue(c.DiffInMinutes(end))
}
2021-08-10 10:57:22 +08:00
// DiffInSeconds gets the difference in seconds.
2021-07-28 15:18:05 +08:00
// 相差多少秒
2021-07-23 10:57:18 +08:00
func (c Carbon) DiffInSeconds(carbon ...Carbon) int64 {
2021-02-18 14:32:31 +08:00
end := c.Now()
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
2021-02-18 14:32:31 +08:00
}
return end.Timestamp() - c.Timestamp()
2021-02-18 14:32:31 +08:00
}
2021-08-10 10:57:22 +08:00
// DiffInSecondsWithAbs gets the difference in seconds with absolute value.
2021-07-28 15:18:05 +08:00
// 相差多少秒(绝对值)
2021-07-23 10:57:18 +08:00
func (c Carbon) DiffInSecondsWithAbs(carbon ...Carbon) int64 {
2021-02-18 14:32:31 +08:00
end := c.Now()
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
2021-02-18 14:32:31 +08:00
}
return getAbsValue(c.DiffInSeconds(end))
}
// DiffInString gets the difference in string, i18n is supported.
// 相差字符串支持i18n
func (c Carbon) DiffInString(carbon ...Carbon) string {
end := c.Now()
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
}
if c.Error != nil || end.Error != nil {
return ""
}
unit, value := c.diff(end)
return c.lang.translate(unit, value)
}
2021-09-06 12:55:07 +08:00
// DiffInStringWithAbs gets the difference in string with absolute value, i18n is supported.
2021-09-15 21:05:21 +08:00
// 相差字符串支持i18n(绝对值)
func (c Carbon) DiffInStringWithAbs(carbon ...Carbon) string {
end := c.Now()
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
}
if c.Error != nil || end.Error != nil {
return ""
}
unit, value := c.diff(end)
return c.lang.translate(unit, getAbsValue(value))
}
2021-09-16 09:52:35 +08:00
// DiffForHumans gets the difference in a human-readable format, i18n is supported.
2021-07-28 15:18:05 +08:00
// 获取对人类友好的可读格式时间差支持i18n
2021-07-23 10:57:18 +08:00
func (c Carbon) DiffForHumans(carbon ...Carbon) string {
2021-02-18 14:32:31 +08:00
end := c.Now()
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
2021-02-18 14:32:31 +08:00
}
if c.Error != nil || end.Error != nil {
return ""
}
unit, value := c.diff(end)
translation := c.lang.translate(unit, getAbsValue(value))
if unit == "now" {
return translation
}
if c.Lt(end) && len(carbon) == 0 {
return strings.Replace(c.lang.resources["ago"], "%s", translation, 1)
}
if c.Lt(end) && len(carbon) > 0 {
return strings.Replace(c.lang.resources["before"], "%s", translation, 1)
}
if c.Gt(end) && len(carbon) == 0 {
return strings.Replace(c.lang.resources["from_now"], "%s", translation, 1)
}
return strings.Replace(c.lang.resources["after"], "%s", translation, 1)
}
// diff gets the difference for unit and value.
// 获取相差单位和差值
func (c Carbon) diff(end Carbon) (unit string, value int64) {
2021-02-18 14:32:31 +08:00
switch true {
case c.DiffInYearsWithAbs(end) > 0:
unit = "year"
value = c.DiffInYears(end)
2021-02-18 14:32:31 +08:00
break
case c.DiffInMonthsWithAbs(end) > 0:
unit = "month"
value = c.DiffInMonths(end)
2021-02-18 14:32:31 +08:00
break
case c.DiffInWeeksWithAbs(end) > 0:
unit = "week"
value = c.DiffInWeeks(end)
2021-02-18 14:32:31 +08:00
break
case c.DiffInDaysWithAbs(end) > 0:
unit = "day"
value = c.DiffInDays(end)
2021-02-18 14:32:31 +08:00
break
case c.DiffInHoursWithAbs(end) > 0:
unit = "hour"
value = c.DiffInHours(end)
2021-02-18 14:32:31 +08:00
break
case c.DiffInMinutesWithAbs(end) > 0:
unit = "minute"
value = c.DiffInMinutes(end)
2021-02-18 14:32:31 +08:00
case c.DiffInSecondsWithAbs(end) > 0:
unit = "second"
value = c.DiffInSeconds(end)
2021-02-18 14:32:31 +08:00
case c.DiffInSecondsWithAbs(end) == 0:
unit = "now"
value = 0
2021-02-18 14:32:31 +08:00
}
return
2021-02-18 14:32:31 +08:00
}