2021-02-18 14:32:31 +08:00
|
|
|
package carbon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
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 {
|
2021-08-16 09:29:07 +08:00
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
|
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-04-01 18:19:46 +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 {
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
|
|
|
|
}
|
|
|
|
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-04-01 18:19:46 +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 {
|
2021-08-16 09:29:07 +08:00
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
|
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 {
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
// 从给定的年月日时分秒创建 Carbon 实例
|
|
|
|
func (c Carbon) CreateFromDateTime(year, month, day, hour, minute, second int, timezone ...string) Carbon {
|
2022-04-14 11:32:01 +08:00
|
|
|
now := c.Now(timezone...)
|
|
|
|
return c.create(year, month, day, hour, minute, second, now.Nanosecond(), 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.
|
2021-07-28 15:18:05 +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-04-12 23:22:14 +08:00
|
|
|
// CreateFromDateTimeMilli creates a Carbon instance from a given date and time with millisecond.
|
|
|
|
// 从给定的年月日时分秒创建 Carbon 实例,包含毫秒
|
|
|
|
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...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateFromDateTimeMilli creates a Carbon instance from a given date and time with millisecond.
|
|
|
|
// 从给定的年月日时分秒创建 Carbon 实例,包含毫秒
|
|
|
|
func CreateFromDateTimeMilli(year, month, day, hour, minute, second, millisecond int, timezone ...string) Carbon {
|
|
|
|
return NewCarbon().CreateFromDateTimeMilli(year, month, day, hour, minute, second, millisecond, timezone...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateFromDateTimeMicro creates a Carbon instance from a given date and time with microsecond.
|
|
|
|
// 从给定的年月日时分秒创建 Carbon 实例,包含微秒
|
|
|
|
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...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateFromDateTimeMicro creates a Carbon instance from a given date and time with microsecond.
|
|
|
|
// 从给定的年月日时分秒创建 Carbon 实例,包含微秒
|
|
|
|
func CreateFromDateTimeMicro(year, month, day, hour, minute, second, microsecond int, timezone ...string) Carbon {
|
|
|
|
return NewCarbon().CreateFromDateTimeMicro(year, month, day, hour, minute, second, microsecond, timezone...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateFromDateTimeNano creates a Carbon instance from a given date and time with nanosecond.
|
|
|
|
// 从给定的年月日时分秒创建 Carbon 实例,包含纳秒
|
|
|
|
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...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateFromDateTimeNano creates a Carbon instance from a given date and time with nanosecond.
|
|
|
|
// 从给定的年月日时分秒创建 Carbon 实例,包含纳秒
|
|
|
|
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.
|
2021-07-28 15:18:05 +08:00
|
|
|
// 从给定的年月日创建 Carbon 实例
|
2022-04-12 17:31:20 +08:00
|
|
|
func (c Carbon) CreateFromDate(year, month, day int, timezone ...string) Carbon {
|
2022-04-13 01:44:54 +08:00
|
|
|
now := c.Now(timezone...)
|
2022-04-12 17:31:20 +08:00
|
|
|
hour, minute, second := now.Time()
|
2022-04-13 01:44:54 +08:00
|
|
|
return c.create(year, month, day, hour, minute, second, now.Nanosecond(), 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.
|
2021-07-28 15:18:05 +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
|
|
|
}
|
|
|
|
|
2021-08-10 10:57:22 +08:00
|
|
|
// CreateFromTime creates a Carbon instance from a given time.
|
2021-07-28 15:18:05 +08:00
|
|
|
// 从给定的时分秒创建 Carbon 实例
|
2022-04-12 17:31:20 +08:00
|
|
|
func (c Carbon) CreateFromTime(hour, minute, second int, timezone ...string) Carbon {
|
2022-04-13 01:44:54 +08:00
|
|
|
now := c.Now(timezone...)
|
2022-04-12 17:31:20 +08:00
|
|
|
year, month, day := now.Date()
|
2022-04-12 23:22:14 +08:00
|
|
|
return c.create(year, month, day, hour, minute, second, now.Nanosecond(), timezone...)
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:57:22 +08:00
|
|
|
// CreateFromTime creates a Carbon instance from a given time.
|
2021-07-28 15:18:05 +08:00
|
|
|
// 从给定的时分秒创建 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
|
|
|
|
2022-04-17 19:50:24 +08:00
|
|
|
// create a Carbon instance from a given time with nanosecond.
|
2022-04-12 23:22:14 +08:00
|
|
|
// 从给定的时间创建 Carbon 实例
|
|
|
|
func (c Carbon) create(year, month, day, hour, minute, second, nanosecond int, timezone ...string) Carbon {
|
|
|
|
if len(timezone) > 0 {
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
|
|
|
|
}
|
|
|
|
if c.Error != nil {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
c.time = time.Date(year, time.Month(month), day, hour, minute, second, nanosecond, c.loc)
|
|
|
|
return c
|
|
|
|
}
|