Skip to content

Commit b1a79ff

Browse files
committed
c.IsInvalid() -> c.Error != nil
1 parent 4db3960 commit b1a79ff

File tree

1 file changed

+48
-36
lines changed

1 file changed

+48
-36
lines changed

getter.go

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func (c Carbon) StdTime() time.Time {
1313
// DaysInYear gets total days in year like 365.
1414
// 获取本年的总天数
1515
func (c Carbon) DaysInYear() int {
16-
if c.IsInvalid() {
16+
if c.Error != nil {
1717
return 0
1818
}
1919
if c.IsLeapYear() {
@@ -25,7 +25,7 @@ func (c Carbon) DaysInYear() int {
2525
// DaysInMonth gets total days in month like 30.
2626
// 获取本月的总天数
2727
func (c Carbon) DaysInMonth() int {
28-
if c.IsInvalid() {
28+
if c.Error != nil {
2929
return 0
3030
}
3131
return c.EndOfMonth().time.In(c.loc).Day()
@@ -34,7 +34,7 @@ func (c Carbon) DaysInMonth() int {
3434
// MonthOfYear gets month of year like 12.
3535
// 获取本年的第几月
3636
func (c Carbon) MonthOfYear() int {
37-
if c.IsInvalid() {
37+
if c.Error != nil {
3838
return 0
3939
}
4040
return int(c.StdTime().Month())
@@ -43,7 +43,7 @@ func (c Carbon) MonthOfYear() int {
4343
// DayOfYear gets day of year like 365.
4444
// 获取本年的第几天
4545
func (c Carbon) DayOfYear() int {
46-
if c.IsInvalid() {
46+
if c.Error != nil {
4747
return 0
4848
}
4949
return c.StdTime().YearDay()
@@ -52,7 +52,7 @@ func (c Carbon) DayOfYear() int {
5252
// DayOfMonth gets day of month like 30.
5353
// 获取本月的第几天
5454
func (c Carbon) DayOfMonth() int {
55-
if c.IsInvalid() {
55+
if c.Error != nil {
5656
return 0
5757
}
5858
return c.StdTime().Day()
@@ -61,7 +61,7 @@ func (c Carbon) DayOfMonth() int {
6161
// DayOfWeek gets day of week like 6.
6262
// 获取本周的第几天
6363
func (c Carbon) DayOfWeek() int {
64-
if c.IsInvalid() {
64+
if c.Error != nil {
6565
return 0
6666
}
6767
day := int(c.StdTime().Weekday())
@@ -74,7 +74,7 @@ func (c Carbon) DayOfWeek() int {
7474
// WeekOfYear gets week of year like 1, see https://en.wikipedia.org/wiki/ISO_8601#Week_dates.
7575
// 获取本年的第几周
7676
func (c Carbon) WeekOfYear() int {
77-
if c.IsInvalid() {
77+
if c.Error != nil {
7878
return 0
7979
}
8080
_, week := c.StdTime().ISOWeek()
@@ -84,7 +84,7 @@ func (c Carbon) WeekOfYear() int {
8484
// WeekOfMonth gets week of month like 1.
8585
// 获取本月的第几周
8686
func (c Carbon) WeekOfMonth() int {
87-
if c.IsInvalid() {
87+
if c.Error != nil {
8888
return 0
8989
}
9090
days := c.Day() + c.StartOfMonth().DayOfWeek() - 1
@@ -97,7 +97,7 @@ func (c Carbon) WeekOfMonth() int {
9797
// DateTime gets current year, month, day, hour, minute, and second like 2020, 8, 5, 13, 14, 15.
9898
// 获取当前年、月、日、时、分、秒
9999
func (c Carbon) DateTime() (year, month, day, hour, minute, second int) {
100-
if c.IsInvalid() {
100+
if c.Error != nil {
101101
return
102102
}
103103
year, month, day = c.Date()
@@ -108,7 +108,7 @@ func (c Carbon) DateTime() (year, month, day, hour, minute, second int) {
108108
// DateTimeMilli gets current year, month, day, hour, minute, second and millisecond like 2020, 8, 5, 13, 14, 15, 999.
109109
// 获取当前年、月、日、时、分、秒、毫秒
110110
func (c Carbon) DateTimeMilli() (year, month, day, hour, minute, second, millisecond int) {
111-
if c.IsInvalid() {
111+
if c.Error != nil {
112112
return
113113
}
114114
year, month, day, hour, minute, second = c.DateTime()
@@ -118,7 +118,7 @@ func (c Carbon) DateTimeMilli() (year, month, day, hour, minute, second, millise
118118
// DateTimeMicro gets current year, month, day, hour, minute, second and microsecond like 2020, 8, 5, 13, 14, 15, 999999.
119119
// 获取当前年、月、日、时、分、秒、微秒
120120
func (c Carbon) DateTimeMicro() (year, month, day, hour, minute, second, microsecond int) {
121-
if c.IsInvalid() {
121+
if c.Error != nil {
122122
return
123123
}
124124
year, month, day, hour, minute, second = c.DateTime()
@@ -128,7 +128,7 @@ func (c Carbon) DateTimeMicro() (year, month, day, hour, minute, second, microse
128128
// DateTimeNano gets current year, month, day, hour, minute, second and nanosecond like 2020, 8, 5, 13, 14, 15, 999999999.
129129
// 获取当前年、月、日、时、分、秒、纳秒
130130
func (c Carbon) DateTimeNano() (year, month, day, hour, minute, second, nanosecond int) {
131-
if c.IsInvalid() {
131+
if c.Error != nil {
132132
return
133133
}
134134
year, month, day, hour, minute, second = c.DateTime()
@@ -138,7 +138,7 @@ func (c Carbon) DateTimeNano() (year, month, day, hour, minute, second, nanoseco
138138
// Date gets current year, month, and day like 2020, 8, 5.
139139
// 获取当前年、月、日
140140
func (c Carbon) Date() (year, month, day int) {
141-
if c.IsInvalid() {
141+
if c.Error != nil {
142142
return
143143
}
144144
var tm time.Month
@@ -149,7 +149,7 @@ func (c Carbon) Date() (year, month, day int) {
149149
// DateMilli gets current year, month, day and millisecond like 2020, 8, 5, 999.
150150
// 获取当前年、月、日、毫秒
151151
func (c Carbon) DateMilli() (year, month, day, millisecond int) {
152-
if c.IsInvalid() {
152+
if c.Error != nil {
153153
return
154154
}
155155
year, month, day = c.Date()
@@ -159,7 +159,7 @@ func (c Carbon) DateMilli() (year, month, day, millisecond int) {
159159
// DateMicro gets current year, month, day and microsecond like 2020, 8, 5, 999999.
160160
// 获取当前年、月、日、微秒
161161
func (c Carbon) DateMicro() (year, month, day, microsecond int) {
162-
if c.IsInvalid() {
162+
if c.Error != nil {
163163
return
164164
}
165165
year, month, day = c.Date()
@@ -169,7 +169,7 @@ func (c Carbon) DateMicro() (year, month, day, microsecond int) {
169169
// DateNano gets current year, month, day and nanosecond like 2020, 8, 5, 999999999.
170170
// 获取当前年、月、日、纳秒
171171
func (c Carbon) DateNano() (year, month, day, nanosecond int) {
172-
if c.IsInvalid() {
172+
if c.Error != nil {
173173
return
174174
}
175175
year, month, day = c.Date()
@@ -179,7 +179,7 @@ func (c Carbon) DateNano() (year, month, day, nanosecond int) {
179179
// Time gets current hour, minute, and second like 13, 14, 15.
180180
// 获取当前时、分、秒
181181
func (c Carbon) Time() (hour, minute, second int) {
182-
if c.IsInvalid() {
182+
if c.Error != nil {
183183
return
184184
}
185185
return c.StdTime().Clock()
@@ -188,7 +188,7 @@ func (c Carbon) Time() (hour, minute, second int) {
188188
// TimeMilli gets current hour, minute, second and millisecond like 13, 14, 15, 999.
189189
// 获取当前时、分、秒、毫秒
190190
func (c Carbon) TimeMilli() (hour, minute, second, millisecond int) {
191-
if c.IsInvalid() {
191+
if c.Error != nil {
192192
return
193193
}
194194
hour, minute, second = c.Time()
@@ -198,7 +198,7 @@ func (c Carbon) TimeMilli() (hour, minute, second, millisecond int) {
198198
// TimeMicro gets current hour, minute, second and microsecond like 13, 14, 15, 999999.
199199
// 获取当前时、分、秒、微秒
200200
func (c Carbon) TimeMicro() (hour, minute, second, microsecond int) {
201-
if c.IsInvalid() {
201+
if c.Error != nil {
202202
return
203203
}
204204
hour, minute, second = c.Time()
@@ -208,7 +208,7 @@ func (c Carbon) TimeMicro() (hour, minute, second, microsecond int) {
208208
// TimeNano gets current hour, minute, second and nanosecond like 13, 14, 15, 999999999.
209209
// 获取当前时、分、秒、纳秒
210210
func (c Carbon) TimeNano() (hour, minute, second, nanosecond int) {
211-
if c.IsInvalid() {
211+
if c.Error != nil {
212212
return
213213
}
214214
hour, minute, second = c.Time()
@@ -218,7 +218,7 @@ func (c Carbon) TimeNano() (hour, minute, second, nanosecond int) {
218218
// Century gets current century like 21.
219219
// 获取当前世纪
220220
func (c Carbon) Century() int {
221-
if c.IsInvalid() {
221+
if c.Error != nil {
222222
return 0
223223
}
224224
return c.Year()/YearsPerCentury + 1
@@ -227,7 +227,7 @@ func (c Carbon) Century() int {
227227
// Decade gets current decade like 20.
228228
// 获取当前年代
229229
func (c Carbon) Decade() int {
230-
if c.IsInvalid() {
230+
if c.Error != nil {
231231
return 0
232232
}
233233
return c.Year() % YearsPerCentury / YearsPerDecade * YearsPerDecade
@@ -236,7 +236,7 @@ func (c Carbon) Decade() int {
236236
// Year gets current year like 2020.
237237
// 获取当前年
238238
func (c Carbon) Year() int {
239-
if c.IsInvalid() {
239+
if c.Error != nil {
240240
return 0
241241
}
242242
return c.StdTime().Year()
@@ -245,7 +245,7 @@ func (c Carbon) Year() int {
245245
// Quarter gets current quarter like 3.
246246
// 获取当前季度
247247
func (c Carbon) Quarter() (quarter int) {
248-
if c.IsInvalid() {
248+
if c.Error != nil {
249249
return
250250
}
251251
month := c.Month()
@@ -271,7 +271,7 @@ func (c Carbon) Month() int {
271271
// Week gets current week like 6, start from 0.
272272
// 获取当前周(从0开始)
273273
func (c Carbon) Week() int {
274-
if c.IsInvalid() {
274+
if c.Error != nil {
275275
return -1
276276
}
277277
return (c.DayOfWeek() + DaysPerWeek - int(c.weekStartsAt)) % DaysPerWeek
@@ -286,7 +286,7 @@ func (c Carbon) Day() int {
286286
// Hour gets current hour like 13.
287287
// 获取当前小时
288288
func (c Carbon) Hour() int {
289-
if c.IsInvalid() {
289+
if c.Error != nil {
290290
return 0
291291
}
292292
return c.StdTime().Hour()
@@ -295,7 +295,7 @@ func (c Carbon) Hour() int {
295295
// Minute gets current minute like 14.
296296
// 获取当前分钟数
297297
func (c Carbon) Minute() int {
298-
if c.IsInvalid() {
298+
if c.Error != nil {
299299
return 0
300300
}
301301
return c.StdTime().Minute()
@@ -304,7 +304,7 @@ func (c Carbon) Minute() int {
304304
// Second gets current second like 15.
305305
// 获取当前秒数
306306
func (c Carbon) Second() int {
307-
if c.IsInvalid() {
307+
if c.Error != nil {
308308
return 0
309309
}
310310
return c.StdTime().Second()
@@ -313,7 +313,7 @@ func (c Carbon) Second() int {
313313
// Millisecond gets current millisecond like 999.
314314
// 获取当前毫秒数
315315
func (c Carbon) Millisecond() int {
316-
if c.IsInvalid() {
316+
if c.Error != nil {
317317
return 0
318318
}
319319
return c.StdTime().Nanosecond() / 1e6
@@ -322,7 +322,7 @@ func (c Carbon) Millisecond() int {
322322
// Microsecond gets current microsecond like 999999.
323323
// 获取当前微秒数
324324
func (c Carbon) Microsecond() int {
325-
if c.IsInvalid() {
325+
if c.Error != nil {
326326
return 0
327327
}
328328
return c.StdTime().Nanosecond() / 1e3
@@ -331,7 +331,7 @@ func (c Carbon) Microsecond() int {
331331
// Nanosecond gets current nanosecond like 999999999.
332332
// 获取当前纳秒数
333333
func (c Carbon) Nanosecond() int {
334-
if c.IsInvalid() {
334+
if c.Error != nil {
335335
return 0
336336
}
337337
return c.StdTime().Nanosecond()
@@ -340,7 +340,7 @@ func (c Carbon) Nanosecond() int {
340340
// Timestamp gets timestamp with second like 1596604455.
341341
// 输出秒级时间戳
342342
func (c Carbon) Timestamp() int64 {
343-
if c.IsInvalid() {
343+
if c.Error != nil {
344344
return 0
345345
}
346346
return c.StdTime().Unix()
@@ -349,7 +349,7 @@ func (c Carbon) Timestamp() int64 {
349349
// TimestampMilli gets timestamp with millisecond like 1596604455000.
350350
// 获取毫秒级时间戳
351351
func (c Carbon) TimestampMilli() int64 {
352-
if c.IsInvalid() {
352+
if c.Error != nil {
353353
return 0
354354
}
355355
t := c.StdTime()
@@ -359,7 +359,7 @@ func (c Carbon) TimestampMilli() int64 {
359359
// TimestampMicro gets timestamp with microsecond like 1596604455000000.
360360
// 获取微秒级时间戳
361361
func (c Carbon) TimestampMicro() int64 {
362-
if c.IsInvalid() {
362+
if c.Error != nil {
363363
return 0
364364
}
365365
t := c.StdTime()
@@ -369,7 +369,7 @@ func (c Carbon) TimestampMicro() int64 {
369369
// TimestampNano gets timestamp with nanosecond like 1596604455000000000.
370370
// 获取纳秒级时间戳
371371
func (c Carbon) TimestampNano() int64 {
372-
if c.IsInvalid() {
372+
if c.Error != nil {
373373
return 0
374374
}
375375
return c.StdTime().UnixNano()
@@ -378,33 +378,45 @@ func (c Carbon) TimestampNano() int64 {
378378
// Location gets location name like "PRC".
379379
// 获取位置
380380
func (c Carbon) Location() string {
381+
if c.Error != nil {
382+
return ""
383+
}
381384
return c.loc.String()
382385
}
383386

384387
// Timezone gets timezone name like "CST".
385388
// 获取时区
386389
func (c Carbon) Timezone() string {
390+
if c.Error != nil {
391+
return ""
392+
}
387393
name, _ := c.StdTime().Zone()
388394
return name
389395
}
390396

391397
// Offset gets offset seconds from the UTC timezone like 28800.
392398
// 获取距离UTC时区的偏移量,单位秒
393399
func (c Carbon) Offset() int {
400+
if c.Error != nil {
401+
return 0
402+
}
394403
_, offset := c.StdTime().Zone()
395404
return offset
396405
}
397406

398407
// Locale gets locale name like "zh-CN".
399408
// 获取语言区域
400409
func (c Carbon) Locale() string {
410+
if c.Error != nil {
411+
return ""
412+
}
401413
return c.lang.locale
402414
}
403415

404416
// Age gets age like 18.
405417
// 获取年龄
406418
func (c Carbon) Age() int {
407-
if c.IsInvalid() {
419+
if c.Error != nil {
408420
return 0
409421
}
410422
now := c.Now()

0 commit comments

Comments
 (0)