-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
51 lines (44 loc) · 1.07 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
package es
import (
"fmt"
"time"
)
type Duration time.Duration
const (
Second = Duration(time.Second)
Minute = Duration(time.Minute)
Hour = Duration(time.Hour)
Day = Duration(24 * time.Hour)
Week = Duration(7 * Day)
Month = Duration(30 * Day)
Year = Duration(365 * Day)
)
func (d Duration) MarshalJSON() ([]byte, error) {
str := `"` + d.String() + `"`
return []byte(str), nil
}
func (d Duration) String() string {
var minusSign string
if d < 0 {
d, minusSign = -d, "-"
}
if d >= Year {
return fmt.Sprintf("%s%fy", minusSign, float64(d)/float64(Year))
}
if d >= Month {
return fmt.Sprintf("%s%fM", minusSign, float64(d)/float64(Month))
}
if d >= Week {
return fmt.Sprintf("%s%fw", minusSign, float64(d)/float64(Week))
}
if d >= Day {
return fmt.Sprintf("%s%fd", minusSign, float64(d)/float64(Day))
}
if d >= Hour {
return fmt.Sprintf("%s%fh", minusSign, float64(d)/float64(Hour))
}
if d >= Minute {
return fmt.Sprintf("%s%fm", minusSign, float64(d)/float64(Minute))
}
return fmt.Sprintf("%s%fs", minusSign, float64(d)/float64(Second))
}