/
util.go
112 lines (98 loc) · 2.52 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
Copyright AppsCode Inc. and Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package restic
import (
"fmt"
"strings"
)
func convertSizeToBytes(dataSize string) (float64, error) {
var size float64
switch {
case strings.HasSuffix(dataSize, "TiB"):
_, err := fmt.Sscanf(dataSize, "%f TiB", &size)
if err != nil {
return 0, err
}
return size * (1 << 40), nil
case strings.HasSuffix(dataSize, "GiB"):
_, err := fmt.Sscanf(dataSize, "%f GiB", &size)
if err != nil {
return 0, err
}
return size * (1 << 30), nil
case strings.HasSuffix(dataSize, "MiB"):
_, err := fmt.Sscanf(dataSize, "%f MiB", &size)
if err != nil {
return 0, err
}
return size * (1 << 20), nil
case strings.HasSuffix(dataSize, "KiB"):
_, err := fmt.Sscanf(dataSize, "%f KiB", &size)
if err != nil {
return 0, err
}
return size * (1 << 10), nil
default:
_, err := fmt.Sscanf(dataSize, "%f B", &size)
if err != nil {
return 0, err
}
return size, nil
}
}
func convertTimeToSeconds(processingTime string) (uint64, error) {
var h, m, s uint64
parts := strings.Split(processingTime, ":")
if len(parts) == 3 {
_, err := fmt.Sscanf(processingTime, "%d:%d:%d", &h, &m, &s)
if err != nil {
return 0, err
}
} else if len(parts) == 2 {
_, err := fmt.Sscanf(processingTime, "%d:%d", &m, &s)
if err != nil {
return 0, err
}
} else {
_, err := fmt.Sscanf(processingTime, "%d", &s)
if err != nil {
return 0, err
}
}
return h*3600 + m*60 + s, nil
}
func formatBytes(c uint64) string {
b := float64(c)
switch {
case c > 1<<40:
return fmt.Sprintf("%.3f TiB", b/(1<<40))
case c > 1<<30:
return fmt.Sprintf("%.3f GiB", b/(1<<30))
case c > 1<<20:
return fmt.Sprintf("%.3f MiB", b/(1<<20))
case c > 1<<10:
return fmt.Sprintf("%.3f KiB", b/(1<<10))
default:
return fmt.Sprintf("%d B", c)
}
}
func formatSeconds(sec uint64) string {
hours := sec / 3600
sec -= hours * 3600
min := sec / 60
sec -= min * 60
if hours > 0 {
return fmt.Sprintf("%d:%02d:%02d", hours, min, sec)
}
return fmt.Sprintf("%d:%02d", min, sec)
}