Skip to content

Commit

Permalink
add KTime.FormatDuration
Browse files Browse the repository at this point in the history
  • Loading branch information
kakuilan committed Sep 24, 2023
1 parent 0d4a714 commit 6223e4e
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
8 changes: 8 additions & 0 deletions data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,14 @@ var tesUrl39 = "https://www.baidu.com/"
var tesUrl40 = "https://www.w3.org/"
var tesUrl41 = "http://test.loc/imgs/1234/abc/230223/d319bec8fbd9e64.png?jwt=eyJhbGciOiJIUzI1Nc8&mode=fit&width=100&height=100"

// 时长
var testDuration0 = 0
var testDuration1 = 128 * time.Hour
var testDuration2 = 45 * time.Minute
var testDuration3 = 23 * time.Second
var testDuration4 = 86513.5
var testDuration5 = (1354 * time.Hour) + (22 * time.Minute) + (13 * time.Second) + (55 * time.Millisecond) + (100 * time.Microsecond)

// 下载文件
var downloadfile01 = "./testdata/download/test001/file001"

Expand Down
35 changes: 35 additions & 0 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kgo

import (
"errors"
"fmt"
"strings"
"time"
)
Expand Down Expand Up @@ -364,3 +365,37 @@ func (kt *LkkTime) IsDate2time(str string) (bool, int64) {

return true, tim
}

// FormatDuration 格式化时长为字符串(如*h*m*s);其中t为时长,默认为秒;colon为是否使用冒号.
func (kt *LkkTime) FormatDuration(t interface{}, colon bool) string {
var dr time.Duration
var res string

if v, ok := t.(time.Duration); ok {
dr = v
} else {
i := toInt(t)
dr = time.Second * time.Duration(i)
}

if colon {
h := dr / time.Hour
dr -= h * time.Hour
m := dr / time.Minute
dr -= m * time.Minute
s := dr / time.Second
res = fmt.Sprintf("%d:%02d:%02d", h, m, s)
} else {
s := dr.Round(time.Millisecond).String()
if strings.HasSuffix(s, "m0s") {
s = s[:len(s)-2]
}
if strings.HasSuffix(s, "h0m") {
s = s[:len(s)-2]
}

res = s
}

return res
}
49 changes: 49 additions & 0 deletions time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,52 @@ func BenchmarkTime_IsDate2time(b *testing.B) {
_, _ = KTime.IsDate2time(strTime7)
}
}

func TestTime_FormatDuration(t *testing.T) {
var res1, res2 string

ds := []time.Duration{
testDuration1 + testDuration2 + testDuration3,
testDuration1 + testDuration2,
testDuration1 + testDuration3,
testDuration2 + testDuration3,
testDuration1,
testDuration2,
testDuration3,
}
for _, d := range ds {
res1 = KTime.FormatDuration(d, true)
assert.Contains(t, res1, ":")
res2 = KTime.FormatDuration(d, false)
assert.NotEmpty(t, res2)
}

res1 = KTime.FormatDuration(testDuration0, true)
res2 = KTime.FormatDuration(testDuration0, false)
assert.Equal(t, res1, "0:00:00")
assert.Equal(t, res2, "0s")

res1 = KTime.FormatDuration(testDuration4, true)
res2 = KTime.FormatDuration(testDuration4, false)
assert.Equal(t, res1, "24:01:53")
assert.Equal(t, res2, "24h1m53s")

res1 = KTime.FormatDuration(testDuration5, true)
res2 = KTime.FormatDuration(testDuration5, false)
assert.Equal(t, res1, "1354:22:13")
assert.Equal(t, res2, "1354h22m13.055s")
}

func BenchmarkTime_FormatDuration0(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = KTime.FormatDuration(testDuration5, false)
}
}

func BenchmarkTime_FormatDuration1(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = KTime.FormatDuration(testDuration5, true)
}
}

0 comments on commit 6223e4e

Please sign in to comment.