Skip to content

Commit

Permalink
c.IsInvalid() -> c.Error != nil
Browse files Browse the repository at this point in the history
  • Loading branch information
gouguoyin committed Apr 9, 2024
1 parent 4db3960 commit b1a79ff
Showing 1 changed file with 48 additions and 36 deletions.
84 changes: 48 additions & 36 deletions getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (c Carbon) StdTime() time.Time {
// DaysInYear gets total days in year like 365.
// 获取本年的总天数
func (c Carbon) DaysInYear() int {
if c.IsInvalid() {
if c.Error != nil {
return 0
}
if c.IsLeapYear() {
Expand All @@ -25,7 +25,7 @@ func (c Carbon) DaysInYear() int {
// DaysInMonth gets total days in month like 30.
// 获取本月的总天数
func (c Carbon) DaysInMonth() int {
if c.IsInvalid() {
if c.Error != nil {
return 0
}
return c.EndOfMonth().time.In(c.loc).Day()
Expand All @@ -34,7 +34,7 @@ func (c Carbon) DaysInMonth() int {
// MonthOfYear gets month of year like 12.
// 获取本年的第几月
func (c Carbon) MonthOfYear() int {
if c.IsInvalid() {
if c.Error != nil {
return 0
}
return int(c.StdTime().Month())
Expand All @@ -43,7 +43,7 @@ func (c Carbon) MonthOfYear() int {
// DayOfYear gets day of year like 365.
// 获取本年的第几天
func (c Carbon) DayOfYear() int {
if c.IsInvalid() {
if c.Error != nil {
return 0
}
return c.StdTime().YearDay()
Expand All @@ -52,7 +52,7 @@ func (c Carbon) DayOfYear() int {
// DayOfMonth gets day of month like 30.
// 获取本月的第几天
func (c Carbon) DayOfMonth() int {
if c.IsInvalid() {
if c.Error != nil {
return 0
}
return c.StdTime().Day()
Expand All @@ -61,7 +61,7 @@ func (c Carbon) DayOfMonth() int {
// DayOfWeek gets day of week like 6.
// 获取本周的第几天
func (c Carbon) DayOfWeek() int {
if c.IsInvalid() {
if c.Error != nil {
return 0
}
day := int(c.StdTime().Weekday())
Expand All @@ -74,7 +74,7 @@ func (c Carbon) DayOfWeek() int {
// WeekOfYear gets week of year like 1, see https://en.wikipedia.org/wiki/ISO_8601#Week_dates.
// 获取本年的第几周
func (c Carbon) WeekOfYear() int {
if c.IsInvalid() {
if c.Error != nil {
return 0
}
_, week := c.StdTime().ISOWeek()
Expand All @@ -84,7 +84,7 @@ func (c Carbon) WeekOfYear() int {
// WeekOfMonth gets week of month like 1.
// 获取本月的第几周
func (c Carbon) WeekOfMonth() int {
if c.IsInvalid() {
if c.Error != nil {
return 0
}
days := c.Day() + c.StartOfMonth().DayOfWeek() - 1
Expand All @@ -97,7 +97,7 @@ func (c Carbon) WeekOfMonth() int {
// DateTime gets current year, month, day, hour, minute, and second like 2020, 8, 5, 13, 14, 15.
// 获取当前年、月、日、时、分、秒
func (c Carbon) DateTime() (year, month, day, hour, minute, second int) {
if c.IsInvalid() {
if c.Error != nil {
return
}
year, month, day = c.Date()
Expand All @@ -108,7 +108,7 @@ func (c Carbon) DateTime() (year, month, day, hour, minute, second int) {
// DateTimeMilli gets current year, month, day, hour, minute, second and millisecond like 2020, 8, 5, 13, 14, 15, 999.
// 获取当前年、月、日、时、分、秒、毫秒
func (c Carbon) DateTimeMilli() (year, month, day, hour, minute, second, millisecond int) {
if c.IsInvalid() {
if c.Error != nil {
return
}
year, month, day, hour, minute, second = c.DateTime()
Expand All @@ -118,7 +118,7 @@ func (c Carbon) DateTimeMilli() (year, month, day, hour, minute, second, millise
// DateTimeMicro gets current year, month, day, hour, minute, second and microsecond like 2020, 8, 5, 13, 14, 15, 999999.
// 获取当前年、月、日、时、分、秒、微秒
func (c Carbon) DateTimeMicro() (year, month, day, hour, minute, second, microsecond int) {
if c.IsInvalid() {
if c.Error != nil {
return
}
year, month, day, hour, minute, second = c.DateTime()
Expand All @@ -128,7 +128,7 @@ func (c Carbon) DateTimeMicro() (year, month, day, hour, minute, second, microse
// DateTimeNano gets current year, month, day, hour, minute, second and nanosecond like 2020, 8, 5, 13, 14, 15, 999999999.
// 获取当前年、月、日、时、分、秒、纳秒
func (c Carbon) DateTimeNano() (year, month, day, hour, minute, second, nanosecond int) {
if c.IsInvalid() {
if c.Error != nil {
return
}
year, month, day, hour, minute, second = c.DateTime()
Expand All @@ -138,7 +138,7 @@ func (c Carbon) DateTimeNano() (year, month, day, hour, minute, second, nanoseco
// Date gets current year, month, and day like 2020, 8, 5.
// 获取当前年、月、日
func (c Carbon) Date() (year, month, day int) {
if c.IsInvalid() {
if c.Error != nil {
return
}
var tm time.Month
Expand All @@ -149,7 +149,7 @@ func (c Carbon) Date() (year, month, day int) {
// DateMilli gets current year, month, day and millisecond like 2020, 8, 5, 999.
// 获取当前年、月、日、毫秒
func (c Carbon) DateMilli() (year, month, day, millisecond int) {
if c.IsInvalid() {
if c.Error != nil {
return
}
year, month, day = c.Date()
Expand All @@ -159,7 +159,7 @@ func (c Carbon) DateMilli() (year, month, day, millisecond int) {
// DateMicro gets current year, month, day and microsecond like 2020, 8, 5, 999999.
// 获取当前年、月、日、微秒
func (c Carbon) DateMicro() (year, month, day, microsecond int) {
if c.IsInvalid() {
if c.Error != nil {
return
}
year, month, day = c.Date()
Expand All @@ -169,7 +169,7 @@ func (c Carbon) DateMicro() (year, month, day, microsecond int) {
// DateNano gets current year, month, day and nanosecond like 2020, 8, 5, 999999999.
// 获取当前年、月、日、纳秒
func (c Carbon) DateNano() (year, month, day, nanosecond int) {
if c.IsInvalid() {
if c.Error != nil {
return
}
year, month, day = c.Date()
Expand All @@ -179,7 +179,7 @@ func (c Carbon) DateNano() (year, month, day, nanosecond int) {
// Time gets current hour, minute, and second like 13, 14, 15.
// 获取当前时、分、秒
func (c Carbon) Time() (hour, minute, second int) {
if c.IsInvalid() {
if c.Error != nil {
return
}
return c.StdTime().Clock()
Expand All @@ -188,7 +188,7 @@ func (c Carbon) Time() (hour, minute, second int) {
// TimeMilli gets current hour, minute, second and millisecond like 13, 14, 15, 999.
// 获取当前时、分、秒、毫秒
func (c Carbon) TimeMilli() (hour, minute, second, millisecond int) {
if c.IsInvalid() {
if c.Error != nil {
return
}
hour, minute, second = c.Time()
Expand All @@ -198,7 +198,7 @@ func (c Carbon) TimeMilli() (hour, minute, second, millisecond int) {
// TimeMicro gets current hour, minute, second and microsecond like 13, 14, 15, 999999.
// 获取当前时、分、秒、微秒
func (c Carbon) TimeMicro() (hour, minute, second, microsecond int) {
if c.IsInvalid() {
if c.Error != nil {
return
}
hour, minute, second = c.Time()
Expand All @@ -208,7 +208,7 @@ func (c Carbon) TimeMicro() (hour, minute, second, microsecond int) {
// TimeNano gets current hour, minute, second and nanosecond like 13, 14, 15, 999999999.
// 获取当前时、分、秒、纳秒
func (c Carbon) TimeNano() (hour, minute, second, nanosecond int) {
if c.IsInvalid() {
if c.Error != nil {
return
}
hour, minute, second = c.Time()
Expand All @@ -218,7 +218,7 @@ func (c Carbon) TimeNano() (hour, minute, second, nanosecond int) {
// Century gets current century like 21.
// 获取当前世纪
func (c Carbon) Century() int {
if c.IsInvalid() {
if c.Error != nil {
return 0
}
return c.Year()/YearsPerCentury + 1
Expand All @@ -227,7 +227,7 @@ func (c Carbon) Century() int {
// Decade gets current decade like 20.
// 获取当前年代
func (c Carbon) Decade() int {
if c.IsInvalid() {
if c.Error != nil {
return 0
}
return c.Year() % YearsPerCentury / YearsPerDecade * YearsPerDecade
Expand All @@ -236,7 +236,7 @@ func (c Carbon) Decade() int {
// Year gets current year like 2020.
// 获取当前年
func (c Carbon) Year() int {
if c.IsInvalid() {
if c.Error != nil {
return 0
}
return c.StdTime().Year()
Expand All @@ -245,7 +245,7 @@ func (c Carbon) Year() int {
// Quarter gets current quarter like 3.
// 获取当前季度
func (c Carbon) Quarter() (quarter int) {
if c.IsInvalid() {
if c.Error != nil {
return
}
month := c.Month()
Expand All @@ -271,7 +271,7 @@ func (c Carbon) Month() int {
// Week gets current week like 6, start from 0.
// 获取当前周(从0开始)
func (c Carbon) Week() int {
if c.IsInvalid() {
if c.Error != nil {
return -1
}
return (c.DayOfWeek() + DaysPerWeek - int(c.weekStartsAt)) % DaysPerWeek
Expand All @@ -286,7 +286,7 @@ func (c Carbon) Day() int {
// Hour gets current hour like 13.
// 获取当前小时
func (c Carbon) Hour() int {
if c.IsInvalid() {
if c.Error != nil {
return 0
}
return c.StdTime().Hour()
Expand All @@ -295,7 +295,7 @@ func (c Carbon) Hour() int {
// Minute gets current minute like 14.
// 获取当前分钟数
func (c Carbon) Minute() int {
if c.IsInvalid() {
if c.Error != nil {
return 0
}
return c.StdTime().Minute()
Expand All @@ -304,7 +304,7 @@ func (c Carbon) Minute() int {
// Second gets current second like 15.
// 获取当前秒数
func (c Carbon) Second() int {
if c.IsInvalid() {
if c.Error != nil {
return 0
}
return c.StdTime().Second()
Expand All @@ -313,7 +313,7 @@ func (c Carbon) Second() int {
// Millisecond gets current millisecond like 999.
// 获取当前毫秒数
func (c Carbon) Millisecond() int {
if c.IsInvalid() {
if c.Error != nil {
return 0
}
return c.StdTime().Nanosecond() / 1e6
Expand All @@ -322,7 +322,7 @@ func (c Carbon) Millisecond() int {
// Microsecond gets current microsecond like 999999.
// 获取当前微秒数
func (c Carbon) Microsecond() int {
if c.IsInvalid() {
if c.Error != nil {
return 0
}
return c.StdTime().Nanosecond() / 1e3
Expand All @@ -331,7 +331,7 @@ func (c Carbon) Microsecond() int {
// Nanosecond gets current nanosecond like 999999999.
// 获取当前纳秒数
func (c Carbon) Nanosecond() int {
if c.IsInvalid() {
if c.Error != nil {
return 0
}
return c.StdTime().Nanosecond()
Expand All @@ -340,7 +340,7 @@ func (c Carbon) Nanosecond() int {
// Timestamp gets timestamp with second like 1596604455.
// 输出秒级时间戳
func (c Carbon) Timestamp() int64 {
if c.IsInvalid() {
if c.Error != nil {
return 0
}
return c.StdTime().Unix()
Expand All @@ -349,7 +349,7 @@ func (c Carbon) Timestamp() int64 {
// TimestampMilli gets timestamp with millisecond like 1596604455000.
// 获取毫秒级时间戳
func (c Carbon) TimestampMilli() int64 {
if c.IsInvalid() {
if c.Error != nil {
return 0
}
t := c.StdTime()
Expand All @@ -359,7 +359,7 @@ func (c Carbon) TimestampMilli() int64 {
// TimestampMicro gets timestamp with microsecond like 1596604455000000.
// 获取微秒级时间戳
func (c Carbon) TimestampMicro() int64 {
if c.IsInvalid() {
if c.Error != nil {
return 0
}
t := c.StdTime()
Expand All @@ -369,7 +369,7 @@ func (c Carbon) TimestampMicro() int64 {
// TimestampNano gets timestamp with nanosecond like 1596604455000000000.
// 获取纳秒级时间戳
func (c Carbon) TimestampNano() int64 {
if c.IsInvalid() {
if c.Error != nil {
return 0
}
return c.StdTime().UnixNano()
Expand All @@ -378,33 +378,45 @@ func (c Carbon) TimestampNano() int64 {
// Location gets location name like "PRC".
// 获取位置
func (c Carbon) Location() string {
if c.Error != nil {
return ""
}
return c.loc.String()
}

// Timezone gets timezone name like "CST".
// 获取时区
func (c Carbon) Timezone() string {
if c.Error != nil {
return ""
}
name, _ := c.StdTime().Zone()
return name
}

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

// Locale gets locale name like "zh-CN".
// 获取语言区域
func (c Carbon) Locale() string {
if c.Error != nil {
return ""
}
return c.lang.locale
}

// Age gets age like 18.
// 获取年龄
func (c Carbon) Age() int {
if c.IsInvalid() {
if c.Error != nil {
return 0
}
now := c.Now()
Expand Down

0 comments on commit b1a79ff

Please sign in to comment.