diff --git a/internal/command/upload.go b/internal/command/upload.go index b7efe55..f32e632 100644 --- a/internal/command/upload.go +++ b/internal/command/upload.go @@ -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 { diff --git a/internal/functions/panupload/upload_task_unit.go b/internal/functions/panupload/upload_task_unit.go index 8347005..17eac18 100644 --- a/internal/functions/panupload/upload_task_unit.go +++ b/internal/functions/panupload/upload_task_unit.go @@ -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() diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 6dcd926..0a7bf52 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -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秒" } \ No newline at end of file diff --git a/internal/utils/utils_test.go b/internal/utils/utils_test.go new file mode 100644 index 0000000..c170496 --- /dev/null +++ b/internal/utils/utils_test.go @@ -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)) +}