Skip to content

Commit

Permalink
Implement Abs method
Browse files Browse the repository at this point in the history
  • Loading branch information
gouguoyin committed Mar 8, 2024
1 parent 076fbdc commit 9e7386a
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions difference.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import (
"time"
)

const (
minDuration time.Duration = -1 << 63
maxDuration time.Duration = 1<<63 - 1
)

// DiffInYears gets the difference in years.
// 相差多少年
func (c Carbon) DiffInYears(carbon ...Carbon) int64 {
Expand Down Expand Up @@ -202,14 +207,11 @@ func (c Carbon) DiffInDuration(carbon ...Carbon) time.Duration {
// DiffAbsInDuration gets the difference in duration with absolute value.
// 相差时长(绝对值)
func (c Carbon) DiffAbsInDuration(carbon ...Carbon) time.Duration {
end := c.Now()
if c.IsSetTestNow() {
end = CreateFromTimestampNano(c.testNow, c.Location())
}
if len(carbon) > 0 {
end = carbon[0]
d := c.DiffInDuration(carbon...)
if d >= 0 {
return d
}
return end.StdTime().Sub(c.StdTime()).Abs()
return -d
}

// DiffForHumans gets the difference in a human-readable format, i18n is supported.
Expand Down

0 comments on commit 9e7386a

Please sign in to comment.