mirror of
https://gitee.com/dromara/carbon.git
synced 2024-12-02 04:07:36 +08:00
2.1.6 新增CreateFromDateXXX() 和 CreateFromTimeXXX() 方法
This commit is contained in:
parent
115e599143
commit
dbb19f328b
84
creator.go
84
creator.go
@ -143,6 +143,48 @@ func CreateFromDate(year, month, day int, timezone ...string) Carbon {
|
||||
return NewCarbon().CreateFromDate(year, month, day, timezone...)
|
||||
}
|
||||
|
||||
// CreateFromDateMilli creates a Carbon instance from a given date with millisecond.
|
||||
// 从给定的年月日创建 Carbon 实例,包含毫秒
|
||||
func (c Carbon) CreateFromDateMilli(year, month, day, millisecond int, timezone ...string) Carbon {
|
||||
now := c.Now(timezone...)
|
||||
hour, minute, second := now.Time()
|
||||
return c.create(year, month, day, hour, minute, second, millisecond*1e6, timezone...)
|
||||
}
|
||||
|
||||
// CreateFromDateMilli creates a Carbon instance from a given date with millisecond.
|
||||
// 从给定的年月日创建 Carbon 实例,包含毫秒
|
||||
func CreateFromDateMilli(year, month, day, millisecond int, timezone ...string) Carbon {
|
||||
return NewCarbon().CreateFromDateMilli(year, month, day, millisecond, timezone...)
|
||||
}
|
||||
|
||||
// CreateFromDateMicro creates a Carbon instance from a given date with microsecond.
|
||||
// 从给定的年月日创建 Carbon 实例,包含微秒
|
||||
func (c Carbon) CreateFromDateMicro(year, month, day, microsecond int, timezone ...string) Carbon {
|
||||
now := c.Now(timezone...)
|
||||
hour, minute, second := now.Time()
|
||||
return c.create(year, month, day, hour, minute, second, microsecond*1e3, timezone...)
|
||||
}
|
||||
|
||||
// CreateFromDateMicro creates a Carbon instance from a given date with microsecond.
|
||||
// 从给定的年月日创建 Carbon 实例,包含微秒
|
||||
func CreateFromDateMicro(year, month, day, microsecond int, timezone ...string) Carbon {
|
||||
return NewCarbon().CreateFromDateMicro(year, month, day, microsecond, timezone...)
|
||||
}
|
||||
|
||||
// CreateFromDateNano creates a Carbon instance from a given date with nanosecond.
|
||||
// 从给定的年月日创建 Carbon 实例,包含纳秒
|
||||
func (c Carbon) CreateFromDateNano(year, month, day, nanosecond int, timezone ...string) Carbon {
|
||||
now := c.Now(timezone...)
|
||||
hour, minute, second := now.Time()
|
||||
return c.create(year, month, day, hour, minute, second, nanosecond, timezone...)
|
||||
}
|
||||
|
||||
// CreateFromDateNano creates a Carbon instance from a given date with nanosecond.
|
||||
// 从给定的年月日创建 Carbon 实例,包含纳秒
|
||||
func CreateFromDateNano(year, month, day, nanosecond int, timezone ...string) Carbon {
|
||||
return NewCarbon().CreateFromDateNano(year, month, day, nanosecond, timezone...)
|
||||
}
|
||||
|
||||
// CreateFromTime creates a Carbon instance from a given time.
|
||||
// 从给定的时分秒创建 Carbon 实例
|
||||
func (c Carbon) CreateFromTime(hour, minute, second int, timezone ...string) Carbon {
|
||||
@ -157,6 +199,48 @@ func CreateFromTime(hour, minute, second int, timezone ...string) Carbon {
|
||||
return NewCarbon().CreateFromTime(hour, minute, second, timezone...)
|
||||
}
|
||||
|
||||
// CreateFromTimeMilli creates a Carbon instance from a given time with millisecond.
|
||||
// 从给定的时分秒创建 Carbon 实例,包含毫秒
|
||||
func (c Carbon) CreateFromTimeMilli(hour, minute, second, millisecond int, timezone ...string) Carbon {
|
||||
now := c.Now(timezone...)
|
||||
year, month, day := now.Date()
|
||||
return c.create(year, month, day, hour, minute, second, millisecond*1e6, timezone...)
|
||||
}
|
||||
|
||||
// CreateFromTimeMilli creates a Carbon instance from a given time with millisecond.
|
||||
// 从给定的时分秒创建 Carbon 实例,包含毫秒
|
||||
func CreateFromTimeMilli(hour, minute, second, millisecond int, timezone ...string) Carbon {
|
||||
return NewCarbon().CreateFromTimeMilli(hour, minute, second, millisecond, timezone...)
|
||||
}
|
||||
|
||||
// CreateFromTimeMicro creates a Carbon instance from a given time with microsecond.
|
||||
// 从给定的时分秒创建 Carbon 实例,包含微秒
|
||||
func (c Carbon) CreateFromTimeMicro(hour, minute, second, microsecond int, timezone ...string) Carbon {
|
||||
now := c.Now(timezone...)
|
||||
year, month, day := now.Date()
|
||||
return c.create(year, month, day, hour, minute, second, microsecond*1e3, timezone...)
|
||||
}
|
||||
|
||||
// CreateFromTimeMicro creates a Carbon instance from a given time with microsecond.
|
||||
// 从给定的时分秒创建 Carbon 实例,包含微秒
|
||||
func CreateFromTimeMicro(hour, minute, second, microsecond int, timezone ...string) Carbon {
|
||||
return NewCarbon().CreateFromTimeMicro(hour, minute, second, microsecond, timezone...)
|
||||
}
|
||||
|
||||
// CreateFromTimeNano creates a Carbon instance from a given time with nanosecond.
|
||||
// 从给定的时分秒创建 Carbon 实例,包含纳秒
|
||||
func (c Carbon) CreateFromTimeNano(hour, minute, second, nanosecond int, timezone ...string) Carbon {
|
||||
now := c.Now(timezone...)
|
||||
year, month, day := now.Date()
|
||||
return c.create(year, month, day, hour, minute, second, nanosecond, timezone...)
|
||||
}
|
||||
|
||||
// CreateFromTimeNano creates a Carbon instance from a given time with nanosecond.
|
||||
// 从给定的时分秒创建 Carbon 实例,包含纳秒
|
||||
func CreateFromTimeNano(hour, minute, second, nanosecond int, timezone ...string) Carbon {
|
||||
return NewCarbon().CreateFromTimeNano(hour, minute, second, nanosecond, timezone...)
|
||||
}
|
||||
|
||||
// creates a Carbon instance from a given date and time with nanosecond.
|
||||
// 从给定的完整日期时间创建 Carbon 实例
|
||||
func (c Carbon) create(year, month, day, hour, minute, second, nanosecond int, timezone ...string) Carbon {
|
||||
|
165
creator_test.go
165
creator_test.go
@ -247,6 +247,90 @@ func TestCarbon_CreateFromDate(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_CreateFromDateMilli(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
clock := Now(PRC).ToTimeString()
|
||||
tests := []struct {
|
||||
year, month, day, millisecond int // 输入参数
|
||||
expected string // 期望值
|
||||
}{
|
||||
{2020, 1, 1, 999, "2020-01-01 " + clock + ".999"},
|
||||
{2020, 1, 31, 999, "2020-01-31 " + clock + ".999"},
|
||||
{2020, 2, 1, 999, "2020-02-01 " + clock + ".999"},
|
||||
{2020, 2, 28, 999, "2020-02-28 " + clock + ".999"},
|
||||
{2020, 2, 29, 999, "2020-02-29 " + clock + ".999"},
|
||||
}
|
||||
|
||||
for index, test := range tests {
|
||||
c := SetTimezone(PRC).CreateFromDateMilli(test.year, test.month, test.day, test.millisecond)
|
||||
assert.Nil(c.Error)
|
||||
assert.Equal(test.expected, c.ToDateTimeMilliString(), "Current test index is "+strconv.Itoa(index))
|
||||
}
|
||||
|
||||
for index, test := range tests {
|
||||
c := CreateFromDateMilli(test.year, test.month, test.day, test.millisecond, PRC)
|
||||
assert.Nil(c.Error)
|
||||
assert.Equal(test.expected, c.ToDateTimeMilliString(), "Current test index is "+strconv.Itoa(index))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_CreateFromDateMicro(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
clock := Now(PRC).ToTimeString()
|
||||
tests := []struct {
|
||||
year, month, day, microsecond int // 输入参数
|
||||
expected string // 期望值
|
||||
}{
|
||||
{2020, 1, 1, 999999, "2020-01-01 " + clock + ".999999"},
|
||||
{2020, 1, 31, 999999, "2020-01-31 " + clock + ".999999"},
|
||||
{2020, 2, 1, 999999, "2020-02-01 " + clock + ".999999"},
|
||||
{2020, 2, 28, 999999, "2020-02-28 " + clock + ".999999"},
|
||||
{2020, 2, 29, 999999, "2020-02-29 " + clock + ".999999"},
|
||||
}
|
||||
|
||||
for index, test := range tests {
|
||||
c := SetTimezone(PRC).CreateFromDateMicro(test.year, test.month, test.day, test.microsecond)
|
||||
assert.Nil(c.Error)
|
||||
assert.Equal(test.expected, c.ToDateTimeMicroString(), "Current test index is "+strconv.Itoa(index))
|
||||
}
|
||||
|
||||
for index, test := range tests {
|
||||
c := CreateFromDateMicro(test.year, test.month, test.day, test.microsecond, PRC)
|
||||
assert.Nil(c.Error)
|
||||
assert.Equal(test.expected, c.ToDateTimeMicroString(), "Current test index is "+strconv.Itoa(index))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_CreateFromDateNano(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
clock := Now(PRC).ToTimeString()
|
||||
tests := []struct {
|
||||
year, month, day, nanosecond int // 输入参数
|
||||
expected string // 期望值
|
||||
}{
|
||||
{2020, 1, 1, 999999999, "2020-01-01 " + clock + ".999999999"},
|
||||
{2020, 1, 31, 999999999, "2020-01-31 " + clock + ".999999999"},
|
||||
{2020, 2, 1, 999999999, "2020-02-01 " + clock + ".999999999"},
|
||||
{2020, 2, 28, 999999999, "2020-02-28 " + clock + ".999999999"},
|
||||
{2020, 2, 29, 999999999, "2020-02-29 " + clock + ".999999999"},
|
||||
}
|
||||
|
||||
for index, test := range tests {
|
||||
c := SetTimezone(PRC).CreateFromDateNano(test.year, test.month, test.day, test.nanosecond)
|
||||
assert.Nil(c.Error)
|
||||
assert.Equal(test.expected, c.ToDateTimeNanoString(), "Current test index is "+strconv.Itoa(index))
|
||||
}
|
||||
|
||||
for index, test := range tests {
|
||||
c := CreateFromDateNano(test.year, test.month, test.day, test.nanosecond, PRC)
|
||||
assert.Nil(c.Error)
|
||||
assert.Equal(test.expected, c.ToDateTimeNanoString(), "Current test index is "+strconv.Itoa(index))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_CreateFromTime(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
@ -274,6 +358,87 @@ func TestCarbon_CreateFromTime(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_CreateFromTimeMilli(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
date := Now(PRC).ToDateString()
|
||||
tests := []struct {
|
||||
hour, minute, second, millisecond int // 输入参数
|
||||
expected string // 期望值
|
||||
}{
|
||||
{0, 0, 0, 999, date + " 00:00:00.999"},
|
||||
{00, 00, 15, 999, date + " 00:00:15.999"},
|
||||
{00, 14, 15, 999, date + " 00:14:15.999"},
|
||||
{13, 14, 15, 999, date + " 13:14:15.999"},
|
||||
}
|
||||
|
||||
for index, test := range tests {
|
||||
c := SetTimezone(PRC).CreateFromTimeMilli(test.hour, test.minute, test.second, test.millisecond)
|
||||
assert.Nil(c.Error)
|
||||
assert.Equal(test.expected, c.ToDateTimeMilliString(), "Current test index is "+strconv.Itoa(index))
|
||||
}
|
||||
|
||||
for index, test := range tests {
|
||||
c := CreateFromTimeMilli(test.hour, test.minute, test.second, test.millisecond, PRC)
|
||||
assert.Nil(c.Error)
|
||||
assert.Equal(test.expected, c.ToDateTimeMilliString(), "Current test index is "+strconv.Itoa(index))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_CreateFromTimeMicro(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
date := Now(PRC).ToDateString()
|
||||
tests := []struct {
|
||||
hour, minute, second, microsecond int // 输入参数
|
||||
expected string // 期望值
|
||||
}{
|
||||
{0, 0, 0, 999999, date + " 00:00:00.999999"},
|
||||
{00, 00, 15, 999999, date + " 00:00:15.999999"},
|
||||
{00, 14, 15, 999999, date + " 00:14:15.999999"},
|
||||
{13, 14, 15, 999999, date + " 13:14:15.999999"},
|
||||
}
|
||||
|
||||
for index, test := range tests {
|
||||
c := SetTimezone(PRC).CreateFromTimeMicro(test.hour, test.minute, test.second, test.microsecond)
|
||||
assert.Nil(c.Error)
|
||||
assert.Equal(test.expected, c.ToDateTimeMicroString(), "Current test index is "+strconv.Itoa(index))
|
||||
}
|
||||
|
||||
for index, test := range tests {
|
||||
c := CreateFromTimeMicro(test.hour, test.minute, test.second, test.microsecond, PRC)
|
||||
assert.Nil(c.Error)
|
||||
assert.Equal(test.expected, c.ToDateTimeMicroString(), "Current test index is "+strconv.Itoa(index))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_CreateFromTimeNano(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
date := Now(PRC).ToDateString()
|
||||
tests := []struct {
|
||||
hour, minute, second, nanosecond int // 输入参数
|
||||
expected string // 期望值
|
||||
}{
|
||||
{0, 0, 0, 999999999, date + " 00:00:00.999999999"},
|
||||
{00, 00, 15, 999999999, date + " 00:00:15.999999999"},
|
||||
{00, 14, 15, 999999999, date + " 00:14:15.999999999"},
|
||||
{13, 14, 15, 999999999, date + " 13:14:15.999999999"},
|
||||
}
|
||||
|
||||
for index, test := range tests {
|
||||
c := SetTimezone(PRC).CreateFromTimeNano(test.hour, test.minute, test.second, test.nanosecond)
|
||||
assert.Nil(c.Error)
|
||||
assert.Equal(test.expected, c.ToDateTimeNanoString(), "Current test index is "+strconv.Itoa(index))
|
||||
}
|
||||
|
||||
for index, test := range tests {
|
||||
c := CreateFromTimeNano(test.hour, test.minute, test.second, test.nanosecond, PRC)
|
||||
assert.Nil(c.Error)
|
||||
assert.Equal(test.expected, c.ToDateTimeNanoString(), "Current test index is "+strconv.Itoa(index))
|
||||
}
|
||||
}
|
||||
|
||||
func TestError_Creator(t *testing.T) {
|
||||
year, month, day, hour, minute, second, timestamp, timezone := 2020, 8, 5, 13, 14, 15, int64(1577855655), "xxx"
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user