forked from dromara/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modifier.go
executable file
·201 lines (181 loc) · 5.14 KB
/
modifier.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package carbon
// StartOfCentury returns a Carbon instance for start of the century.
// 本世纪开始时间
func (c Carbon) StartOfCentury() Carbon {
if c.IsInvalid() {
return c
}
return c.create(c.Year()/YearsPerCentury*YearsPerCentury, 1, 1, 0, 0, 0, 0)
}
// EndOfCentury returns a Carbon instance for end of the century.
// 本世纪结束时间
func (c Carbon) EndOfCentury() Carbon {
if c.IsInvalid() {
return c
}
return c.create(c.Year()/YearsPerCentury*YearsPerCentury+99, 12, 31, 23, 59, 59, 999999999)
}
// StartOfDecade returns a Carbon instance for start of the decade.
// 本年代开始时间
func (c Carbon) StartOfDecade() Carbon {
if c.IsInvalid() {
return c
}
return c.create(c.Year()/YearsPerDecade*YearsPerDecade, 1, 1, 0, 0, 0, 0)
}
// EndOfDecade returns a Carbon instance for end of the decade.
// 本年代结束时间
func (c Carbon) EndOfDecade() Carbon {
if c.IsInvalid() {
return c
}
return c.create(c.Year()/YearsPerDecade*YearsPerDecade+9, 12, 31, 23, 59, 59, 999999999)
}
// StartOfYear returns a Carbon instance for start of the year.
// 本年开始时间
func (c Carbon) StartOfYear() Carbon {
if c.IsInvalid() {
return c
}
return c.create(c.Year(), 1, 1, 0, 0, 0, 0)
}
// EndOfYear returns a Carbon instance for end of the year.
// 本年结束时间
func (c Carbon) EndOfYear() Carbon {
if c.IsInvalid() {
return c
}
return c.create(c.Year(), 12, 31, 23, 59, 59, 999999999)
}
// StartOfQuarter returns a Carbon instance for start of the quarter.
// 本季度开始时间
func (c Carbon) StartOfQuarter() Carbon {
if c.IsInvalid() {
return c
}
year, quarter, day := c.Year(), c.Quarter(), 1
return c.create(year, 3*quarter-2, day, 0, 0, 0, 0)
}
// EndOfQuarter returns a Carbon instance for end of the quarter.
// 本季度结束时间
func (c Carbon) EndOfQuarter() Carbon {
if c.IsInvalid() {
return c
}
year, quarter, day := c.Year(), c.Quarter(), 30
switch quarter {
case 1, 4:
day = 31
case 2, 3:
day = 30
}
return c.create(year, 3*quarter, day, 23, 59, 59, 999999999)
}
// StartOfMonth returns a Carbon instance for start of the month.
// 本月开始时间
func (c Carbon) StartOfMonth() Carbon {
if c.IsInvalid() {
return c
}
year, month, _ := c.Date()
return c.create(year, month, 1, 0, 0, 0, 0)
}
// EndOfMonth returns a Carbon instance for end of the month.
// 本月结束时间
func (c Carbon) EndOfMonth() Carbon {
if c.IsInvalid() {
return c
}
year, month, _ := c.Date()
return c.create(year, month+1, 0, 23, 59, 59, 999999999)
}
// StartOfWeek returns a Carbon instance for start of the week.
// 本周开始时间
func (c Carbon) StartOfWeek() Carbon {
if c.IsInvalid() {
return c
}
dayOfWeek, weekStartsAt := c.DayOfWeek(), int(c.weekStartsAt)
return c.SubDays((DaysPerWeek + dayOfWeek - weekStartsAt) % DaysPerWeek).StartOfDay()
}
// EndOfWeek returns a Carbon instance for end of the week.
// 本周结束时间
func (c Carbon) EndOfWeek() Carbon {
if c.IsInvalid() {
return c
}
dayOfWeek, weekEndsAt := c.DayOfWeek(), int(c.weekStartsAt)+DaysPerWeek-1
return c.AddDays((DaysPerWeek - dayOfWeek + weekEndsAt) % DaysPerWeek).EndOfDay()
}
// StartOfDay returns a Carbon instance for start of the day.
// 本日开始时间
func (c Carbon) StartOfDay() Carbon {
if c.IsInvalid() {
return c
}
year, month, day := c.Date()
return c.create(year, month, day, 0, 0, 0, 0)
}
// EndOfDay returns a Carbon instance for end of the day.
// 本日结束时间
func (c Carbon) EndOfDay() Carbon {
if c.IsInvalid() {
return c
}
year, month, day := c.Date()
return c.create(year, month, day, 23, 59, 59, 999999999)
}
// StartOfHour returns a Carbon instance for start of the hour.
// 小时开始时间
func (c Carbon) StartOfHour() Carbon {
if c.IsInvalid() {
return c
}
year, month, day := c.Date()
return c.create(year, month, day, c.Hour(), 0, 0, 0)
}
// EndOfHour returns a Carbon instance for end of the hour.
// 小时结束时间
func (c Carbon) EndOfHour() Carbon {
if c.IsInvalid() {
return c
}
year, month, day := c.Date()
return c.create(year, month, day, c.Hour(), 59, 59, 999999999)
}
// StartOfMinute returns a Carbon instance for start of the minute.
// 分钟开始时间
func (c Carbon) StartOfMinute() Carbon {
if c.IsInvalid() {
return c
}
year, month, day, hour, minute, _ := c.DateTime()
return c.create(year, month, day, hour, minute, 0, 0)
}
// EndOfMinute returns a Carbon instance for end of the minute.
// 分钟结束时间
func (c Carbon) EndOfMinute() Carbon {
if c.IsInvalid() {
return c
}
year, month, day, hour, minute, _ := c.DateTime()
return c.create(year, month, day, hour, minute, 59, 999999999)
}
// StartOfSecond returns a Carbon instance for start of the second.
// 秒开始时间
func (c Carbon) StartOfSecond() Carbon {
if c.IsInvalid() {
return c
}
year, month, day, hour, minute, second := c.DateTime()
return c.create(year, month, day, hour, minute, second, 0)
}
// EndOfSecond returns a Carbon instance for end of the second.
// 秒结束时间
func (c Carbon) EndOfSecond() Carbon {
if c.IsInvalid() {
return c
}
year, month, day, hour, minute, second := c.DateTime()
return c.create(year, month, day, hour, minute, second, 999999999)
}