2021-02-18 14:32:31 +08:00
|
|
|
|
package carbon
|
|
|
|
|
|
2022-04-24 22:14:51 +08:00
|
|
|
|
import (
|
|
|
|
|
"time"
|
|
|
|
|
)
|
2021-08-16 09:36:04 +08:00
|
|
|
|
|
2024-03-06 09:26:51 +08:00
|
|
|
|
// StdTime gets standard time.Time.
|
|
|
|
|
// 获取标准 time.Time
|
2024-02-01 11:24:43 +08:00
|
|
|
|
func (c Carbon) StdTime() time.Time {
|
2024-10-17 15:15:30 +08:00
|
|
|
|
if c.time.IsZero() {
|
|
|
|
|
return c.time
|
|
|
|
|
}
|
2024-02-01 11:24:43 +08:00
|
|
|
|
return c.time.In(c.loc)
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-14 11:32:01 +08:00
|
|
|
|
// DaysInYear gets total days in year like 365.
|
2022-05-01 09:11:22 +08:00
|
|
|
|
// 获取本年的总天数
|
2021-02-18 14:32:31 +08:00
|
|
|
|
func (c Carbon) DaysInYear() int {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
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 {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2021-02-18 14:32:31 +08:00
|
|
|
|
return 0
|
|
|
|
|
}
|
2024-10-17 15:15:30 +08:00
|
|
|
|
return c.EndOfMonth().StdTime().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 {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2021-02-18 14:32:31 +08:00
|
|
|
|
return 0
|
|
|
|
|
}
|
2024-02-01 11:24:43 +08:00
|
|
|
|
return int(c.StdTime().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 {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2021-02-18 14:32:31 +08:00
|
|
|
|
return 0
|
|
|
|
|
}
|
2024-02-01 11:24:43 +08:00
|
|
|
|
return c.StdTime().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 {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2021-02-18 14:32:31 +08:00
|
|
|
|
return 0
|
|
|
|
|
}
|
2024-02-01 11:24:43 +08:00
|
|
|
|
return c.StdTime().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 {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2021-02-18 14:32:31 +08:00
|
|
|
|
return 0
|
|
|
|
|
}
|
2024-02-01 11:24:43 +08:00
|
|
|
|
day := int(c.StdTime().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 {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2021-02-18 14:32:31 +08:00
|
|
|
|
return 0
|
|
|
|
|
}
|
2024-02-01 11:24:43 +08:00
|
|
|
|
_, week := c.StdTime().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 {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
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.
|
2022-05-07 09:18:38 +08:00
|
|
|
|
// 获取当前年、月、日、时、分、秒
|
2022-04-12 17:33:42 +08:00
|
|
|
|
func (c Carbon) DateTime() (year, month, day, hour, minute, second int) {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2022-04-14 11:32:01 +08:00
|
|
|
|
return
|
2022-04-12 17:33:42 +08:00
|
|
|
|
}
|
2023-01-07 21:50:09 +08:00
|
|
|
|
year, month, day = c.Date()
|
|
|
|
|
hour, minute, second = c.Time()
|
|
|
|
|
return year, month, day, hour, minute, second
|
2022-04-12 17:33:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
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.
|
2022-05-07 09:18:38 +08:00
|
|
|
|
// 获取当前年、月、日、时、分、秒、毫秒
|
2022-04-14 11:32:01 +08:00
|
|
|
|
func (c Carbon) DateTimeMilli() (year, month, day, hour, minute, second, millisecond int) {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2022-04-14 11:32:01 +08:00
|
|
|
|
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.
|
2022-05-07 09:18:38 +08:00
|
|
|
|
// 获取当前年、月、日、时、分、秒、微秒
|
2022-04-14 11:32:01 +08:00
|
|
|
|
func (c Carbon) DateTimeMicro() (year, month, day, hour, minute, second, microsecond int) {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2022-04-14 11:32:01 +08:00
|
|
|
|
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.
|
2022-05-07 09:18:38 +08:00
|
|
|
|
// 获取当前年、月、日、时、分、秒、纳秒
|
2022-04-14 11:32:01 +08:00
|
|
|
|
func (c Carbon) DateTimeNano() (year, month, day, hour, minute, second, nanosecond int) {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2022-04-14 11:32:01 +08:00
|
|
|
|
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.
|
2022-05-07 09:18:38 +08:00
|
|
|
|
// 获取当前年、月、日
|
2022-04-12 17:33:42 +08:00
|
|
|
|
func (c Carbon) Date() (year, month, day int) {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2022-04-14 11:32:01 +08:00
|
|
|
|
return
|
2022-04-12 17:33:42 +08:00
|
|
|
|
}
|
|
|
|
|
var tm time.Month
|
2024-02-01 11:24:43 +08:00
|
|
|
|
year, tm, day = c.StdTime().Date()
|
2022-04-12 17:33:42 +08:00
|
|
|
|
return year, int(tm), day
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 09:06:28 +08:00
|
|
|
|
// DateMilli gets current year, month, day and millisecond like 2020, 8, 5, 999.
|
2022-05-07 09:18:38 +08:00
|
|
|
|
// 获取当前年、月、日、毫秒
|
2022-05-01 09:06:28 +08:00
|
|
|
|
func (c Carbon) DateMilli() (year, month, day, millisecond int) {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2022-05-01 09:06:28 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
2023-01-07 21:50:09 +08:00
|
|
|
|
year, month, day = c.Date()
|
|
|
|
|
return year, month, day, c.Millisecond()
|
2022-05-01 09:06:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DateMicro gets current year, month, day and microsecond like 2020, 8, 5, 999999.
|
2022-05-07 09:18:38 +08:00
|
|
|
|
// 获取当前年、月、日、微秒
|
2022-05-01 09:06:28 +08:00
|
|
|
|
func (c Carbon) DateMicro() (year, month, day, microsecond int) {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2022-05-01 09:06:28 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
2023-01-07 21:50:09 +08:00
|
|
|
|
year, month, day = c.Date()
|
|
|
|
|
return year, month, day, c.Microsecond()
|
2022-05-01 09:06:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DateNano gets current year, month, day and nanosecond like 2020, 8, 5, 999999999.
|
2022-05-07 09:18:38 +08:00
|
|
|
|
// 获取当前年、月、日、纳秒
|
2022-05-01 09:06:28 +08:00
|
|
|
|
func (c Carbon) DateNano() (year, month, day, nanosecond int) {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2022-05-01 09:06:28 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
2023-01-07 21:50:09 +08:00
|
|
|
|
year, month, day = c.Date()
|
|
|
|
|
return year, month, day, c.Nanosecond()
|
2022-05-01 09:06:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-14 11:32:01 +08:00
|
|
|
|
// Time gets current hour, minute, and second like 13, 14, 15.
|
2022-05-07 09:18:38 +08:00
|
|
|
|
// 获取当前时、分、秒
|
2022-04-12 17:33:42 +08:00
|
|
|
|
func (c Carbon) Time() (hour, minute, second int) {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2022-04-14 11:32:01 +08:00
|
|
|
|
return
|
2022-04-12 17:33:42 +08:00
|
|
|
|
}
|
2024-02-01 11:24:43 +08:00
|
|
|
|
return c.StdTime().Clock()
|
2022-04-12 17:33:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 09:06:28 +08:00
|
|
|
|
// TimeMilli gets current hour, minute, second and millisecond like 13, 14, 15, 999.
|
2022-05-07 09:18:38 +08:00
|
|
|
|
// 获取当前时、分、秒、毫秒
|
2022-05-01 09:06:28 +08:00
|
|
|
|
func (c Carbon) TimeMilli() (hour, minute, second, millisecond int) {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2022-05-01 09:06:28 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
2023-01-07 21:50:09 +08:00
|
|
|
|
hour, minute, second = c.Time()
|
2022-05-01 09:06:28 +08:00
|
|
|
|
return hour, minute, second, c.Millisecond()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TimeMicro gets current hour, minute, second and microsecond like 13, 14, 15, 999999.
|
2022-05-07 09:18:38 +08:00
|
|
|
|
// 获取当前时、分、秒、微秒
|
2022-05-01 09:06:28 +08:00
|
|
|
|
func (c Carbon) TimeMicro() (hour, minute, second, microsecond int) {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2022-05-01 09:06:28 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
2023-01-07 21:50:09 +08:00
|
|
|
|
hour, minute, second = c.Time()
|
2022-05-01 09:06:28 +08:00
|
|
|
|
return hour, minute, second, c.Microsecond()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TimeNano gets current hour, minute, second and nanosecond like 13, 14, 15, 999999999.
|
2022-05-07 09:18:38 +08:00
|
|
|
|
// 获取当前时、分、秒、纳秒
|
2022-05-01 09:06:28 +08:00
|
|
|
|
func (c Carbon) TimeNano() (hour, minute, second, nanosecond int) {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2022-05-01 09:06:28 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
2023-01-07 21:50:09 +08:00
|
|
|
|
hour, minute, second = c.Time()
|
2022-05-01 09:06:28 +08:00
|
|
|
|
return hour, minute, second, c.Nanosecond()
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-14 11:32:01 +08:00
|
|
|
|
// Century gets current century like 21.
|
2021-07-28 15:18:05 +08:00
|
|
|
|
// 获取当前世纪
|
2021-03-03 11:38:50 +08:00
|
|
|
|
func (c Carbon) Century() int {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2021-03-03 11:38:50 +08:00
|
|
|
|
return 0
|
|
|
|
|
}
|
2021-04-04 22:45:53 +08:00
|
|
|
|
return c.Year()/YearsPerCentury + 1
|
2021-03-03 11:38:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
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 {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2021-02-18 14:32:31 +08:00
|
|
|
|
return 0
|
|
|
|
|
}
|
2024-02-01 11:24:43 +08:00
|
|
|
|
return c.StdTime().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) {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2022-04-17 19:51:07 +08:00
|
|
|
|
return
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
2022-04-12 17:33:42 +08:00
|
|
|
|
month := c.Month()
|
2021-02-18 14:32:31 +08:00
|
|
|
|
switch {
|
2022-04-12 17:33:42 +08:00
|
|
|
|
case month >= 10:
|
2021-07-19 09:52:03 +08:00
|
|
|
|
quarter = 4
|
2022-04-12 17:33:42 +08:00
|
|
|
|
case month >= 7:
|
2021-07-19 09:52:03 +08:00
|
|
|
|
quarter = 3
|
2022-04-12 17:33:42 +08:00
|
|
|
|
case month >= 4:
|
2021-07-19 09:52:03 +08:00
|
|
|
|
quarter = 2
|
2022-04-12 17:33:42 +08:00
|
|
|
|
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 {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2021-02-23 09:32:55 +08:00
|
|
|
|
return -1
|
|
|
|
|
}
|
2021-08-16 09:36:04 +08:00
|
|
|
|
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 {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2021-02-18 14:32:31 +08:00
|
|
|
|
return 0
|
|
|
|
|
}
|
2024-02-01 11:24:43 +08:00
|
|
|
|
return c.StdTime().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 {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2021-02-18 14:32:31 +08:00
|
|
|
|
return 0
|
|
|
|
|
}
|
2024-02-01 11:24:43 +08:00
|
|
|
|
return c.StdTime().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 {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2021-02-18 14:32:31 +08:00
|
|
|
|
return 0
|
|
|
|
|
}
|
2024-02-01 11:24:43 +08:00
|
|
|
|
return c.StdTime().Second()
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-14 11:32:01 +08:00
|
|
|
|
// Millisecond gets current millisecond like 999.
|
2022-05-07 09:18:38 +08:00
|
|
|
|
// 获取当前毫秒数
|
2021-02-18 14:32:31 +08:00
|
|
|
|
func (c Carbon) Millisecond() int {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2021-02-18 14:32:31 +08:00
|
|
|
|
return 0
|
|
|
|
|
}
|
2024-02-01 11:24:43 +08:00
|
|
|
|
return c.StdTime().Nanosecond() / 1e6
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-14 11:32:01 +08:00
|
|
|
|
// Microsecond gets current microsecond like 999999.
|
2022-05-07 09:18:38 +08:00
|
|
|
|
// 获取当前微秒数
|
2021-02-18 14:32:31 +08:00
|
|
|
|
func (c Carbon) Microsecond() int {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2021-02-18 14:32:31 +08:00
|
|
|
|
return 0
|
|
|
|
|
}
|
2024-02-01 11:24:43 +08:00
|
|
|
|
return c.StdTime().Nanosecond() / 1e3
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-14 11:32:01 +08:00
|
|
|
|
// Nanosecond gets current nanosecond like 999999999.
|
2022-05-07 09:18:38 +08:00
|
|
|
|
// 获取当前纳秒数
|
2021-02-18 14:32:31 +08:00
|
|
|
|
func (c Carbon) Nanosecond() int {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2021-02-18 14:32:31 +08:00
|
|
|
|
return 0
|
|
|
|
|
}
|
2024-02-01 11:24:43 +08:00
|
|
|
|
return c.StdTime().Nanosecond()
|
2021-08-16 09:36:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-07 09:18:38 +08:00
|
|
|
|
// Timestamp gets timestamp with second like 1596604455.
|
2021-08-16 09:36:04 +08:00
|
|
|
|
// 输出秒级时间戳
|
2022-05-07 09:18:38 +08:00
|
|
|
|
func (c Carbon) Timestamp() int64 {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2021-08-16 09:36:04 +08:00
|
|
|
|
return 0
|
|
|
|
|
}
|
2024-02-01 11:24:43 +08:00
|
|
|
|
return c.StdTime().Unix()
|
2022-04-12 17:33:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-07 09:18:38 +08:00
|
|
|
|
// TimestampMilli gets timestamp with millisecond like 1596604455000.
|
2021-08-16 09:36:04 +08:00
|
|
|
|
// 获取毫秒级时间戳
|
2022-05-07 09:18:38 +08:00
|
|
|
|
func (c Carbon) TimestampMilli() int64 {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2021-08-16 09:36:04 +08:00
|
|
|
|
return 0
|
|
|
|
|
}
|
2024-02-01 11:24:43 +08:00
|
|
|
|
t := c.StdTime()
|
2022-05-07 09:18:38 +08:00
|
|
|
|
return t.Unix()*1e3 + int64(t.Nanosecond())/1e6
|
2021-08-16 09:36:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-07 09:18:38 +08:00
|
|
|
|
// TimestampMicro gets timestamp with microsecond like 1596604455000000.
|
2021-08-16 09:36:04 +08:00
|
|
|
|
// 获取微秒级时间戳
|
2022-05-07 09:18:38 +08:00
|
|
|
|
func (c Carbon) TimestampMicro() int64 {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2021-08-16 09:36:04 +08:00
|
|
|
|
return 0
|
|
|
|
|
}
|
2024-02-01 11:24:43 +08:00
|
|
|
|
t := c.StdTime()
|
2022-05-07 09:18:38 +08:00
|
|
|
|
return t.Unix()*1e6 + int64(t.Nanosecond())/1e3
|
2021-08-16 09:36:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-07 09:18:38 +08:00
|
|
|
|
// TimestampNano gets timestamp with nanosecond like 1596604455000000000.
|
2021-08-16 09:36:04 +08:00
|
|
|
|
// 获取纳秒级时间戳
|
2022-05-07 09:18:38 +08:00
|
|
|
|
func (c Carbon) TimestampNano() int64 {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2021-08-16 09:36:04 +08:00
|
|
|
|
return 0
|
|
|
|
|
}
|
2024-02-01 11:24:43 +08:00
|
|
|
|
return c.StdTime().UnixNano()
|
2022-04-12 17:33:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2021-08-16 09:36:04 +08:00
|
|
|
|
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 {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2024-02-01 11:24:43 +08:00
|
|
|
|
name, _ := c.StdTime().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 {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
2024-02-01 11:24:43 +08:00
|
|
|
|
_, offset := c.StdTime().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 {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2021-08-16 09:36:04 +08:00
|
|
|
|
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 {
|
2024-04-09 20:13:50 +08:00
|
|
|
|
if c.Error != nil {
|
2021-02-23 09:32:55 +08:00
|
|
|
|
return 0
|
|
|
|
|
}
|
2022-04-14 11:32:01 +08:00
|
|
|
|
now := c.Now()
|
2023-12-27 11:14:52 +08:00
|
|
|
|
if c.IsSetTestNow() {
|
2023-10-16 14:06:45 +08:00
|
|
|
|
now = CreateFromTimestampNano(c.testNow, c.Location())
|
|
|
|
|
}
|
|
|
|
|
if c.TimestampNano() > now.TimestampNano() {
|
2021-02-23 09:32:55 +08:00
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
return int(c.DiffInYears(now))
|
|
|
|
|
}
|