-
Notifications
You must be signed in to change notification settings - Fork 8
/
quarter.go
86 lines (75 loc) · 1.73 KB
/
quarter.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
package synchro
import "time"
type Quarter[T TimeZone] struct {
year int
number int
}
// Quarter gets current quarter.
func (t Time[T]) Quarter() Quarter[T] {
return Quarter[T]{
year: t.Year(),
number: numberOfQuarter(t.Month()),
}
}
// Year returns the year in which q occurs.
func (q Quarter[T]) Year() int { return q.year }
// Number returns the number of quarter.
func (q Quarter[T]) Number() int { return q.number }
// Start returns start time in the quarter.
func (q Quarter[T]) Start() Time[T] {
year, quarter, day := q.year, q.number, 1
return New[T](year, time.Month(3*quarter-2), day, 0, 0, 0, 0)
}
// End returns end time in the quarter.
func (q Quarter[T]) End() Time[T] {
year, quarter := q.year, q.number
day := 31
switch quarter {
case 2, 3:
day = 30
}
return New[T](year, time.Month(3*quarter), day, 23, 59, 59, 999999999)
}
// After reports whether the Quarter instant q is after u.
func (q Quarter[T]) After(u Quarter[T]) bool {
if q.year > u.year {
return true
}
if q.number > u.number {
return true
}
return false
}
// Before reports whether the Quarter instant q is before u.
func (q Quarter[T]) Before(u Quarter[T]) bool {
if q.year < u.year {
return true
}
if q.number < u.number {
return true
}
return false
}
// Compare compares the Quarter instant q with u. If q is before u, it returns -1;
// if q is after u, it returns +1; if they're the same, it returns 0.
func (q Quarter[T]) Compare(u Quarter[T]) int {
if q.year == u.year && q.number == u.number {
return 0
}
if q.Before(u) {
return -1
}
return 1
}
func numberOfQuarter(month time.Month) int {
if month >= time.October {
return 4
}
if month >= time.July {
return 3
}
if month >= time.April {
return 2
}
return 1
}