2021-02-18 14:32:31 +08:00
|
|
|
package carbon
|
|
|
|
|
|
|
|
// DaysInYear 获取本年的总天数
|
|
|
|
func (c Carbon) DaysInYear() int {
|
|
|
|
if c.IsZero() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
days := DaysPerNormalYear
|
|
|
|
if c.IsLeapYear() {
|
|
|
|
days = DaysPerLeapYear
|
|
|
|
}
|
|
|
|
return days
|
|
|
|
}
|
|
|
|
|
|
|
|
// DaysInMonth 获取本月的总天数
|
|
|
|
func (c Carbon) DaysInMonth() int {
|
|
|
|
if c.IsZero() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return c.EndOfMonth().Time.In(c.Loc).Day()
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:32:55 +08:00
|
|
|
// MonthOfYear 获取本年的第几月(从1开始)
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) MonthOfYear() int {
|
|
|
|
if c.IsZero() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return int(c.Time.In(c.Loc).Month())
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:32:55 +08:00
|
|
|
// DayOfYear 获取本年的第几天(从1开始)
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) DayOfYear() int {
|
|
|
|
if c.IsZero() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return c.Time.In(c.Loc).YearDay()
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:32:55 +08:00
|
|
|
// DayOfMonth 获取本月的第几天(从1开始)
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) DayOfMonth() int {
|
|
|
|
if c.IsZero() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return c.Time.In(c.Loc).Day()
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:32:55 +08:00
|
|
|
// DayOfWeek 获取本周的第几天(从1开始)
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) DayOfWeek() int {
|
|
|
|
if c.IsZero() {
|
|
|
|
return 0
|
|
|
|
}
|
2021-02-23 09:32:55 +08:00
|
|
|
day := int(c.Time.In(c.Loc).Weekday())
|
|
|
|
if day == 0 {
|
|
|
|
return DaysPerWeek
|
|
|
|
}
|
|
|
|
return day
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
|
2021-02-23 09:32:55 +08:00
|
|
|
// WeekOfYear 获取本年的第几周(从1开始)
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) WeekOfYear() int {
|
|
|
|
if c.IsZero() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
_, week := c.Time.In(c.Loc).ISOWeek()
|
|
|
|
return week
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:32:55 +08:00
|
|
|
// WeekOfMonth 获取本月的第几周(从1开始)
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) WeekOfMonth() int {
|
|
|
|
if c.IsZero() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
day := c.Time.In(c.Loc).Day()
|
|
|
|
if day < DaysPerWeek {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return day%DaysPerWeek + 1
|
|
|
|
}
|
|
|
|
|
2021-03-03 11:38:50 +08:00
|
|
|
// Century 获取当前世纪
|
|
|
|
func (c Carbon) Century() int {
|
|
|
|
if c.IsZero() {
|
|
|
|
return 0
|
|
|
|
}
|
2021-04-04 22:45:53 +08:00
|
|
|
return c.Year()/YearsPerCentury + 1
|
2021-03-03 11:38:50 +08:00
|
|
|
}
|
|
|
|
|
2021-02-18 15:43:44 +08:00
|
|
|
// Year 获取当前年
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) Year() int {
|
|
|
|
if c.IsZero() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return c.Time.In(c.Loc).Year()
|
|
|
|
}
|
|
|
|
|
2021-02-18 15:43:44 +08:00
|
|
|
// Quarter 获取当前季度
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) Quarter() int {
|
|
|
|
if c.IsZero() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
switch {
|
2021-02-23 09:32:55 +08:00
|
|
|
case c.Month() >= 10:
|
2021-02-18 14:32:31 +08:00
|
|
|
return 4
|
2021-02-23 09:32:55 +08:00
|
|
|
case c.Month() >= 7:
|
2021-02-18 14:32:31 +08:00
|
|
|
return 3
|
2021-02-23 09:32:55 +08:00
|
|
|
case c.Month() >= 4:
|
2021-02-18 14:32:31 +08:00
|
|
|
return 2
|
2021-02-23 09:32:55 +08:00
|
|
|
case c.Month() >= 1:
|
2021-02-18 14:32:31 +08:00
|
|
|
return 1
|
2021-02-23 09:32:55 +08:00
|
|
|
default:
|
|
|
|
return 0
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 15:43:44 +08:00
|
|
|
// Month 获取当前月
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) Month() int {
|
|
|
|
if c.IsZero() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return c.MonthOfYear()
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:32:55 +08:00
|
|
|
// Week 获取当前周(从0开始)
|
|
|
|
func (c Carbon) Week() int {
|
|
|
|
if c.IsZero() {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return int(c.Time.In(c.Loc).Weekday())
|
|
|
|
}
|
|
|
|
|
2021-02-18 15:43:44 +08:00
|
|
|
// Day 获取当前日
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) Day() int {
|
|
|
|
if c.IsZero() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return c.DayOfMonth()
|
|
|
|
}
|
|
|
|
|
2021-02-18 15:43:44 +08:00
|
|
|
// Hour 获取当前小时
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) Hour() int {
|
|
|
|
if c.IsZero() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return c.Time.In(c.Loc).Hour()
|
|
|
|
}
|
|
|
|
|
2021-02-18 15:43:44 +08:00
|
|
|
// Minute 获取当前分钟数
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) Minute() int {
|
|
|
|
if c.IsZero() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return c.Time.In(c.Loc).Minute()
|
|
|
|
}
|
|
|
|
|
2021-02-18 15:43:44 +08:00
|
|
|
// Second 获取当前秒数
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) Second() int {
|
|
|
|
if c.IsZero() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return c.Time.In(c.Loc).Second()
|
|
|
|
}
|
|
|
|
|
2021-02-18 15:43:44 +08:00
|
|
|
// Millisecond 获取当前毫秒数
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) Millisecond() int {
|
|
|
|
if c.IsZero() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return c.Time.In(c.Loc).Nanosecond() / 1e6
|
|
|
|
}
|
|
|
|
|
2021-02-18 15:43:44 +08:00
|
|
|
// Microsecond 获取当前微秒数
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) Microsecond() int {
|
|
|
|
if c.IsZero() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return c.Time.In(c.Loc).Nanosecond() / 1e9
|
|
|
|
}
|
|
|
|
|
2021-02-18 15:43:44 +08:00
|
|
|
// Nanosecond 获取当前纳秒数
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) Nanosecond() int {
|
|
|
|
if c.IsZero() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return c.Time.In(c.Loc).Nanosecond()
|
|
|
|
}
|
2021-02-23 09:32:55 +08:00
|
|
|
|
|
|
|
// Timezone 获取时区
|
|
|
|
func (c Carbon) Timezone() string {
|
|
|
|
return c.Loc.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Locale 获取语言区域
|
|
|
|
func (c Carbon) Locale() string {
|
|
|
|
return c.Lang.locale
|
|
|
|
}
|
|
|
|
|
|
|
|
// Age 获取年龄
|
|
|
|
func (c Carbon) Age() int {
|
|
|
|
if c.IsZero() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
now := Now()
|
|
|
|
if c.ToTimestamp() > now.ToTimestamp() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return int(c.DiffInYears(now))
|
|
|
|
}
|