forked from dromara/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
season.go
executable file
·115 lines (107 loc) · 2.54 KB
/
season.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
113
114
115
package carbon
import (
"strings"
"time"
)
// Season get season name according to the meteorological division method, i18n is supported
// 获取当前季节(以气象划分),支持i18n
func (c Carbon) Season() string {
if c.IsInvalid() {
return ""
}
if len(c.Lang.resources) == 0 {
c.Lang.SetLocale(defaultLocale)
}
index := -1
switch {
case c.Month() == 3 || c.Month() == 4 || c.Month() == 5:
index = 0
case c.Month() == 6 || c.Month() == 7 || c.Month() == 8:
index = 1
case c.Month() == 9 || c.Month() == 10 || c.Month() == 11:
index = 2
case c.Month() == 12 || c.Month() == 1 || c.Month() == 2:
index = 3
}
if seasons, ok := c.Lang.resources["seasons"]; ok {
slice := strings.Split(seasons, "|")
if len(slice) == 4 {
return slice[index]
}
}
return ""
}
// StartOfSeason return a Carbon instance for start of the season
// 本季节开始时间
func (c Carbon) StartOfSeason() Carbon {
if c.IsInvalid() {
return c
}
if c.Month() == 1 || c.Month() == 2 {
c.Time = time.Date(c.Year()-1, time.Month(12), 1, 0, 0, 0, 0, c.Loc)
return c
}
c.Time = time.Date(c.Year(), time.Month(c.Month()/3*3), 1, 0, 0, 0, 0, c.Loc)
return c
}
// EndOfSeason return a Carbon instance for end of the season
// 本季节结束时间
func (c Carbon) EndOfSeason() Carbon {
if c.IsInvalid() {
return c
}
if c.Month() == 1 || c.Month() == 2 {
c.Time = time.Date(c.Year(), time.Month(2), 1, 23, 59, 59, 999999999, c.Loc).AddDate(0, 1, -1)
return c
}
if c.Month() == 12 {
c.Time = time.Date(c.Year()+1, time.Month(2), 1, 23, 59, 59, 999999999, c.Loc).AddDate(0, 1, -1)
return c
}
c.Time = time.Date(c.Year(), time.Month(c.Month()/3*3+2), 1, 23, 59, 59, 999999999, c.Loc).AddDate(0, 1, -1)
return c
}
// IsSpring whether is spring
// 是否是春季
func (c Carbon) IsSpring() bool {
if c.IsInvalid() {
return false
}
if c.Month() == 3 || c.Month() == 4 || c.Month() == 5 {
return true
}
return false
}
// IsSummer whether is summer
// 是否是夏季
func (c Carbon) IsSummer() bool {
if c.IsInvalid() {
return false
}
if c.Month() == 6 || c.Month() == 7 || c.Month() == 8 {
return true
}
return false
}
// IsAutumn whether is autumn
// 是否是秋季
func (c Carbon) IsAutumn() bool {
if c.IsInvalid() {
return false
}
if c.Month() == 9 || c.Month() == 10 || c.Month() == 11 {
return true
}
return false
}
// IsWinter whether is winter
// 是否是冬季
func (c Carbon) IsWinter() bool {
if c.IsInvalid() {
return false
}
if c.Month() == 12 || c.Month() == 1 || c.Month() == 2 {
return true
}
return false
}