-
Notifications
You must be signed in to change notification settings - Fork 22
/
rate.go
55 lines (48 loc) · 789 Bytes
/
rate.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
package xtables
import "strconv"
// time related
type Unit int
const (
_ Unit = iota
Microsecond
Millisecond
Second
Minute
Hour
Day
BPS // bytes per second
KBPS // kilo bytes per second
MBPS // million bytes per second
)
type Rate struct {
Rate int
Unit Unit
}
func (rate Rate) String() string {
unit := "second"
switch rate.Unit {
case Minute:
unit = "minute"
case Hour:
unit = "hour"
case Day:
unit = "day"
}
return strconv.Itoa(rate.Rate) + "/" + unit
}
type RateFloat struct {
Rate float64
Unit Unit
}
func (rateFloat RateFloat) Sting() string {
unit := "second"
switch rateFloat.Unit {
case Microsecond:
unit = "us"
case Millisecond:
unit = "ms"
case Second:
unit = "s"
}
return strconv.FormatFloat(rateFloat.Rate, 'f', 2, 64) + unit
}