carbon/creator.go

172 lines
7.3 KiB
Go
Raw Normal View History

2021-02-18 14:32:31 +08:00
package carbon
import (
"time"
)
// CreateFromTimestamp creates a Carbon instance from a given timestamp with second.
2022-04-01 18:19:46 +08:00
// 从给定的秒级时间戳创建 Carbon 实例
func (c Carbon) CreateFromTimestamp(timestamp int64, timezone ...string) Carbon {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.Error != nil {
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
}
// CreateFromTimestamp creates a Carbon instance from a given timestamp with second.
2022-04-01 18:19:46 +08:00
// 从给定的秒级时间戳创建 Carbon 实例
func CreateFromTimestamp(timestamp int64, timezone ...string) Carbon {
return NewCarbon().CreateFromTimestamp(timestamp, timezone...)
2021-02-18 14:32:31 +08:00
}
// CreateFromTimestampMilli creates a Carbon instance from a given timestamp with millisecond.
2022-04-01 18:19:46 +08:00
// 从给定的微秒级时间戳创建 Carbon 实例
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
}
// CreateFromTimestampMilli creates a Carbon instance from a given timestamp with millisecond.
2022-04-01 18:19:46 +08:00
// 从给定的微秒级时间戳创建 Carbon 实例
func CreateFromTimestampMilli(timestamp int64, timezone ...string) Carbon {
return NewCarbon().CreateFromTimestampMilli(timestamp, timezone...)
2022-04-01 18:19:46 +08:00
}
// CreateFromTimestampMicro creates a Carbon instance from a given timestamp with microsecond.
2022-04-01 18:19:46 +08:00
// 从给定的微秒级时间戳创建 Carbon 实例
func (c Carbon) CreateFromTimestampMicro(timestamp int64, timezone ...string) Carbon {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
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
}
// CreateFromTimestampMicro creates a Carbon instance from a given timestamp with microsecond.
2022-04-01 18:19:46 +08:00
// 从给定的微秒级时间戳创建 Carbon 实例
func CreateFromTimestampMicro(timestamp int64, timezone ...string) Carbon {
return NewCarbon().CreateFromTimestampMicro(timestamp, timezone...)
2022-04-01 18:19:46 +08:00
}
// CreateFromTimestampNano creates a Carbon instance from a given timestamp with nanosecond.
2022-04-01 18:19:46 +08:00
// 从给定的纳秒级时间戳创建 Carbon 实例
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
}
// CreateFromTimestampNano creates a Carbon instance from a given timestamp with nanosecond.
2022-04-01 18:19:46 +08:00
// 从给定的纳秒级时间戳创建 Carbon 实例
func CreateFromTimestampNano(timestamp int64, timezone ...string) Carbon {
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 实例
func CreateFromDateTime(year, month, day, hour, minute, second int, timezone ...string) Carbon {
return NewCarbon().CreateFromDateTime(year, month, day, hour, minute, second, timezone...)
2021-02-18 14:32:31 +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 实例
func (c Carbon) CreateFromDate(year, month, day int, timezone ...string) Carbon {
2022-04-13 01:44:54 +08:00
now := c.Now(timezone...)
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 实例
func CreateFromDate(year, month, day int, timezone ...string) Carbon {
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 实例
func (c Carbon) CreateFromTime(hour, minute, second int, timezone ...string) Carbon {
2022-04-13 01:44:54 +08:00
now := c.Now(timezone...)
year, month, day := now.Date()
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 实例
func CreateFromTime(hour, minute, second int, timezone ...string) Carbon {
return NewCarbon().CreateFromTime(hour, minute, second, timezone...)
2021-02-18 14:32:31 +08:00
}
2022-04-17 19:50:24 +08:00
// create a Carbon instance from a given time with nanosecond.
// 从给定的时间创建 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
}