diff --git a/data_test.go b/data_test.go index 79a3400..a808ce5 100644 --- a/data_test.go +++ b/data_test.go @@ -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" diff --git a/time.go b/time.go index c34bdff..c240d3e 100644 --- a/time.go +++ b/time.go @@ -2,6 +2,7 @@ package kgo import ( "errors" + "fmt" "strings" "time" ) @@ -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 +} diff --git a/time_test.go b/time_test.go index 91f9070..a05265e 100644 --- a/time_test.go +++ b/time_test.go @@ -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) + } +}