2021-02-18 14:32:31 +08:00
|
|
|
|
package carbon
|
|
|
|
|
|
2021-02-23 09:32:55 +08:00
|
|
|
|
import "strings"
|
2021-02-18 14:32:31 +08:00
|
|
|
|
|
|
|
|
|
// DiffInYears 相差多少年
|
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()
|
2021-07-23 10:57:18 +08:00
|
|
|
|
if len(carbon) == 1 {
|
|
|
|
|
end = carbon[0]
|
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-02-23 09:32:55 +08:00
|
|
|
|
// DiffInYearsWithAbs 相差多少年(绝对值)
|
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()
|
2021-07-23 10:57:18 +08:00
|
|
|
|
if len(carbon) == 1 {
|
|
|
|
|
end = carbon[0]
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
|
|
|
|
return getAbsValue(c.DiffInYears(end))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DiffInMonths 相差多少月
|
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()
|
2021-07-23 10:57:18 +08:00
|
|
|
|
if len(carbon) == 1 {
|
|
|
|
|
end = carbon[0]
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
|
|
|
|
dy, dm, dd := end.Year()-c.Year(), end.Month()-c.Month(), end.Day()-c.Day()
|
|
|
|
|
if dd < 0 {
|
|
|
|
|
dm = dm - 1
|
|
|
|
|
}
|
|
|
|
|
if dy == 0 && dm == 0 {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
if dy == 0 && dm != 0 && dd != 0 {
|
|
|
|
|
if int(end.DiffInHoursWithAbs(c)) < c.DaysInMonth()*HoursPerDay {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
return int64(dm)
|
|
|
|
|
}
|
|
|
|
|
return int64(dy*MonthsPerYear + dm)
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-23 09:32:55 +08:00
|
|
|
|
// DiffInMonthsWithAbs 相差多少月(绝对值)
|
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()
|
2021-07-23 10:57:18 +08:00
|
|
|
|
if len(carbon) == 1 {
|
|
|
|
|
end = carbon[0]
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
|
|
|
|
return getAbsValue(c.DiffInMonths(end))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DiffInWeeks 相差多少周
|
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()
|
2021-07-23 10:57:18 +08:00
|
|
|
|
if len(carbon) == 1 {
|
|
|
|
|
end = carbon[0]
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
|
|
|
|
return c.DiffInDays(end) / DaysPerWeek
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-23 09:32:55 +08:00
|
|
|
|
// DiffInWeeksWithAbs 相差多少周(绝对值)
|
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()
|
2021-07-23 10:57:18 +08:00
|
|
|
|
if len(carbon) == 1 {
|
|
|
|
|
end = carbon[0]
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
|
|
|
|
return getAbsValue(c.DiffInWeeks(end))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DiffInDays 相差多少天
|
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()
|
2021-07-23 10:57:18 +08:00
|
|
|
|
if len(carbon) == 1 {
|
|
|
|
|
end = carbon[0]
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
|
|
|
|
return c.DiffInSeconds(end) / SecondsPerDay
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-23 09:32:55 +08:00
|
|
|
|
// DiffInDaysWithAbs 相差多少天(绝对值)
|
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()
|
2021-07-23 10:57:18 +08:00
|
|
|
|
if len(carbon) == 1 {
|
|
|
|
|
end = carbon[0]
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
|
|
|
|
return getAbsValue(c.DiffInDays(end))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DiffInHours 相差多少小时
|
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()
|
2021-07-23 10:57:18 +08:00
|
|
|
|
if len(carbon) == 1 {
|
|
|
|
|
end = carbon[0]
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
|
|
|
|
return c.DiffInSeconds(end) / SecondsPerHour
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-23 09:32:55 +08:00
|
|
|
|
// DiffInHoursWithAbs 相差多少小时(绝对值)
|
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()
|
2021-07-23 10:57:18 +08:00
|
|
|
|
if len(carbon) == 1 {
|
|
|
|
|
end = carbon[0]
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
|
|
|
|
return getAbsValue(c.DiffInHours(end))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DiffInMinutes 相差多少分钟
|
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()
|
2021-07-23 10:57:18 +08:00
|
|
|
|
if len(carbon) == 1 {
|
|
|
|
|
end = carbon[0]
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
|
|
|
|
return c.DiffInSeconds(end) / SecondsPerMinute
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-23 09:32:55 +08:00
|
|
|
|
// DiffInMinutesWithAbs 相差多少分钟(绝对值)
|
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()
|
2021-07-23 10:57:18 +08:00
|
|
|
|
if len(carbon) == 1 {
|
|
|
|
|
end = carbon[0]
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
|
|
|
|
return getAbsValue(c.DiffInMinutes(end))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DiffInSeconds 相差多少秒
|
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()
|
2021-07-23 10:57:18 +08:00
|
|
|
|
if len(carbon) == 1 {
|
|
|
|
|
end = carbon[0]
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
|
|
|
|
return end.ToTimestamp() - c.ToTimestamp()
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-23 09:32:55 +08:00
|
|
|
|
// DiffInSecondsWithAbs 相差多少秒(绝对值)
|
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()
|
2021-07-23 10:57:18 +08:00
|
|
|
|
if len(carbon) == 1 {
|
|
|
|
|
end = carbon[0]
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
|
|
|
|
return getAbsValue(c.DiffInSeconds(end))
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 20:47:22 +08:00
|
|
|
|
// DiffForHumans 获取对人类友好的可读格式时间差,支持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()
|
2021-07-23 10:57:18 +08:00
|
|
|
|
if len(carbon) == 1 {
|
|
|
|
|
end = carbon[0]
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
2021-07-23 10:57:18 +08:00
|
|
|
|
unit, diff := "", int64(0)
|
2021-02-18 14:32:31 +08:00
|
|
|
|
switch true {
|
|
|
|
|
case c.DiffInYearsWithAbs(end) > 0:
|
|
|
|
|
unit = "year"
|
|
|
|
|
diff = c.DiffInYearsWithAbs(end)
|
|
|
|
|
break
|
|
|
|
|
case c.DiffInMonthsWithAbs(end) > 0:
|
|
|
|
|
unit = "month"
|
|
|
|
|
diff = c.DiffInMonthsWithAbs(end)
|
|
|
|
|
break
|
|
|
|
|
case c.DiffInWeeksWithAbs(end) > 0:
|
|
|
|
|
unit = "week"
|
|
|
|
|
diff = c.DiffInWeeksWithAbs(end)
|
|
|
|
|
break
|
|
|
|
|
case c.DiffInDaysWithAbs(end) > 0:
|
|
|
|
|
unit = "day"
|
|
|
|
|
diff = c.DiffInDaysWithAbs(end)
|
|
|
|
|
break
|
|
|
|
|
case c.DiffInHoursWithAbs(end) > 0:
|
|
|
|
|
unit = "hour"
|
|
|
|
|
diff = c.DiffInHoursWithAbs(end)
|
|
|
|
|
break
|
|
|
|
|
case c.DiffInMinutesWithAbs(end) > 0:
|
|
|
|
|
unit = "minute"
|
|
|
|
|
diff = c.DiffInMinutesWithAbs(end)
|
|
|
|
|
case c.DiffInSecondsWithAbs(end) > 0:
|
|
|
|
|
unit = "second"
|
|
|
|
|
diff = c.DiffInSecondsWithAbs(end)
|
|
|
|
|
case c.DiffInSecondsWithAbs(end) == 0:
|
|
|
|
|
unit = "now"
|
|
|
|
|
diff = 0
|
|
|
|
|
return c.Lang.translate(unit, diff)
|
|
|
|
|
}
|
|
|
|
|
translation := c.Lang.translate(unit, diff)
|
2021-07-23 10:57:18 +08:00
|
|
|
|
if c.Lt(end) && len(carbon) == 0 {
|
2021-02-18 14:32:31 +08:00
|
|
|
|
return strings.Replace(c.Lang.resources["ago"], "%s", translation, 1)
|
|
|
|
|
}
|
2021-07-23 10:57:18 +08:00
|
|
|
|
if c.Lt(end) && len(carbon) == 1 {
|
2021-02-18 14:32:31 +08:00
|
|
|
|
return strings.Replace(c.Lang.resources["before"], "%s", translation, 1)
|
|
|
|
|
}
|
2021-07-23 10:57:18 +08:00
|
|
|
|
if c.Gt(end) && len(carbon) == 0 {
|
2021-02-18 14:32:31 +08:00
|
|
|
|
return strings.Replace(c.Lang.resources["from_now"], "%s", translation, 1)
|
|
|
|
|
}
|
2021-07-23 10:57:18 +08:00
|
|
|
|
if c.Gt(end) && len(carbon) == 1 {
|
2021-02-18 14:32:31 +08:00
|
|
|
|
return strings.Replace(c.Lang.resources["after"], "%s", translation, 1)
|
|
|
|
|
}
|
|
|
|
|
return translation
|
|
|
|
|
}
|