carbon/getter.go

252 lines
4.5 KiB
Go
Raw Normal View History

2021-02-18 14:32:31 +08:00
package carbon
2021-07-28 15:18:05 +08:00
// DaysInYear get days in year
// 获取本年的总天数
2021-02-18 14:32:31 +08:00
func (c Carbon) DaysInYear() int {
2021-07-23 11:01:00 +08:00
if c.IsInvalid() {
2021-02-18 14:32:31 +08:00
return 0
}
if c.IsLeapYear() {
2021-08-05 19:40:40 +08:00
return DaysPerLeapYear
2021-02-18 14:32:31 +08:00
}
2021-08-05 19:40:40 +08:00
return DaysPerNormalYear
2021-02-18 14:32:31 +08:00
}
2021-07-28 15:18:05 +08:00
// DaysInMonth get days in month
// 获取本月的总天数
2021-02-18 14:32:31 +08:00
func (c Carbon) DaysInMonth() int {
2021-07-23 11:01:00 +08:00
if c.IsInvalid() {
2021-02-18 14:32:31 +08:00
return 0
}
return c.EndOfMonth().Time.In(c.Loc).Day()
}
2021-07-28 15:18:05 +08:00
// MonthOfYear get month of year
2021-08-02 10:24:59 +08:00
// 获取本年的第几月
2021-02-18 14:32:31 +08:00
func (c Carbon) MonthOfYear() int {
2021-07-23 11:01:00 +08:00
if c.IsInvalid() {
2021-02-18 14:32:31 +08:00
return 0
}
return int(c.Time.In(c.Loc).Month())
}
2021-08-02 10:24:59 +08:00
// DayOfYear get day of year
// 获取本年的第几天
2021-02-18 14:32:31 +08:00
func (c Carbon) DayOfYear() int {
2021-07-23 11:01:00 +08:00
if c.IsInvalid() {
2021-02-18 14:32:31 +08:00
return 0
}
return c.Time.In(c.Loc).YearDay()
}
2021-08-02 10:24:59 +08:00
// DayOfMonth get day of month
// 获取本月的第几天
2021-02-18 14:32:31 +08:00
func (c Carbon) DayOfMonth() int {
2021-07-23 11:01:00 +08:00
if c.IsInvalid() {
2021-02-18 14:32:31 +08:00
return 0
}
return c.Time.In(c.Loc).Day()
}
2021-08-02 10:24:59 +08:00
// DayOfWeek get day of week
// 获取本周的第几天
2021-02-18 14:32:31 +08:00
func (c Carbon) DayOfWeek() int {
2021-07-23 11:01:00 +08:00
if c.IsInvalid() {
2021-02-18 14:32:31 +08:00
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-08-02 10:24:59 +08:00
// WeekOfYear get week of year. see https://en.wikipedia.org/wiki/ISO_8601#Week_dates
// 获取本年的第几周
2021-02-18 14:32:31 +08:00
func (c Carbon) WeekOfYear() int {
2021-07-23 11:01:00 +08:00
if c.IsInvalid() {
2021-02-18 14:32:31 +08:00
return 0
}
_, week := c.Time.In(c.Loc).ISOWeek()
return week
}
2021-08-02 10:24:59 +08:00
// WeekOfMonth get week of month
// 获取本月的第几周
2021-02-18 14:32:31 +08:00
func (c Carbon) WeekOfMonth() int {
2021-07-23 11:01:00 +08:00
if c.IsInvalid() {
2021-02-18 14:32:31 +08:00
return 0
}
2021-07-31 13:02:31 +08:00
days := c.Day() + c.StartOfMonth().DayOfWeek() - 1
if days%DaysPerWeek == 0 {
return days / DaysPerWeek
2021-02-18 14:32:31 +08:00
}
2021-07-31 13:02:31 +08:00
return days/DaysPerWeek + 1
2021-02-18 14:32:31 +08:00
}
2021-07-28 15:18:05 +08:00
// Century get current century
// 获取当前世纪
func (c Carbon) Century() int {
2021-07-23 11:01:00 +08:00
if c.IsInvalid() {
return 0
}
2021-04-04 22:45:53 +08:00
return c.Year()/YearsPerCentury + 1
}
2021-07-28 15:18:05 +08:00
// Decade get current decade
// 获取当前年代
2021-07-09 15:13:13 +08:00
func (c Carbon) Decade() int {
2021-07-23 11:01:00 +08:00
if c.IsInvalid() {
2021-07-09 15:13:13 +08:00
return 0
}
return c.Year() % YearsPerCentury / YearsPerDecade * YearsPerDecade
}
2021-07-28 15:18:05 +08:00
// Year get current year
// 获取当前年
2021-02-18 14:32:31 +08:00
func (c Carbon) Year() int {
2021-07-23 11:01:00 +08:00
if c.IsInvalid() {
2021-02-18 14:32:31 +08:00
return 0
}
return c.Time.In(c.Loc).Year()
}
2021-07-28 15:18:05 +08:00
// Quarter get current quarter
// 获取当前季度
2021-07-19 09:52:03 +08:00
func (c Carbon) Quarter() (quarter int) {
2021-07-23 11:01:00 +08:00
if c.IsInvalid() {
2021-02-18 14:32:31 +08:00
return 0
}
switch {
2021-02-23 09:32:55 +08:00
case c.Month() >= 10:
2021-07-19 09:52:03 +08:00
quarter = 4
2021-02-23 09:32:55 +08:00
case c.Month() >= 7:
2021-07-19 09:52:03 +08:00
quarter = 3
2021-02-23 09:32:55 +08:00
case c.Month() >= 4:
2021-07-19 09:52:03 +08:00
quarter = 2
2021-02-23 09:32:55 +08:00
case c.Month() >= 1:
2021-07-19 09:52:03 +08:00
quarter = 1
2021-02-18 14:32:31 +08:00
}
2021-07-19 09:52:03 +08:00
return
2021-02-18 14:32:31 +08:00
}
2021-07-28 15:18:05 +08:00
// Month get current month
// 获取当前月
2021-02-18 14:32:31 +08:00
func (c Carbon) Month() int {
2021-07-23 11:01:00 +08:00
if c.IsInvalid() {
2021-02-18 14:32:31 +08:00
return 0
}
return c.MonthOfYear()
}
2021-07-28 15:18:05 +08:00
// Week get current week, start from 0
// 获取当前周(从0开始)
2021-02-23 09:32:55 +08:00
func (c Carbon) Week() int {
2021-07-23 11:01:00 +08:00
if c.IsInvalid() {
2021-02-23 09:32:55 +08:00
return -1
}
return int(c.Time.In(c.Loc).Weekday())
}
2021-07-28 15:18:05 +08:00
// Day get current day
// 获取当前日
2021-02-18 14:32:31 +08:00
func (c Carbon) Day() int {
2021-07-23 11:01:00 +08:00
if c.IsInvalid() {
2021-02-18 14:32:31 +08:00
return 0
}
return c.DayOfMonth()
}
2021-07-28 15:18:05 +08:00
// Hour get current hour
// 获取当前小时
2021-02-18 14:32:31 +08:00
func (c Carbon) Hour() int {
2021-07-23 11:01:00 +08:00
if c.IsInvalid() {
2021-02-18 14:32:31 +08:00
return 0
}
return c.Time.In(c.Loc).Hour()
}
2021-07-28 15:18:05 +08:00
// Minute get current minute
// 获取当前分钟数
2021-02-18 14:32:31 +08:00
func (c Carbon) Minute() int {
2021-07-23 11:01:00 +08:00
if c.IsInvalid() {
2021-02-18 14:32:31 +08:00
return 0
}
return c.Time.In(c.Loc).Minute()
}
2021-07-28 15:18:05 +08:00
// Second get current second
// 获取当前秒数
2021-02-18 14:32:31 +08:00
func (c Carbon) Second() int {
2021-07-23 11:01:00 +08:00
if c.IsInvalid() {
2021-02-18 14:32:31 +08:00
return 0
}
return c.Time.In(c.Loc).Second()
}
2021-07-28 15:18:05 +08:00
// Millisecond get current millisecond
2021-08-05 19:40:40 +08:00
// 获取当前毫秒数3位数字
2021-02-18 14:32:31 +08:00
func (c Carbon) Millisecond() int {
2021-07-23 11:01:00 +08:00
if c.IsInvalid() {
2021-02-18 14:32:31 +08:00
return 0
}
return c.Time.In(c.Loc).Nanosecond() / 1e6
}
2021-07-28 15:18:05 +08:00
// Microsecond get current microsecond
2021-08-05 19:40:40 +08:00
// 获取当前微秒数6位数字
2021-02-18 14:32:31 +08:00
func (c Carbon) Microsecond() int {
2021-07-23 11:01:00 +08:00
if c.IsInvalid() {
2021-02-18 14:32:31 +08:00
return 0
}
2021-07-19 09:52:03 +08:00
return c.Time.In(c.Loc).Nanosecond() / 1e3
2021-02-18 14:32:31 +08:00
}
2021-07-28 15:18:05 +08:00
// Nanosecond get current nanosecond
// 获取当前纳秒数9位数字
2021-02-18 14:32:31 +08:00
func (c Carbon) Nanosecond() int {
2021-07-23 11:01:00 +08:00
if c.IsInvalid() {
2021-02-18 14:32:31 +08:00
return 0
}
return c.Time.In(c.Loc).Nanosecond()
}
2021-02-23 09:32:55 +08:00
2021-07-28 15:18:05 +08:00
// Location get location name
// 获取位置
2021-07-23 11:01:00 +08:00
func (c Carbon) Location() string {
return c.Loc.String()
}
2021-07-28 15:18:05 +08:00
// Timezone get timezone name
// 获取时区
2021-02-23 09:32:55 +08:00
func (c Carbon) Timezone() string {
2021-07-23 11:01:00 +08:00
name, _ := c.Time.Zone()
return name
}
2021-07-28 15:18:05 +08:00
// Offset get offset seconds from the UTC timezone
// 获取距离UTC时区的偏移量单位秒
2021-07-23 11:01:00 +08:00
func (c Carbon) Offset() int {
_, offset := c.Time.Zone()
return offset
2021-02-23 09:32:55 +08:00
}
2021-07-28 15:18:05 +08:00
// Locale get locale name
// 获取语言区域
2021-02-23 09:32:55 +08:00
func (c Carbon) Locale() string {
return c.Lang.locale
}
2021-07-28 15:18:05 +08:00
// Age get age
// 获取年龄
2021-02-23 09:32:55 +08:00
func (c Carbon) Age() int {
2021-07-23 11:01:00 +08:00
if c.IsInvalid() {
2021-02-23 09:32:55 +08:00
return 0
}
now := Now()
if c.ToTimestamp() > now.ToTimestamp() {
return 0
}
return int(c.DiffInYears(now))
}