-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
67 lines (52 loc) · 1.34 KB
/
time.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
package unit
import (
"fmt"
"time"
"github.com/darabuchi/utils/language"
"github.com/darabuchi/utils/xtime"
)
func Duration2Str(t time.Duration) string {
return newDuration(t).String()
}
func Duration2StrWithLanguage(t time.Duration, languageCode language.LanguageCode) string {
return newDuration(t).StringWithLanguage(languageCode)
}
type duration struct {
day, hour, min, second, millisecond, duration time.Duration
}
func newDuration(t time.Duration) *duration {
p := &duration{}
if t > xtime.Day {
p.day = t / xtime.Day
}
t = t - p.day*xtime.Day
if t > xtime.Hour {
p.hour = t / xtime.Hour
}
t = t - p.hour*xtime.Hour
if t > xtime.Minute {
p.min = t / xtime.Minute
}
t = t - p.min*xtime.Minute
if t > xtime.Second {
p.second = t / xtime.Second
}
t = t - p.second*xtime.Second
if t > xtime.Millisecond {
p.millisecond = t / xtime.Millisecond
}
t = t - p.millisecond*xtime.Millisecond
p.duration = t
return p
}
func (p *duration) String() string {
return fmt.Sprintf("%dd%dh%dm%ds%sms", p.day, p.hour, p.min, p.second, p.millisecond)
}
func (p *duration) StringWithLanguage(code language.LanguageCode) string {
return fmt.Sprintf("%d%s%d%s%d%s%d%s",
p.day, GetUnitCode2Show(Day, code),
p.hour, GetUnitCode2Show(Hour, code),
p.min, GetUnitCode2Show(Minute, code),
p.second, GetUnitCode2Show(Second, code),
)
}