carbon/getter.go

372 lines
8.7 KiB
Go
Raw Normal View History

2021-02-18 14:32:31 +08:00
package carbon
import "time"
2022-04-14 11:32:01 +08:00
// DaysInYear gets total days in year like 365.
2021-07-28 15:18:05 +08:00
// 获取本年的总天数
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
}
2022-04-14 11:32:01 +08:00
// DaysInMonth gets total days in month like 30.
2021-07-28 15:18:05 +08:00
// 获取本月的总天数
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-02-18 14:32:31 +08:00
}
2022-04-14 11:32:01 +08:00
// MonthOfYear gets month of year like 12.
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-02-18 14:32:31 +08:00
}
2022-04-14 11:32:01 +08:00
// DayOfYear gets day of year like 365.
2021-08-02 10:24:59 +08:00
// 获取本年的第几天
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-02-18 14:32:31 +08:00
}
2022-04-14 11:32:01 +08:00
// DayOfMonth gets day of month like 30.
2021-08-02 10:24:59 +08:00
// 获取本月的第几天
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-02-18 14:32:31 +08:00
}
2022-04-14 11:32:01 +08:00
// DayOfWeek gets day of week like 6.
2021-08-02 10:24:59 +08:00
// 获取本周的第几天
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
}
day := int(c.time.In(c.loc).Weekday())
2021-02-23 09:32:55 +08:00
if day == 0 {
return DaysPerWeek
}
return day
2021-02-18 14:32:31 +08:00
}
2022-04-14 11:32:01 +08:00
// WeekOfYear gets week of year like 1, see https://en.wikipedia.org/wiki/ISO_8601#Week_dates.
2021-08-02 10:24:59 +08:00
// 获取本年的第几周
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()
2021-02-18 14:32:31 +08:00
return week
}
2022-04-14 11:32:01 +08:00
// WeekOfMonth gets week of month like 1.
2021-08-02 10:24:59 +08:00
// 获取本月的第几周
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
}
2022-04-14 11:32:01 +08:00
// 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() {
2022-04-14 11:32:01 +08:00
return
}
carbon := c.time.In(c.loc)
var tm time.Month
year, tm, day = carbon.Date()
hour, minute, second = carbon.Clock()
return year, int(tm), day, hour, minute, second
}
2022-04-14 11:32:01 +08:00
// 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() {
return
}
year, month, day, hour, minute, second = c.DateTime()
return year, month, day, hour, minute, second, c.Millisecond()
}
// 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() {
return
}
year, month, day, hour, minute, second = c.DateTime()
return year, month, day, hour, minute, second, c.Microsecond()
}
// 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() {
return
}
year, month, day, hour, minute, second = c.DateTime()
return year, month, day, hour, minute, second, c.Nanosecond()
}
// Date gets current year, month, and day like 2020, 8, 5.
// 获取当前年月日
func (c Carbon) Date() (year, month, day int) {
if c.IsInvalid() {
2022-04-14 11:32:01 +08:00
return
}
var tm time.Month
year, tm, day = c.time.In(c.loc).Date()
return year, int(tm), day
}
2022-04-14 11:32:01 +08:00
// Time gets current hour, minute, and second like 13, 14, 15.
// 获取当前时分秒
func (c Carbon) Time() (hour, minute, second int) {
if c.IsInvalid() {
2022-04-14 11:32:01 +08:00
return
}
return c.time.In(c.loc).Clock()
}
2022-04-14 11:32:01 +08:00
// Century gets current century like 21.
2021-07-28 15:18:05 +08:00
// 获取当前世纪
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
}
2022-04-14 11:32:01 +08:00
// Decade gets current decade like 20.
2021-07-28 15:18:05 +08:00
// 获取当前年代
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
}
2022-04-14 11:32:01 +08:00
// Year gets current year like 2020.
2021-07-28 15:18:05 +08:00
// 获取当前年
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-02-18 14:32:31 +08:00
}
2022-04-14 11:32:01 +08:00
// Quarter gets current quarter like 3.
2021-07-28 15:18:05 +08:00
// 获取当前季度
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
}
month := c.Month()
2021-02-18 14:32:31 +08:00
switch {
case month >= 10:
2021-07-19 09:52:03 +08:00
quarter = 4
case month >= 7:
2021-07-19 09:52:03 +08:00
quarter = 3
case month >= 4:
2021-07-19 09:52:03 +08:00
quarter = 2
case 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
}
2022-04-14 11:32:01 +08:00
// Month gets current month like 8.
2021-07-28 15:18:05 +08:00
// 获取当前月
2021-02-18 14:32:31 +08:00
func (c Carbon) Month() int {
return c.MonthOfYear()
}
2022-04-14 11:32:01 +08:00
// Week gets current week like 6, start from 0.
2021-07-28 15:18:05 +08:00
// 获取当前周(从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 (c.DayOfWeek() + DaysPerWeek - int(c.weekStartsAt)) % DaysPerWeek
2021-02-23 09:32:55 +08:00
}
2022-04-14 11:32:01 +08:00
// Day gets current day like 5.
2021-07-28 15:18:05 +08:00
// 获取当前日
2021-02-18 14:32:31 +08:00
func (c Carbon) Day() int {
return c.DayOfMonth()
}
2022-04-14 11:32:01 +08:00
// Hour gets current hour like 13.
2021-07-28 15:18:05 +08:00
// 获取当前小时
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-02-18 14:32:31 +08:00
}
2022-04-14 11:32:01 +08:00
// Minute gets current minute like 14.
2021-07-28 15:18:05 +08:00
// 获取当前分钟数
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-02-18 14:32:31 +08:00
}
2022-04-14 11:32:01 +08:00
// Second gets current second like 15.
2021-07-28 15:18:05 +08:00
// 获取当前秒数
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-02-18 14:32:31 +08:00
}
2022-04-14 11:32:01 +08:00
// Millisecond gets current millisecond like 999.
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-02-18 14:32:31 +08:00
}
2022-04-14 11:32:01 +08:00
// Microsecond gets current microsecond like 999999.
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
}
return c.time.In(c.loc).Nanosecond() / 1e3
2021-02-18 14:32:31 +08:00
}
2022-04-14 11:32:01 +08:00
// Nanosecond gets current nanosecond like 999999999.
2021-07-28 15:18:05 +08:00
// 获取当前纳秒数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()
}
2022-04-14 11:32:01 +08:00
// TimestampWithSecond gets timestamp with second like 1596604455.
// 输出秒级时间戳
func (c Carbon) TimestampWithSecond() int64 {
if c.IsInvalid() {
return 0
}
return c.time.Unix()
}
2022-04-14 11:32:01 +08:00
// Timestamp gets timestamp with second like 1596604455, it is shorthand for TimestampWithSecond.
// 获取秒级时间戳, 是 TimestampWithSecond 的简写
func (c Carbon) Timestamp() int64 {
return c.TimestampWithSecond()
}
2022-04-14 11:32:01 +08:00
// TimestampWithMillisecond gets timestamp with millisecond like 1596604455000.
// 获取毫秒级时间戳
func (c Carbon) TimestampWithMillisecond() int64 {
if c.IsInvalid() {
return 0
}
return c.time.Unix()*1e3 + int64(c.time.Nanosecond())/1e6
}
2022-04-14 11:32:01 +08:00
// TimestampMilli gets timestamp with millisecond like 1596604455000, it is shorthand for TimestampWithMillisecond.
// 获取毫秒级时间戳, 是 TimestampMilli 的简写
func (c Carbon) TimestampMilli() int64 {
return c.TimestampWithMillisecond()
}
2022-04-14 11:32:01 +08:00
// TimestampWithMicrosecond gets timestamp with microsecond like 1596604455000000.
// 获取微秒级时间戳
func (c Carbon) TimestampWithMicrosecond() int64 {
if c.IsInvalid() {
return 0
}
return c.time.Unix()*1e6 + int64(c.time.Nanosecond())/1e3
}
2022-04-14 11:32:01 +08:00
// TimestampMicro gets timestamp with microsecond like 1596604455000000, it is shorthand for TimestampWithMicrosecond.
// 获取微秒级时间戳, 是 TimestampWithMicrosecond 的简写
func (c Carbon) TimestampMicro() int64 {
return c.TimestampWithMicrosecond()
}
2022-04-14 11:32:01 +08:00
// TimestampWithNanosecond gets timestamp with nanosecond like 1596604455000000000.
// 获取纳秒级时间戳
func (c Carbon) TimestampWithNanosecond() int64 {
if c.IsInvalid() {
return 0
}
return c.time.UnixNano()
2021-02-18 14:32:31 +08:00
}
2021-02-23 09:32:55 +08:00
2022-04-14 11:32:01 +08:00
// TimestampNano gets timestamp with nanosecond like 1596604455000000000, it is shorthand for TimestampWithNanosecond.
// 获取纳秒级时间戳, 是 TimestampWithNanosecond 的简写
func (c Carbon) TimestampNano() int64 {
return c.TimestampWithNanosecond()
}
2022-04-14 11:32:01 +08:00
// Location gets location name like "PRC".
2021-07-28 15:18:05 +08:00
// 获取位置
2021-07-23 11:01:00 +08:00
func (c Carbon) Location() string {
return c.loc.String()
2021-07-23 11:01:00 +08:00
}
2022-04-14 11:32:01 +08:00
// Timezone gets timezone name like "CST".
2021-07-28 15:18:05 +08:00
// 获取时区
2021-02-23 09:32:55 +08:00
func (c Carbon) Timezone() string {
name, _ := c.time.Zone()
2021-07-23 11:01:00 +08:00
return name
}
2022-04-14 11:32:01 +08:00
// Offset gets offset seconds from the UTC timezone like 28800.
2021-07-28 15:18:05 +08:00
// 获取距离UTC时区的偏移量单位秒
2021-07-23 11:01:00 +08:00
func (c Carbon) Offset() int {
_, offset := c.time.Zone()
2021-07-23 11:01:00 +08:00
return offset
2021-02-23 09:32:55 +08:00
}
2022-04-14 11:32:01 +08:00
// Locale gets locale name like "zh-CN".
2021-07-28 15:18:05 +08:00
// 获取语言区域
2021-02-23 09:32:55 +08:00
func (c Carbon) Locale() string {
return c.lang.locale
2021-02-23 09:32:55 +08:00
}
2022-04-14 11:32:01 +08:00
// Age gets age like 18.
2021-07-28 15:18:05 +08:00
// 获取年龄
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
}
2022-04-14 11:32:01 +08:00
now := c.Now()
if c.Timestamp() > now.Timestamp() {
2021-02-23 09:32:55 +08:00
return 0
}
return int(c.DiffInYears(now))
}