forked from dromara/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setter.go
executable file
·302 lines (270 loc) · 7.73 KB
/
setter.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package carbon
import (
"time"
)
// SetWeekStartsAt sets start day of the week.
// 设置一周的开始日期
func (c Carbon) SetWeekStartsAt(day string) Carbon {
if c.Error != nil {
return c
}
if weekday, ok := weekdays[day]; ok {
c.weekStartsAt = weekday
}
return c
}
// SetWeekStartsAt sets start day of the week.
// 设置一周的开始日期
func SetWeekStartsAt(day string) Carbon {
return NewCarbon().SetWeekStartsAt(day)
}
// SetTimezone sets timezone.
// 设置时区
func (c Carbon) SetTimezone(name string) Carbon {
if c.Error != nil {
return c
}
c.loc, c.Error = getLocationByTimezone(name)
return c
}
// SetTimezone sets timezone.
// 设置时区
func SetTimezone(name string) Carbon {
return NewCarbon().SetTimezone(name)
}
// SetLocation sets location.
// 设置地区
func (c Carbon) SetLocation(loc *time.Location) Carbon {
if c.Error != nil {
return c
}
if loc == nil {
c.Error = invalidLocationError()
}
c.loc = loc
return c
}
// SetLocation sets location.
// 设置地区
func SetLocation(loc *time.Location) Carbon {
return NewCarbon().SetLocation(loc)
}
// SetLocale sets locale.
// 设置语言区域
func (c Carbon) SetLocale(locale string) Carbon {
if c.Error != nil {
return c
}
c.lang.SetLocale(locale)
c.Error = c.lang.Error
return c
}
// SetLocale sets locale.
// 设置语言区域
func SetLocale(locale string) Carbon {
c := NewCarbon()
c.lang.SetLocale(locale)
c.Error = c.lang.Error
return c
}
// SetDateTime sets year, month, day, hour, minute and second.
// 设置年、月、日、时、分、秒
func (c Carbon) SetDateTime(year, month, day, hour, minute, second int) Carbon {
if c.IsInvalid() {
return c
}
return c.create(year, month, day, hour, minute, second, c.Nanosecond())
}
// SetDateTimeMilli sets year, month, day, hour, minute, second and millisecond.
// 设置年、月、日、时、分、秒、毫秒
func (c Carbon) SetDateTimeMilli(year, month, day, hour, minute, second, millisecond int) Carbon {
if c.IsInvalid() {
return c
}
return c.create(year, month, day, hour, minute, second, millisecond*1e6)
}
// SetDateTimeMicro sets year, month, day, hour, minute, second and microsecond.
// 设置年、月、日、时、分、秒、微秒
func (c Carbon) SetDateTimeMicro(year, month, day, hour, minute, second, microsecond int) Carbon {
if c.IsInvalid() {
return c
}
return c.create(year, month, day, hour, minute, second, microsecond*1e3)
}
// SetDateTimeNano sets year, month, day, hour, minute, second and nanosecond.
// 设置年、月、日、时、分、秒、纳秒
func (c Carbon) SetDateTimeNano(year, month, day, hour, minute, second, nanosecond int) Carbon {
if c.IsInvalid() {
return c
}
return c.create(year, month, day, hour, minute, second, nanosecond)
}
// SetDate sets year, month and day.
// 设置年、月、日
func (c Carbon) SetDate(year, month, day int) Carbon {
if c.IsInvalid() {
return c
}
hour, minute, second := c.Time()
return c.create(year, month, day, hour, minute, second, c.Nanosecond())
}
// SetDateMilli sets year, month, day and millisecond.
// 设置年、月、日、毫秒
func (c Carbon) SetDateMilli(year, month, day, millisecond int) Carbon {
if c.IsInvalid() {
return c
}
hour, minute, second := c.Time()
return c.create(year, month, day, hour, minute, second, millisecond*1e6)
}
// SetDateMicro sets year, month, day and microsecond.
// 设置年、月、日、微秒
func (c Carbon) SetDateMicro(year, month, day, microsecond int) Carbon {
if c.IsInvalid() {
return c
}
hour, minute, second := c.Time()
return c.create(year, month, day, hour, minute, second, microsecond*1e3)
}
// SetDateNano sets year, month, day and nanosecond.
// 设置年、月、日、纳秒
func (c Carbon) SetDateNano(year, month, day, nanosecond int) Carbon {
if c.IsInvalid() {
return c
}
hour, minute, second := c.Time()
return c.create(year, month, day, hour, minute, second, nanosecond)
}
// SetTime sets hour, minute and second.
// 设置时、分、秒
func (c Carbon) SetTime(hour, minute, second int) Carbon {
if c.IsInvalid() {
return c
}
year, month, day := c.Date()
return c.create(year, month, day, hour, minute, second, c.Nanosecond())
}
// SetTimeMilli sets hour, minute, second and millisecond.
// 设置时、分、秒、毫秒
func (c Carbon) SetTimeMilli(hour, minute, second, millisecond int) Carbon {
if c.IsInvalid() {
return c
}
year, month, day := c.Date()
return c.create(year, month, day, hour, minute, second, millisecond*1e6)
}
// SetTimeMicro sets hour, minute, second and microsecond.
// 设置时、分、秒、微秒
func (c Carbon) SetTimeMicro(hour, minute, second, microsecond int) Carbon {
if c.IsInvalid() {
return c
}
year, month, day := c.Date()
return c.create(year, month, day, hour, minute, second, microsecond*1e3)
}
// SetTimeNano sets hour, minute, second and nanosecond.
// 设置、时、分、秒、纳秒
func (c Carbon) SetTimeNano(hour, minute, second, nanosecond int) Carbon {
if c.IsInvalid() {
return c
}
year, month, day := c.Date()
return c.create(year, month, day, hour, minute, second, nanosecond)
}
// SetYear sets year.
// 设置年份
func (c Carbon) SetYear(year int) Carbon {
if c.IsInvalid() {
return c
}
_, month, day, hour, minute, second := c.DateTime()
return c.create(year, month, day, hour, minute, second, c.Nanosecond())
}
// SetYearNoOverflow sets year without overflowing month.
// 设置年份(月份不溢出)
func (c Carbon) SetYearNoOverflow(year int) Carbon {
if c.IsInvalid() {
return c
}
return c.AddYearsNoOverflow(year - c.Year())
}
// SetMonth sets month.
// 设置月份
func (c Carbon) SetMonth(month int) Carbon {
if c.IsInvalid() {
return c
}
year, _, day, hour, minute, second := c.DateTime()
return c.create(year, month, day, hour, minute, second, c.Nanosecond())
}
// SetMonthNoOverflow sets month without overflowing month.
// 设置月份(月份不溢出)
func (c Carbon) SetMonthNoOverflow(month int) Carbon {
if c.IsInvalid() {
return c
}
return c.AddMonthsNoOverflow(month - c.Month())
}
// SetDay sets day.
// 设置日期
func (c Carbon) SetDay(day int) Carbon {
if c.IsInvalid() {
return c
}
year, month, _, hour, minute, second := c.DateTime()
return c.create(year, month, day, hour, minute, second, c.Nanosecond())
}
// SetHour sets hour.
// 设置小时
func (c Carbon) SetHour(hour int) Carbon {
if c.IsInvalid() {
return c
}
year, month, day, _, minute, second := c.DateTime()
return c.create(year, month, day, hour, minute, second, c.Nanosecond())
}
// SetMinute sets minute.
// 设置分钟
func (c Carbon) SetMinute(minute int) Carbon {
if c.IsInvalid() {
return c
}
year, month, day, hour, _, second := c.DateTime()
return c.create(year, month, day, hour, minute, second, c.Nanosecond())
}
// SetSecond sets second.
// 设置秒数
func (c Carbon) SetSecond(second int) Carbon {
if c.IsInvalid() {
return c
}
year, month, day, hour, minute, _ := c.DateTime()
return c.create(year, month, day, hour, minute, second, c.Nanosecond())
}
// SetMillisecond sets millisecond.
// 设置毫秒
func (c Carbon) SetMillisecond(millisecond int) Carbon {
if c.IsInvalid() {
return c
}
year, month, day, hour, minute, second := c.DateTime()
return c.create(year, month, day, hour, minute, second, millisecond*1e6)
}
// SetMicrosecond sets microsecond.
// 设置微秒
func (c Carbon) SetMicrosecond(microsecond int) Carbon {
if c.IsInvalid() {
return c
}
year, month, day, hour, minute, second := c.DateTime()
return c.create(year, month, day, hour, minute, second, microsecond*1e3)
}
// SetNanosecond sets nanosecond.
// 设置纳秒
func (c Carbon) SetNanosecond(nanosecond int) Carbon {
if c.IsInvalid() {
return c
}
year, month, day, hour, minute, second := c.DateTime()
return c.create(year, month, day, hour, minute, second, nanosecond)
}