format time string

This commit is contained in:
tickstep 2021-10-24 22:25:40 +08:00
parent 971369e968
commit 267cf937be
4 changed files with 50 additions and 2 deletions

View File

@ -17,6 +17,7 @@ import (
"fmt"
"github.com/tickstep/aliyunpan-api/aliyunpan/apierror"
"github.com/tickstep/aliyunpan/cmder"
"github.com/tickstep/aliyunpan/internal/utils"
"io/ioutil"
"os"
"path"
@ -427,7 +428,7 @@ func RunUpload(localPaths []string, savePath string, opt *UploadOptions) {
}
fmt.Printf("\n")
fmt.Printf("上传结束, 时间: %s, 总大小: %s\n", statistic.Elapsed()/1e6*1e6, converter.ConvertFileSize(statistic.TotalSize()))
fmt.Printf("上传结束, 时间: %s, 总大小: %s\n", utils.ConvertTime(statistic.Elapsed()), converter.ConvertFileSize(statistic.TotalSize()))
// 输出上传失败的文件列表
for _, failed := range failedList {

View File

@ -15,6 +15,7 @@ package panupload
import (
"fmt"
"github.com/tickstep/aliyunpan/internal/utils"
"os"
"path"
"path/filepath"
@ -285,7 +286,7 @@ func (utu *UploadTaskUnit) Run() (result *taskframework.TaskUnitRunResult) {
} else {
msg = result.ResultMessage
}
fmt.Printf("%s [%s] 文件上传结果:%s 耗时 %.2fs\n", time.Now().Format("2006-01-02 15:04:06"), utu.taskInfo.Id(), msg, time.Now().Sub(timeStart).Seconds())
fmt.Printf("%s [%s] 文件上传结果:%s 耗时 %s\n", time.Now().Format("2006-01-02 15:04:06"), utu.taskInfo.Id(), msg, utils.ConvertTime(time.Now().Sub(timeStart)))
}()
// 准备文件
utu.prepareFile()

View File

@ -16,12 +16,14 @@ package utils
import (
"compress/gzip"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http/cookiejar"
"net/url"
"strconv"
"strings"
"time"
)
// TrimPathPrefix 去除目录的前缀
@ -111,4 +113,27 @@ func parseInt(numStr string) int {
return 0
}
return num
}
func ConvertTime(t time.Duration) string {
seconds := int64(t.Seconds())
MT := int64(1 * 60)
HT := int64(1 * 60 * 60)
if seconds <= 0 {
return "0秒"
}
if seconds < MT {
return fmt.Sprintf("%d秒", seconds)
}
if seconds >= MT && seconds < HT {
return fmt.Sprintf("%d分%d秒", seconds / MT, seconds % MT)
}
if seconds >= HT {
h := seconds / HT
tmp := seconds % HT
return fmt.Sprintf("%d小时%d分%d秒", h, tmp / MT, tmp % MT)
}
return "0秒"
}

View File

@ -0,0 +1,21 @@
package utils
import (
"fmt"
"testing"
"time"
)
func TestConvertTime(t *testing.T) {
seconds := time.Duration(50) * time.Second
fmt.Println(ConvertTime(seconds))
seconds = time.Duration(150) * time.Second
fmt.Println(ConvertTime(seconds))
seconds = time.Duration(3600) * time.Second
fmt.Println(ConvertTime(seconds))
seconds = time.Duration(1246852) * time.Second
fmt.Println(ConvertTime(seconds))
}