-
-
Notifications
You must be signed in to change notification settings - Fork 254
/
Copy pathsetter.go
executable file
·390 lines (353 loc) · 9.76 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package carbon
import (
"time"
)
// SetLayout sets globally default layout.
// 设置全局默认布局模板
func SetLayout(layout string) *Carbon {
c := NewCarbon().SetLayout(layout)
if !c.HasError() {
DefaultLayout = layout
}
return c
}
// SetFormat sets globally default format.
// 设置全局默认格式模板
func SetFormat(format string) *Carbon {
layout := format2layout(format)
c := NewCarbon().SetLayout(layout)
if !c.HasError() {
DefaultLayout = layout
}
return c
}
// SetTimezone sets globally default timezone.
// 设置全局默认时区
func SetTimezone(name string) *Carbon {
c := NewCarbon().SetTimezone(name)
if !c.HasError() {
DefaultTimezone = name
}
return c
}
// SetLocation sets globally default location.
// 设置全局默认位置
func SetLocation(loc *time.Location) *Carbon {
c := NewCarbon().SetLocation(loc)
if !c.HasError() {
DefaultTimezone = loc.String()
}
return c
}
// SetLocale sets globally default locale.
// 设置全局默认语言区域
func SetLocale(locale string) *Carbon {
c := NewCarbon().SetLocale(locale)
if !c.HasError() {
DefaultLocale = locale
}
return c
}
// SetWeekStartsAt sets globally default start day of the week.
// 设置全局默认周起始日期
func SetWeekStartsAt(day string) *Carbon {
c := NewCarbon().SetWeekStartsAt(day)
if !c.HasError() {
DefaultWeekStartsAt = day
}
return c
}
// SetLayout sets layout.
// 设置布局模板
func (c *Carbon) SetLayout(layout string) *Carbon {
if layout == "" {
c.Error = emptyLayoutError()
}
if c.IsInvalid() {
return c
}
c.layout = layout
return c
}
// SetFormat sets format.
// 设置格式模板
func (c *Carbon) SetFormat(format string) *Carbon {
if format == "" {
c.Error = emptyFormatError()
}
if c.IsInvalid() {
return c
}
c.layout = format2layout(format)
return c
}
// SetTimezone sets timezone.
// 设置时区
func (c *Carbon) SetTimezone(name string) *Carbon {
if name == "" {
c.Error = emptyTimezoneError()
}
if c.IsInvalid() {
return c
}
c.loc, c.Error = getLocationByTimezone(name)
return c
}
// SetLocation sets location.
// 设置位置
func (c *Carbon) SetLocation(loc *time.Location) *Carbon {
if loc == nil {
c.Error = nilLocationError()
}
if c.IsInvalid() {
return c
}
c.loc = loc
return c
}
// SetLocale sets locale.
// 设置语言区域
func (c *Carbon) SetLocale(locale string) *Carbon {
if locale == "" {
c.Error = emptyLocaleError()
}
if c.IsInvalid() {
return c
}
c.lang = NewLanguage().SetLocale(locale)
c.Error = c.lang.Error
return c
}
// SetWeekStartsAt sets start day of the week.
// 设置一周起始日期
func (c *Carbon) SetWeekStartsAt(day string) *Carbon {
if day == "" {
c.Error = emptyWeekStartsDayError()
}
if c.IsInvalid() {
return c
}
if weekday, ok := weekdays[day]; ok {
c.weekStartsAt = weekday
} else {
c.Error = invalidWeekStartsAtError(day)
}
return c
}
// SetLanguage sets language.
// 设置语言对象
func (c *Carbon) SetLanguage(lang *Language) *Carbon {
if c.IsInvalid() {
return c
}
if lang == nil {
c.Error = nilLanguageError()
return c
}
c.lang.dir = lang.dir
c.lang.locale = lang.locale
c.lang.resources = lang.resources
c.lang.Error = 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 create(year, month, day, hour, minute, second, c.Nanosecond(), c.Timezone())
}
// 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 create(year, month, day, hour, minute, second, millisecond*1e6, c.Timezone())
}
// 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 create(year, month, day, hour, minute, second, microsecond*1e3, c.Timezone())
}
// 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 create(year, month, day, hour, minute, second, nanosecond, c.Timezone())
}
// 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 create(year, month, day, hour, minute, second, c.Nanosecond(), c.Timezone())
}
// 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 create(year, month, day, hour, minute, second, millisecond*1e6, c.Timezone())
}
// 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 create(year, month, day, hour, minute, second, microsecond*1e3, c.Timezone())
}
// 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 create(year, month, day, hour, minute, second, nanosecond, c.Timezone())
}
// 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 create(year, month, day, hour, minute, second, c.Nanosecond(), c.Timezone())
}
// 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 create(year, month, day, hour, minute, second, millisecond*1e6, c.Timezone())
}
// 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 create(year, month, day, hour, minute, second, microsecond*1e3, c.Timezone())
}
// 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 create(year, month, day, hour, minute, second, nanosecond, c.Timezone())
}
// SetYear sets year.
// 设置年份
func (c *Carbon) SetYear(year int) *Carbon {
if c.IsInvalid() {
return c
}
_, month, day, hour, minute, second := c.DateTime()
return create(year, month, day, hour, minute, second, c.Nanosecond(), c.Timezone())
}
// 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 create(year, month, day, hour, minute, second, c.Nanosecond(), c.Timezone())
}
// 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 create(year, month, day, hour, minute, second, c.Nanosecond(), c.Timezone())
}
// SetHour sets hour.
// 设置小时
func (c *Carbon) SetHour(hour int) *Carbon {
if c.IsInvalid() {
return c
}
year, month, day, _, minute, second := c.DateTime()
return create(year, month, day, hour, minute, second, c.Nanosecond(), c.Timezone())
}
// SetMinute sets minute.
// 设置分钟
func (c *Carbon) SetMinute(minute int) *Carbon {
if c.IsInvalid() {
return c
}
year, month, day, hour, _, second := c.DateTime()
return create(year, month, day, hour, minute, second, c.Nanosecond(), c.Timezone())
}
// SetSecond sets second.
// 设置秒数
func (c *Carbon) SetSecond(second int) *Carbon {
if c.IsInvalid() {
return c
}
year, month, day, hour, minute, _ := c.DateTime()
return create(year, month, day, hour, minute, second, c.Nanosecond(), c.Timezone())
}
// SetMillisecond sets millisecond.
// 设置毫秒
func (c *Carbon) SetMillisecond(millisecond int) *Carbon {
if c.IsInvalid() {
return c
}
year, month, day, hour, minute, second := c.DateTime()
return create(year, month, day, hour, minute, second, millisecond*1e6, c.Timezone())
}
// SetMicrosecond sets microsecond.
// 设置微秒
func (c *Carbon) SetMicrosecond(microsecond int) *Carbon {
if c.IsInvalid() {
return c
}
year, month, day, hour, minute, second := c.DateTime()
return create(year, month, day, hour, minute, second, microsecond*1e3, c.Timezone())
}
// SetNanosecond sets nanosecond.
// 设置纳秒
func (c *Carbon) SetNanosecond(nanosecond int) *Carbon {
if c.IsInvalid() {
return c
}
year, month, day, hour, minute, second := c.DateTime()
return create(year, month, day, hour, minute, second, nanosecond, c.Timezone())
}