Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Feb 22, 2024
1 parent 92c30c9 commit 11f46e7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
32 changes: 32 additions & 0 deletions datebin/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,38 @@ func Test_Diff_DurationBetweens(t *testing.T) {
}
}

func Test_Diff_DurationBetweenAbs(t *testing.T) {
eq := assertEqualT(t)

tests := []struct {
index string
date1 string
date2 string
check string
}{
{
index: "index-1",
date1: "2023-06-06 21:15:12",
date2: "2023-06-05 01:35:00",
check: "43h40m12s",
},
{
index: "index-2",
date1: "2022-07-05 21:15:12",
date2: "2022-07-12 12:17:32",
check: "159h2m20s",
},
}

for _, td := range tests {
check := Parse(td.date1).
Diff(Parse(td.date2)).
DurationBetweenAbs()

eq(check.String(), td.check, "failed Diff DurationBetweenAbs, index "+td.index)
}
}

func Test_Diff_DurationBetweensAbs(t *testing.T) {
eq := assertEqualT(t)

Expand Down
28 changes: 13 additions & 15 deletions datebin/diff_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,38 +180,36 @@ func (this DiffTime) DurationBetweens() (days, hours, minutes, seconds int) {
return
}

// 计算两个日期之间的持续时间,绝对值
// get abs Duration data
func (this DiffTime) DurationBetweenAbs() time.Duration {
return this.End.time.Sub(this.Start.time).Abs()
}

// 返回持续时间为人类可读的数据,绝对值
// return abs Duration datas
func (this DiffTime) DurationBetweensAbs() (days, hours, minutes, seconds int) {
days, hours, minutes, seconds = this.DurationBetweens()
duration := this.DurationBetweenAbs()

if days < 0 {
days = -days
}
if hours < 0 {
hours = -hours
}
if minutes < 0 {
minutes = -minutes
}
if seconds < 0 {
seconds = -seconds
}
days = int(duration.Hours() / 24)
hours = int(duration.Hours()) % 24
minutes = int(duration.Minutes()) % 60
seconds = int(duration.Seconds()) % 60

return
}

// 获取格式化
// get diff Formatter
func (this DiffTime) Formatter() Formatter {
return NewFormatter().FromSecond(this.SecondsAbs())
return NewFormatter().FromSecond(this.Seconds())
}

// 格式化输出
// output format data
func (this DiffTime) Format(str string) string {
// 格式化
formatter := NewFormatter().FromSecond(this.SecondsAbs())
formatter := NewFormatter().FromSecond(this.Seconds())

// 使用周数和天数
weeks, days := formatter.WeekAndDay()
Expand Down

0 comments on commit 11f46e7

Please sign in to comment.