2021-02-18 14:32:31 +08:00
|
|
|
package carbon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-09-04 10:48:22 +08:00
|
|
|
// CreateFromStdTime creates a Carbon instance from standard time.Time.
|
|
|
|
// 从标准的 time.Time 创建 Carbon 实例
|
2024-01-02 23:24:50 +08:00
|
|
|
func CreateFromStdTime(tt time.Time, timezone ...string) Carbon {
|
2023-09-04 10:48:22 +08:00
|
|
|
c := NewCarbon()
|
|
|
|
c.loc = tt.Location()
|
2024-01-02 23:24:50 +08:00
|
|
|
if len(timezone) > 0 {
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
|
|
|
}
|
|
|
|
c.time = tt
|
2023-09-04 10:48:22 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2022-04-12 23:22:14 +08:00
|
|
|
// CreateFromTimestamp creates a Carbon instance from a given timestamp with second.
|
2022-04-01 18:19:46 +08:00
|
|
|
// 从给定的秒级时间戳创建 Carbon 实例
|
2022-04-12 23:22:14 +08:00
|
|
|
func (c Carbon) CreateFromTimestamp(timestamp int64, timezone ...string) Carbon {
|
2021-07-31 13:27:17 +08:00
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-07-23 10:56:39 +08:00
|
|
|
}
|
|
|
|
if c.Error != nil {
|
2021-07-19 09:51:04 +08:00
|
|
|
return c
|
|
|
|
}
|
2022-04-01 18:19:46 +08:00
|
|
|
c.time = time.Unix(timestamp, 0)
|
2021-02-18 14:32:31 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2022-04-12 23:22:14 +08:00
|
|
|
// CreateFromTimestamp creates a Carbon instance from a given timestamp with second.
|
2022-04-01 18:19:46 +08:00
|
|
|
// 从给定的秒级时间戳创建 Carbon 实例
|
2021-07-23 10:56:39 +08:00
|
|
|
func CreateFromTimestamp(timestamp int64, timezone ...string) Carbon {
|
2022-04-12 23:22:14 +08:00
|
|
|
return NewCarbon().CreateFromTimestamp(timestamp, timezone...)
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
|
2022-04-12 23:22:14 +08:00
|
|
|
// CreateFromTimestampMilli creates a Carbon instance from a given timestamp with millisecond.
|
2022-06-09 07:40:28 +08:00
|
|
|
// 从给定的毫秒级时间戳创建 Carbon 实例
|
2022-04-12 23:22:14 +08:00
|
|
|
func (c Carbon) CreateFromTimestampMilli(timestamp int64, timezone ...string) Carbon {
|
2022-04-01 18:19:46 +08:00
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-04-01 18:19:46 +08:00
|
|
|
}
|
|
|
|
if c.Error != nil {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
c.time = time.Unix(timestamp/1e3, (timestamp%1e3)*1e6)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2022-04-12 23:22:14 +08:00
|
|
|
// CreateFromTimestampMilli creates a Carbon instance from a given timestamp with millisecond.
|
2022-06-09 07:40:28 +08:00
|
|
|
// 从给定的毫秒级时间戳创建 Carbon 实例
|
2022-04-12 17:31:20 +08:00
|
|
|
func CreateFromTimestampMilli(timestamp int64, timezone ...string) Carbon {
|
2022-04-12 23:22:14 +08:00
|
|
|
return NewCarbon().CreateFromTimestampMilli(timestamp, timezone...)
|
2022-04-01 18:19:46 +08:00
|
|
|
}
|
|
|
|
|
2022-04-12 23:22:14 +08:00
|
|
|
// CreateFromTimestampMicro creates a Carbon instance from a given timestamp with microsecond.
|
2022-04-01 18:19:46 +08:00
|
|
|
// 从给定的微秒级时间戳创建 Carbon 实例
|
2022-04-12 23:22:14 +08:00
|
|
|
func (c Carbon) CreateFromTimestampMicro(timestamp int64, timezone ...string) Carbon {
|
2021-07-31 13:27:17 +08:00
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-07-23 10:56:39 +08:00
|
|
|
}
|
|
|
|
if c.Error != nil {
|
|
|
|
return c
|
|
|
|
}
|
2022-04-01 18:19:46 +08:00
|
|
|
c.time = time.Unix(timestamp/1e6, (timestamp%1e6)*1e3)
|
2021-02-18 14:32:31 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2022-04-12 23:22:14 +08:00
|
|
|
// CreateFromTimestampMicro creates a Carbon instance from a given timestamp with microsecond.
|
2022-04-01 18:19:46 +08:00
|
|
|
// 从给定的微秒级时间戳创建 Carbon 实例
|
2022-04-12 17:31:20 +08:00
|
|
|
func CreateFromTimestampMicro(timestamp int64, timezone ...string) Carbon {
|
2022-04-12 23:22:14 +08:00
|
|
|
return NewCarbon().CreateFromTimestampMicro(timestamp, timezone...)
|
2022-04-01 18:19:46 +08:00
|
|
|
}
|
|
|
|
|
2022-04-12 23:22:14 +08:00
|
|
|
// CreateFromTimestampNano creates a Carbon instance from a given timestamp with nanosecond.
|
2022-04-01 18:19:46 +08:00
|
|
|
// 从给定的纳秒级时间戳创建 Carbon 实例
|
2022-04-12 23:22:14 +08:00
|
|
|
func (c Carbon) CreateFromTimestampNano(timestamp int64, timezone ...string) Carbon {
|
2022-04-01 18:19:46 +08:00
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-04-01 18:19:46 +08:00
|
|
|
}
|
|
|
|
if c.Error != nil {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
c.time = time.Unix(timestamp/1e9, timestamp%1e9)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2022-04-12 23:22:14 +08:00
|
|
|
// CreateFromTimestampNano creates a Carbon instance from a given timestamp with nanosecond.
|
2022-04-01 18:19:46 +08:00
|
|
|
// 从给定的纳秒级时间戳创建 Carbon 实例
|
2022-04-12 17:31:20 +08:00
|
|
|
func CreateFromTimestampNano(timestamp int64, timezone ...string) Carbon {
|
2022-04-12 23:22:14 +08:00
|
|
|
return NewCarbon().CreateFromTimestampNano(timestamp, timezone...)
|
2022-04-01 18:19:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateFromDateTime creates a Carbon instance from a given date and time.
|
2022-05-07 09:17:42 +08:00
|
|
|
// 从给定的年、月、日、时、分、秒创建 Carbon 实例
|
2022-04-01 18:19:46 +08:00
|
|
|
func (c Carbon) CreateFromDateTime(year, month, day, hour, minute, second int, timezone ...string) Carbon {
|
2023-12-23 22:21:25 +08:00
|
|
|
return c.create(year, month, day, hour, minute, second, 0, timezone...)
|
2022-04-01 18:19:46 +08:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:57:22 +08:00
|
|
|
// CreateFromDateTime creates a Carbon instance from a given date and time.
|
2022-05-07 09:17:42 +08:00
|
|
|
// 从给定的年、月、日、时、分、秒创建 Carbon 实例
|
2022-04-12 17:31:20 +08:00
|
|
|
func CreateFromDateTime(year, month, day, hour, minute, second int, timezone ...string) Carbon {
|
2021-07-23 10:56:39 +08:00
|
|
|
return NewCarbon().CreateFromDateTime(year, month, day, hour, minute, second, timezone...)
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
|
2022-05-07 09:17:42 +08:00
|
|
|
// CreateFromDateTimeMilli creates a Carbon instance from a given date, time and millisecond.
|
|
|
|
// 从给定的年、月、日、时、分、秒、毫秒创建 Carbon 实例
|
2022-04-12 23:22:14 +08:00
|
|
|
func (c Carbon) CreateFromDateTimeMilli(year, month, day, hour, minute, second, millisecond int, timezone ...string) Carbon {
|
|
|
|
return c.create(year, month, day, hour, minute, second, millisecond*1e6, timezone...)
|
|
|
|
}
|
|
|
|
|
2022-05-07 09:17:42 +08:00
|
|
|
// CreateFromDateTimeMilli creates a Carbon instance from a given date, time and millisecond.
|
|
|
|
// 从给定的年、月、日、时、分、秒、毫秒创建 Carbon 实例
|
2022-04-12 23:22:14 +08:00
|
|
|
func CreateFromDateTimeMilli(year, month, day, hour, minute, second, millisecond int, timezone ...string) Carbon {
|
|
|
|
return NewCarbon().CreateFromDateTimeMilli(year, month, day, hour, minute, second, millisecond, timezone...)
|
|
|
|
}
|
|
|
|
|
2022-05-07 09:17:42 +08:00
|
|
|
// CreateFromDateTimeMicro creates a Carbon instance from a given date, time and microsecond.
|
|
|
|
// 从给定的年、月、日、时、分、秒、微秒创建 Carbon 实例
|
2022-04-12 23:22:14 +08:00
|
|
|
func (c Carbon) CreateFromDateTimeMicro(year, month, day, hour, minute, second, microsecond int, timezone ...string) Carbon {
|
|
|
|
return c.create(year, month, day, hour, minute, second, microsecond*1e3, timezone...)
|
|
|
|
}
|
|
|
|
|
2022-05-07 09:17:42 +08:00
|
|
|
// CreateFromDateTimeMicro creates a Carbon instance from a given date, time and microsecond.
|
|
|
|
// 从给定的年、月、日、时、分、秒、微秒创建 Carbon 实例
|
2022-04-12 23:22:14 +08:00
|
|
|
func CreateFromDateTimeMicro(year, month, day, hour, minute, second, microsecond int, timezone ...string) Carbon {
|
|
|
|
return NewCarbon().CreateFromDateTimeMicro(year, month, day, hour, minute, second, microsecond, timezone...)
|
|
|
|
}
|
|
|
|
|
2022-05-07 09:17:42 +08:00
|
|
|
// CreateFromDateTimeNano creates a Carbon instance from a given date, time and nanosecond.
|
|
|
|
// 从给定的年、月、日、时、分、秒、纳秒创建 Carbon 实例
|
2022-04-12 23:22:14 +08:00
|
|
|
func (c Carbon) CreateFromDateTimeNano(year, month, day, hour, minute, second, nanosecond int, timezone ...string) Carbon {
|
|
|
|
return c.create(year, month, day, hour, minute, second, nanosecond, timezone...)
|
|
|
|
}
|
|
|
|
|
2022-05-07 09:17:42 +08:00
|
|
|
// CreateFromDateTimeNano creates a Carbon instance from a given date, time and nanosecond.
|
|
|
|
// 从给定的年、月、日、时、分、秒、纳秒创建 Carbon 实例
|
2022-04-12 23:22:14 +08:00
|
|
|
func CreateFromDateTimeNano(year, month, day, hour, minute, second, nanosecond int, timezone ...string) Carbon {
|
|
|
|
return NewCarbon().CreateFromDateTimeNano(year, month, day, hour, minute, second, nanosecond, timezone...)
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:57:22 +08:00
|
|
|
// CreateFromDate creates a Carbon instance from a given date.
|
2022-05-07 09:17:42 +08:00
|
|
|
// 从给定的年、月、日创建 Carbon 实例
|
2022-04-12 17:31:20 +08:00
|
|
|
func (c Carbon) CreateFromDate(year, month, day int, timezone ...string) Carbon {
|
2023-12-22 16:02:09 +08:00
|
|
|
return c.create(year, month, day, 0, 0, 0, 0, timezone...)
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:57:22 +08:00
|
|
|
// CreateFromDate creates a Carbon instance from a given date.
|
2022-05-07 09:17:42 +08:00
|
|
|
// 从给定的年、月、日创建 Carbon 实例
|
2022-04-12 17:31:20 +08:00
|
|
|
func CreateFromDate(year, month, day int, timezone ...string) Carbon {
|
2021-07-23 10:56:39 +08:00
|
|
|
return NewCarbon().CreateFromDate(year, month, day, timezone...)
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
|
2022-05-07 09:17:42 +08:00
|
|
|
// CreateFromDateMilli creates a Carbon instance from a given date and millisecond.
|
|
|
|
// 从给定的年、月、日、毫秒创建 Carbon 实例
|
2022-05-05 00:00:23 +08:00
|
|
|
func (c Carbon) CreateFromDateMilli(year, month, day, millisecond int, timezone ...string) Carbon {
|
2023-12-22 16:02:09 +08:00
|
|
|
return c.create(year, month, day, 0, 0, 0, millisecond*1e6, timezone...)
|
2022-05-05 00:00:23 +08:00
|
|
|
}
|
|
|
|
|
2022-05-07 09:17:42 +08:00
|
|
|
// CreateFromDateMilli creates a Carbon instance from a given date and millisecond.
|
|
|
|
// 从给定的年、月、日、毫秒创建 Carbon 实例
|
2022-05-05 00:00:23 +08:00
|
|
|
func CreateFromDateMilli(year, month, day, millisecond int, timezone ...string) Carbon {
|
|
|
|
return NewCarbon().CreateFromDateMilli(year, month, day, millisecond, timezone...)
|
|
|
|
}
|
|
|
|
|
2022-05-07 09:17:42 +08:00
|
|
|
// CreateFromDateMicro creates a Carbon instance from a given date and microsecond.
|
|
|
|
// 从给定的年、月、日、微秒创建 Carbon 实例
|
2022-05-05 00:00:23 +08:00
|
|
|
func (c Carbon) CreateFromDateMicro(year, month, day, microsecond int, timezone ...string) Carbon {
|
2023-12-22 16:02:09 +08:00
|
|
|
return c.create(year, month, day, 0, 0, 0, microsecond*1e3, timezone...)
|
2022-05-05 00:00:23 +08:00
|
|
|
}
|
|
|
|
|
2022-05-07 09:17:42 +08:00
|
|
|
// CreateFromDateMicro creates a Carbon instance from a given date and microsecond.
|
|
|
|
// 从给定的年、月、日、微秒创建 Carbon 实例
|
2022-05-05 00:00:23 +08:00
|
|
|
func CreateFromDateMicro(year, month, day, microsecond int, timezone ...string) Carbon {
|
|
|
|
return NewCarbon().CreateFromDateMicro(year, month, day, microsecond, timezone...)
|
|
|
|
}
|
|
|
|
|
2022-05-07 09:17:42 +08:00
|
|
|
// CreateFromDateNano creates a Carbon instance from a given date and nanosecond.
|
|
|
|
// 从给定的年、月、日、纳秒创建 Carbon 实例
|
2022-05-05 00:00:23 +08:00
|
|
|
func (c Carbon) CreateFromDateNano(year, month, day, nanosecond int, timezone ...string) Carbon {
|
2023-12-22 16:02:09 +08:00
|
|
|
return c.create(year, month, day, 0, 0, 0, nanosecond, timezone...)
|
2022-05-05 00:00:23 +08:00
|
|
|
}
|
|
|
|
|
2022-05-07 09:17:42 +08:00
|
|
|
// CreateFromDateNano creates a Carbon instance from a given date and nanosecond.
|
|
|
|
// 从给定的年、月、日、纳秒创建 Carbon 实例
|
2022-05-05 00:00:23 +08:00
|
|
|
func CreateFromDateNano(year, month, day, nanosecond int, timezone ...string) Carbon {
|
|
|
|
return NewCarbon().CreateFromDateNano(year, month, day, nanosecond, timezone...)
|
|
|
|
}
|
|
|
|
|
2023-12-22 16:02:09 +08:00
|
|
|
// CreateFromTime creates a Carbon instance from a given time(year, month and day are taken from the current time).
|
|
|
|
// 从给定的时、分、秒创建 Carbon 实例(年、月、日取自当前时间)
|
2022-04-12 17:31:20 +08:00
|
|
|
func (c Carbon) CreateFromTime(hour, minute, second int, timezone ...string) Carbon {
|
2023-12-23 22:01:00 +08:00
|
|
|
year, month, day := c.Now(timezone...).Date()
|
|
|
|
return c.create(year, month, day, hour, minute, second, 0, timezone...)
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
|
2023-12-22 16:02:09 +08:00
|
|
|
// CreateFromTime creates a Carbon instance from a given time(year, month and day are taken from the current time).
|
|
|
|
// 从给定的时、分、秒创建 Carbon 实例(年、月、日取自当前时间)
|
2022-04-12 17:31:20 +08:00
|
|
|
func CreateFromTime(hour, minute, second int, timezone ...string) Carbon {
|
2021-07-23 10:56:39 +08:00
|
|
|
return NewCarbon().CreateFromTime(hour, minute, second, timezone...)
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
2022-04-12 23:22:14 +08:00
|
|
|
|
2023-12-22 16:02:09 +08:00
|
|
|
// CreateFromTimeMilli creates a Carbon instance from a given time and millisecond(year, month and day are taken from the current time).
|
|
|
|
// 从给定的时、分、秒、毫秒创建 Carbon 实例(年、月、日取自当前时间)
|
2022-05-05 00:00:23 +08:00
|
|
|
func (c Carbon) CreateFromTimeMilli(hour, minute, second, millisecond int, timezone ...string) Carbon {
|
2023-12-23 22:01:00 +08:00
|
|
|
year, month, day := c.Now(timezone...).Date()
|
2022-05-05 00:00:23 +08:00
|
|
|
return c.create(year, month, day, hour, minute, second, millisecond*1e6, timezone...)
|
|
|
|
}
|
|
|
|
|
2023-12-22 16:02:09 +08:00
|
|
|
// CreateFromTimeMilli creates a Carbon instance from a given time and millisecond(year, month and day are taken from the current time).
|
|
|
|
// 从给定的时、分、秒、毫秒创建 Carbon 实例(年、月、日取自当前时间)
|
2022-05-05 00:00:23 +08:00
|
|
|
func CreateFromTimeMilli(hour, minute, second, millisecond int, timezone ...string) Carbon {
|
|
|
|
return NewCarbon().CreateFromTimeMilli(hour, minute, second, millisecond, timezone...)
|
|
|
|
}
|
|
|
|
|
2023-12-22 16:02:09 +08:00
|
|
|
// CreateFromTimeMicro creates a Carbon instance from a given time and microsecond(year, month and day are taken from the current time).
|
|
|
|
// 从给定的时、分、秒、微秒创建 Carbon 实例(年、月、日取自当前时间)
|
2022-05-05 00:00:23 +08:00
|
|
|
func (c Carbon) CreateFromTimeMicro(hour, minute, second, microsecond int, timezone ...string) Carbon {
|
2023-12-23 22:01:00 +08:00
|
|
|
year, month, day := c.Now(timezone...).Date()
|
2022-05-05 00:00:23 +08:00
|
|
|
return c.create(year, month, day, hour, minute, second, microsecond*1e3, timezone...)
|
|
|
|
}
|
|
|
|
|
2023-12-22 16:02:09 +08:00
|
|
|
// CreateFromTimeMicro creates a Carbon instance from a given time and microsecond(year, month and day are taken from the current time).
|
|
|
|
// 从给定的时、分、秒、微秒创建 Carbon 实例(年、月、日取自当前时间)
|
2022-05-05 00:00:23 +08:00
|
|
|
func CreateFromTimeMicro(hour, minute, second, microsecond int, timezone ...string) Carbon {
|
|
|
|
return NewCarbon().CreateFromTimeMicro(hour, minute, second, microsecond, timezone...)
|
|
|
|
}
|
|
|
|
|
2023-12-22 16:02:09 +08:00
|
|
|
// CreateFromTimeNano creates a Carbon instance from a given time and nanosecond(year, month and day are taken from the current time).
|
|
|
|
// 从给定的时、分、秒、纳秒创建 Carbon 实例(年、月、日取自当前时间)
|
2022-05-05 00:00:23 +08:00
|
|
|
func (c Carbon) CreateFromTimeNano(hour, minute, second, nanosecond int, timezone ...string) Carbon {
|
2023-12-23 22:01:00 +08:00
|
|
|
year, month, day := c.Now(timezone...).Date()
|
2022-05-05 00:00:23 +08:00
|
|
|
return c.create(year, month, day, hour, minute, second, nanosecond, timezone...)
|
|
|
|
}
|
|
|
|
|
2023-12-22 16:02:09 +08:00
|
|
|
// CreateFromTimeNano creates a Carbon instance from a given time and nanosecond(year, month and day are taken from the current time).
|
|
|
|
// 从给定的时、分、秒、纳秒创建 Carbon 实例(年、月、日取自当前时间)
|
2022-05-05 00:00:23 +08:00
|
|
|
func CreateFromTimeNano(hour, minute, second, nanosecond int, timezone ...string) Carbon {
|
|
|
|
return NewCarbon().CreateFromTimeNano(hour, minute, second, nanosecond, timezone...)
|
|
|
|
}
|
|
|
|
|
2022-05-07 09:17:42 +08:00
|
|
|
// creates a Carbon instance from a given date, time and nanosecond.
|
|
|
|
// 从给定的年、月、日、时、分、秒、纳秒创建 Carbon 实例
|
2022-04-12 23:22:14 +08:00
|
|
|
func (c Carbon) create(year, month, day, hour, minute, second, nanosecond int, timezone ...string) Carbon {
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-04-12 23:22:14 +08:00
|
|
|
}
|
|
|
|
if c.Error != nil {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
c.time = time.Date(year, time.Month(month), day, hour, minute, second, nanosecond, c.loc)
|
|
|
|
return c
|
|
|
|
}
|