@@ -13,7 +13,7 @@ func (c Carbon) StdTime() time.Time {
13
13
// DaysInYear gets total days in year like 365.
14
14
// 获取本年的总天数
15
15
func (c Carbon ) DaysInYear () int {
16
- if c .IsInvalid () {
16
+ if c .Error != nil {
17
17
return 0
18
18
}
19
19
if c .IsLeapYear () {
@@ -25,7 +25,7 @@ func (c Carbon) DaysInYear() int {
25
25
// DaysInMonth gets total days in month like 30.
26
26
// 获取本月的总天数
27
27
func (c Carbon ) DaysInMonth () int {
28
- if c .IsInvalid () {
28
+ if c .Error != nil {
29
29
return 0
30
30
}
31
31
return c .EndOfMonth ().time .In (c .loc ).Day ()
@@ -34,7 +34,7 @@ func (c Carbon) DaysInMonth() int {
34
34
// MonthOfYear gets month of year like 12.
35
35
// 获取本年的第几月
36
36
func (c Carbon ) MonthOfYear () int {
37
- if c .IsInvalid () {
37
+ if c .Error != nil {
38
38
return 0
39
39
}
40
40
return int (c .StdTime ().Month ())
@@ -43,7 +43,7 @@ func (c Carbon) MonthOfYear() int {
43
43
// DayOfYear gets day of year like 365.
44
44
// 获取本年的第几天
45
45
func (c Carbon ) DayOfYear () int {
46
- if c .IsInvalid () {
46
+ if c .Error != nil {
47
47
return 0
48
48
}
49
49
return c .StdTime ().YearDay ()
@@ -52,7 +52,7 @@ func (c Carbon) DayOfYear() int {
52
52
// DayOfMonth gets day of month like 30.
53
53
// 获取本月的第几天
54
54
func (c Carbon ) DayOfMonth () int {
55
- if c .IsInvalid () {
55
+ if c .Error != nil {
56
56
return 0
57
57
}
58
58
return c .StdTime ().Day ()
@@ -61,7 +61,7 @@ func (c Carbon) DayOfMonth() int {
61
61
// DayOfWeek gets day of week like 6.
62
62
// 获取本周的第几天
63
63
func (c Carbon ) DayOfWeek () int {
64
- if c .IsInvalid () {
64
+ if c .Error != nil {
65
65
return 0
66
66
}
67
67
day := int (c .StdTime ().Weekday ())
@@ -74,7 +74,7 @@ func (c Carbon) DayOfWeek() int {
74
74
// WeekOfYear gets week of year like 1, see https://en.wikipedia.org/wiki/ISO_8601#Week_dates.
75
75
// 获取本年的第几周
76
76
func (c Carbon ) WeekOfYear () int {
77
- if c .IsInvalid () {
77
+ if c .Error != nil {
78
78
return 0
79
79
}
80
80
_ , week := c .StdTime ().ISOWeek ()
@@ -84,7 +84,7 @@ func (c Carbon) WeekOfYear() int {
84
84
// WeekOfMonth gets week of month like 1.
85
85
// 获取本月的第几周
86
86
func (c Carbon ) WeekOfMonth () int {
87
- if c .IsInvalid () {
87
+ if c .Error != nil {
88
88
return 0
89
89
}
90
90
days := c .Day () + c .StartOfMonth ().DayOfWeek () - 1
@@ -97,7 +97,7 @@ func (c Carbon) WeekOfMonth() int {
97
97
// DateTime gets current year, month, day, hour, minute, and second like 2020, 8, 5, 13, 14, 15.
98
98
// 获取当前年、月、日、时、分、秒
99
99
func (c Carbon ) DateTime () (year , month , day , hour , minute , second int ) {
100
- if c .IsInvalid () {
100
+ if c .Error != nil {
101
101
return
102
102
}
103
103
year , month , day = c .Date ()
@@ -108,7 +108,7 @@ func (c Carbon) DateTime() (year, month, day, hour, minute, second int) {
108
108
// DateTimeMilli gets current year, month, day, hour, minute, second and millisecond like 2020, 8, 5, 13, 14, 15, 999.
109
109
// 获取当前年、月、日、时、分、秒、毫秒
110
110
func (c Carbon ) DateTimeMilli () (year , month , day , hour , minute , second , millisecond int ) {
111
- if c .IsInvalid () {
111
+ if c .Error != nil {
112
112
return
113
113
}
114
114
year , month , day , hour , minute , second = c .DateTime ()
@@ -118,7 +118,7 @@ func (c Carbon) DateTimeMilli() (year, month, day, hour, minute, second, millise
118
118
// DateTimeMicro gets current year, month, day, hour, minute, second and microsecond like 2020, 8, 5, 13, 14, 15, 999999.
119
119
// 获取当前年、月、日、时、分、秒、微秒
120
120
func (c Carbon ) DateTimeMicro () (year , month , day , hour , minute , second , microsecond int ) {
121
- if c .IsInvalid () {
121
+ if c .Error != nil {
122
122
return
123
123
}
124
124
year , month , day , hour , minute , second = c .DateTime ()
@@ -128,7 +128,7 @@ func (c Carbon) DateTimeMicro() (year, month, day, hour, minute, second, microse
128
128
// DateTimeNano gets current year, month, day, hour, minute, second and nanosecond like 2020, 8, 5, 13, 14, 15, 999999999.
129
129
// 获取当前年、月、日、时、分、秒、纳秒
130
130
func (c Carbon ) DateTimeNano () (year , month , day , hour , minute , second , nanosecond int ) {
131
- if c .IsInvalid () {
131
+ if c .Error != nil {
132
132
return
133
133
}
134
134
year , month , day , hour , minute , second = c .DateTime ()
@@ -138,7 +138,7 @@ func (c Carbon) DateTimeNano() (year, month, day, hour, minute, second, nanoseco
138
138
// Date gets current year, month, and day like 2020, 8, 5.
139
139
// 获取当前年、月、日
140
140
func (c Carbon ) Date () (year , month , day int ) {
141
- if c .IsInvalid () {
141
+ if c .Error != nil {
142
142
return
143
143
}
144
144
var tm time.Month
@@ -149,7 +149,7 @@ func (c Carbon) Date() (year, month, day int) {
149
149
// DateMilli gets current year, month, day and millisecond like 2020, 8, 5, 999.
150
150
// 获取当前年、月、日、毫秒
151
151
func (c Carbon ) DateMilli () (year , month , day , millisecond int ) {
152
- if c .IsInvalid () {
152
+ if c .Error != nil {
153
153
return
154
154
}
155
155
year , month , day = c .Date ()
@@ -159,7 +159,7 @@ func (c Carbon) DateMilli() (year, month, day, millisecond int) {
159
159
// DateMicro gets current year, month, day and microsecond like 2020, 8, 5, 999999.
160
160
// 获取当前年、月、日、微秒
161
161
func (c Carbon ) DateMicro () (year , month , day , microsecond int ) {
162
- if c .IsInvalid () {
162
+ if c .Error != nil {
163
163
return
164
164
}
165
165
year , month , day = c .Date ()
@@ -169,7 +169,7 @@ func (c Carbon) DateMicro() (year, month, day, microsecond int) {
169
169
// DateNano gets current year, month, day and nanosecond like 2020, 8, 5, 999999999.
170
170
// 获取当前年、月、日、纳秒
171
171
func (c Carbon ) DateNano () (year , month , day , nanosecond int ) {
172
- if c .IsInvalid () {
172
+ if c .Error != nil {
173
173
return
174
174
}
175
175
year , month , day = c .Date ()
@@ -179,7 +179,7 @@ func (c Carbon) DateNano() (year, month, day, nanosecond int) {
179
179
// Time gets current hour, minute, and second like 13, 14, 15.
180
180
// 获取当前时、分、秒
181
181
func (c Carbon ) Time () (hour , minute , second int ) {
182
- if c .IsInvalid () {
182
+ if c .Error != nil {
183
183
return
184
184
}
185
185
return c .StdTime ().Clock ()
@@ -188,7 +188,7 @@ func (c Carbon) Time() (hour, minute, second int) {
188
188
// TimeMilli gets current hour, minute, second and millisecond like 13, 14, 15, 999.
189
189
// 获取当前时、分、秒、毫秒
190
190
func (c Carbon ) TimeMilli () (hour , minute , second , millisecond int ) {
191
- if c .IsInvalid () {
191
+ if c .Error != nil {
192
192
return
193
193
}
194
194
hour , minute , second = c .Time ()
@@ -198,7 +198,7 @@ func (c Carbon) TimeMilli() (hour, minute, second, millisecond int) {
198
198
// TimeMicro gets current hour, minute, second and microsecond like 13, 14, 15, 999999.
199
199
// 获取当前时、分、秒、微秒
200
200
func (c Carbon ) TimeMicro () (hour , minute , second , microsecond int ) {
201
- if c .IsInvalid () {
201
+ if c .Error != nil {
202
202
return
203
203
}
204
204
hour , minute , second = c .Time ()
@@ -208,7 +208,7 @@ func (c Carbon) TimeMicro() (hour, minute, second, microsecond int) {
208
208
// TimeNano gets current hour, minute, second and nanosecond like 13, 14, 15, 999999999.
209
209
// 获取当前时、分、秒、纳秒
210
210
func (c Carbon ) TimeNano () (hour , minute , second , nanosecond int ) {
211
- if c .IsInvalid () {
211
+ if c .Error != nil {
212
212
return
213
213
}
214
214
hour , minute , second = c .Time ()
@@ -218,7 +218,7 @@ func (c Carbon) TimeNano() (hour, minute, second, nanosecond int) {
218
218
// Century gets current century like 21.
219
219
// 获取当前世纪
220
220
func (c Carbon ) Century () int {
221
- if c .IsInvalid () {
221
+ if c .Error != nil {
222
222
return 0
223
223
}
224
224
return c .Year ()/ YearsPerCentury + 1
@@ -227,7 +227,7 @@ func (c Carbon) Century() int {
227
227
// Decade gets current decade like 20.
228
228
// 获取当前年代
229
229
func (c Carbon ) Decade () int {
230
- if c .IsInvalid () {
230
+ if c .Error != nil {
231
231
return 0
232
232
}
233
233
return c .Year () % YearsPerCentury / YearsPerDecade * YearsPerDecade
@@ -236,7 +236,7 @@ func (c Carbon) Decade() int {
236
236
// Year gets current year like 2020.
237
237
// 获取当前年
238
238
func (c Carbon ) Year () int {
239
- if c .IsInvalid () {
239
+ if c .Error != nil {
240
240
return 0
241
241
}
242
242
return c .StdTime ().Year ()
@@ -245,7 +245,7 @@ func (c Carbon) Year() int {
245
245
// Quarter gets current quarter like 3.
246
246
// 获取当前季度
247
247
func (c Carbon ) Quarter () (quarter int ) {
248
- if c .IsInvalid () {
248
+ if c .Error != nil {
249
249
return
250
250
}
251
251
month := c .Month ()
@@ -271,7 +271,7 @@ func (c Carbon) Month() int {
271
271
// Week gets current week like 6, start from 0.
272
272
// 获取当前周(从0开始)
273
273
func (c Carbon ) Week () int {
274
- if c .IsInvalid () {
274
+ if c .Error != nil {
275
275
return - 1
276
276
}
277
277
return (c .DayOfWeek () + DaysPerWeek - int (c .weekStartsAt )) % DaysPerWeek
@@ -286,7 +286,7 @@ func (c Carbon) Day() int {
286
286
// Hour gets current hour like 13.
287
287
// 获取当前小时
288
288
func (c Carbon ) Hour () int {
289
- if c .IsInvalid () {
289
+ if c .Error != nil {
290
290
return 0
291
291
}
292
292
return c .StdTime ().Hour ()
@@ -295,7 +295,7 @@ func (c Carbon) Hour() int {
295
295
// Minute gets current minute like 14.
296
296
// 获取当前分钟数
297
297
func (c Carbon ) Minute () int {
298
- if c .IsInvalid () {
298
+ if c .Error != nil {
299
299
return 0
300
300
}
301
301
return c .StdTime ().Minute ()
@@ -304,7 +304,7 @@ func (c Carbon) Minute() int {
304
304
// Second gets current second like 15.
305
305
// 获取当前秒数
306
306
func (c Carbon ) Second () int {
307
- if c .IsInvalid () {
307
+ if c .Error != nil {
308
308
return 0
309
309
}
310
310
return c .StdTime ().Second ()
@@ -313,7 +313,7 @@ func (c Carbon) Second() int {
313
313
// Millisecond gets current millisecond like 999.
314
314
// 获取当前毫秒数
315
315
func (c Carbon ) Millisecond () int {
316
- if c .IsInvalid () {
316
+ if c .Error != nil {
317
317
return 0
318
318
}
319
319
return c .StdTime ().Nanosecond () / 1e6
@@ -322,7 +322,7 @@ func (c Carbon) Millisecond() int {
322
322
// Microsecond gets current microsecond like 999999.
323
323
// 获取当前微秒数
324
324
func (c Carbon ) Microsecond () int {
325
- if c .IsInvalid () {
325
+ if c .Error != nil {
326
326
return 0
327
327
}
328
328
return c .StdTime ().Nanosecond () / 1e3
@@ -331,7 +331,7 @@ func (c Carbon) Microsecond() int {
331
331
// Nanosecond gets current nanosecond like 999999999.
332
332
// 获取当前纳秒数
333
333
func (c Carbon ) Nanosecond () int {
334
- if c .IsInvalid () {
334
+ if c .Error != nil {
335
335
return 0
336
336
}
337
337
return c .StdTime ().Nanosecond ()
@@ -340,7 +340,7 @@ func (c Carbon) Nanosecond() int {
340
340
// Timestamp gets timestamp with second like 1596604455.
341
341
// 输出秒级时间戳
342
342
func (c Carbon ) Timestamp () int64 {
343
- if c .IsInvalid () {
343
+ if c .Error != nil {
344
344
return 0
345
345
}
346
346
return c .StdTime ().Unix ()
@@ -349,7 +349,7 @@ func (c Carbon) Timestamp() int64 {
349
349
// TimestampMilli gets timestamp with millisecond like 1596604455000.
350
350
// 获取毫秒级时间戳
351
351
func (c Carbon ) TimestampMilli () int64 {
352
- if c .IsInvalid () {
352
+ if c .Error != nil {
353
353
return 0
354
354
}
355
355
t := c .StdTime ()
@@ -359,7 +359,7 @@ func (c Carbon) TimestampMilli() int64 {
359
359
// TimestampMicro gets timestamp with microsecond like 1596604455000000.
360
360
// 获取微秒级时间戳
361
361
func (c Carbon ) TimestampMicro () int64 {
362
- if c .IsInvalid () {
362
+ if c .Error != nil {
363
363
return 0
364
364
}
365
365
t := c .StdTime ()
@@ -369,7 +369,7 @@ func (c Carbon) TimestampMicro() int64 {
369
369
// TimestampNano gets timestamp with nanosecond like 1596604455000000000.
370
370
// 获取纳秒级时间戳
371
371
func (c Carbon ) TimestampNano () int64 {
372
- if c .IsInvalid () {
372
+ if c .Error != nil {
373
373
return 0
374
374
}
375
375
return c .StdTime ().UnixNano ()
@@ -378,33 +378,45 @@ func (c Carbon) TimestampNano() int64 {
378
378
// Location gets location name like "PRC".
379
379
// 获取位置
380
380
func (c Carbon ) Location () string {
381
+ if c .Error != nil {
382
+ return ""
383
+ }
381
384
return c .loc .String ()
382
385
}
383
386
384
387
// Timezone gets timezone name like "CST".
385
388
// 获取时区
386
389
func (c Carbon ) Timezone () string {
390
+ if c .Error != nil {
391
+ return ""
392
+ }
387
393
name , _ := c .StdTime ().Zone ()
388
394
return name
389
395
}
390
396
391
397
// Offset gets offset seconds from the UTC timezone like 28800.
392
398
// 获取距离UTC时区的偏移量,单位秒
393
399
func (c Carbon ) Offset () int {
400
+ if c .Error != nil {
401
+ return 0
402
+ }
394
403
_ , offset := c .StdTime ().Zone ()
395
404
return offset
396
405
}
397
406
398
407
// Locale gets locale name like "zh-CN".
399
408
// 获取语言区域
400
409
func (c Carbon ) Locale () string {
410
+ if c .Error != nil {
411
+ return ""
412
+ }
401
413
return c .lang .locale
402
414
}
403
415
404
416
// Age gets age like 18.
405
417
// 获取年龄
406
418
func (c Carbon ) Age () int {
407
- if c .IsInvalid () {
419
+ if c .Error != nil {
408
420
return 0
409
421
}
410
422
now := c .Now ()
0 commit comments