mirror of
https://gitee.com/dromara/carbon.git
synced 2024-11-29 18:57:37 +08:00
2.1.6 新增 SetDateXXX()、SetTimeXXX() 等系列方法
This commit is contained in:
parent
bce828fa5a
commit
b84f73dfb6
72
setter.go
72
setter.go
@ -60,7 +60,7 @@ func SetLanguage(lang *Language) Carbon {
|
||||
}
|
||||
|
||||
// SetDateTime sets year, month, day, hour, minute and second.
|
||||
// 设置年月日时分秒
|
||||
// 设置年、月、日、时、分、秒
|
||||
func (c Carbon) SetDateTime(year, month, day, hour, minute, second int) Carbon {
|
||||
if c.IsInvalid() {
|
||||
return c
|
||||
@ -69,7 +69,7 @@ func (c Carbon) SetDateTime(year, month, day, hour, minute, second int) Carbon {
|
||||
}
|
||||
|
||||
// SetDateTimeMilli sets year, month, day, hour, minute, second and millisecond.
|
||||
// 设置年月日时分秒毫秒
|
||||
// 设置年、月、日、时、分、秒、毫秒
|
||||
func (c Carbon) SetDateTimeMilli(year, month, day, hour, minute, second, millisecond int) Carbon {
|
||||
if c.IsInvalid() {
|
||||
return c
|
||||
@ -78,7 +78,7 @@ func (c Carbon) SetDateTimeMilli(year, month, day, hour, minute, second, millise
|
||||
}
|
||||
|
||||
// SetDateTimeMicro sets year, month, day, hour, minute, second and microsecond.
|
||||
// 设置年月日时分秒微秒
|
||||
// 设置年、月、日、时、分、秒、微秒
|
||||
func (c Carbon) SetDateTimeMicro(year, month, day, hour, minute, second, microsecond int) Carbon {
|
||||
if c.IsInvalid() {
|
||||
return c
|
||||
@ -87,7 +87,7 @@ func (c Carbon) SetDateTimeMicro(year, month, day, hour, minute, second, microse
|
||||
}
|
||||
|
||||
// SetDateTimeNano sets year, month, day, hour, minute, second and nanosecond.
|
||||
// 设置年月日时分秒纳秒
|
||||
// 设置年、月、日、时、分、秒、纳秒
|
||||
func (c Carbon) SetDateTimeNano(year, month, day, hour, minute, second, nanosecond int) Carbon {
|
||||
if c.IsInvalid() {
|
||||
return c
|
||||
@ -96,7 +96,7 @@ func (c Carbon) SetDateTimeNano(year, month, day, hour, minute, second, nanoseco
|
||||
}
|
||||
|
||||
// SetDate sets year, month and day.
|
||||
// 设置年月日
|
||||
// 设置年、月、日
|
||||
func (c Carbon) SetDate(year, month, day int) Carbon {
|
||||
if c.IsInvalid() {
|
||||
return c
|
||||
@ -105,8 +105,38 @@ func (c Carbon) SetDate(year, month, day int) Carbon {
|
||||
return c.create(year, month, day, hour, minute, second, c.Nanosecond())
|
||||
}
|
||||
|
||||
// SetDateMilli sets year, month, day and millisecond.
|
||||
// 设置年、月、日、毫秒
|
||||
func (c Carbon) SetDateMilli(year, month, day, millisecond int) Carbon {
|
||||
if c.IsInvalid() {
|
||||
return c
|
||||
}
|
||||
hour, minute, second := c.Time()
|
||||
return c.create(year, month, day, hour, minute, second, millisecond*1e6)
|
||||
}
|
||||
|
||||
// SetDateMicro sets year, month, day and microsecond.
|
||||
// 设置年、月、日、微秒
|
||||
func (c Carbon) SetDateMicro(year, month, day, microsecond int) Carbon {
|
||||
if c.IsInvalid() {
|
||||
return c
|
||||
}
|
||||
hour, minute, second := c.Time()
|
||||
return c.create(year, month, day, hour, minute, second, microsecond*1e3)
|
||||
}
|
||||
|
||||
// SetDateNano sets year, month, day and nanosecond.
|
||||
// 设置年、月、日、纳秒
|
||||
func (c Carbon) SetDateNano(year, month, day, nanosecond int) Carbon {
|
||||
if c.IsInvalid() {
|
||||
return c
|
||||
}
|
||||
hour, minute, second := c.Time()
|
||||
return c.create(year, month, day, hour, minute, second, nanosecond)
|
||||
}
|
||||
|
||||
// SetTime sets hour, minute and second.
|
||||
// 设置时分秒
|
||||
// 设置时、分、秒
|
||||
func (c Carbon) SetTime(hour, minute, second int) Carbon {
|
||||
if c.IsInvalid() {
|
||||
return c
|
||||
@ -115,6 +145,36 @@ func (c Carbon) SetTime(hour, minute, second int) Carbon {
|
||||
return c.create(year, month, day, hour, minute, second, c.Nanosecond())
|
||||
}
|
||||
|
||||
// SetTimeMilli sets hour, minute, second and millisecond.
|
||||
// 设置时、分、秒、毫秒
|
||||
func (c Carbon) SetTimeMilli(hour, minute, second, millisecond int) Carbon {
|
||||
if c.IsInvalid() {
|
||||
return c
|
||||
}
|
||||
year, month, day := c.Date()
|
||||
return c.create(year, month, day, hour, minute, second, millisecond*1e6)
|
||||
}
|
||||
|
||||
// SetTimeMicro sets hour, minute, second and microsecond.
|
||||
// 设置时、分、秒、微秒
|
||||
func (c Carbon) SetTimeMicro(hour, minute, second, microsecond int) Carbon {
|
||||
if c.IsInvalid() {
|
||||
return c
|
||||
}
|
||||
year, month, day := c.Date()
|
||||
return c.create(year, month, day, hour, minute, second, microsecond*1e3)
|
||||
}
|
||||
|
||||
// SetTimeNano sets hour, minute, second and nanosecond.
|
||||
// 设置、时、分、秒、纳秒
|
||||
func (c Carbon) SetTimeNano(hour, minute, second, nanosecond int) Carbon {
|
||||
if c.IsInvalid() {
|
||||
return c
|
||||
}
|
||||
year, month, day := c.Date()
|
||||
return c.create(year, month, day, hour, minute, second, nanosecond)
|
||||
}
|
||||
|
||||
// SetYear sets year.
|
||||
// 设置年份
|
||||
func (c Carbon) SetYear(year int) Carbon {
|
||||
|
134
setter_test.go
134
setter_test.go
@ -182,6 +182,63 @@ func TestCarbon_SetDate(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_SetDateMilli(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
tests := []struct {
|
||||
input string // 输入值
|
||||
year, month, day, millisecond int // 输入参数
|
||||
expected string // 期望值
|
||||
}{
|
||||
{"2020-01-01", 2019, 02, 02, 999, "2019-02-02 00:00:00.999"},
|
||||
{"2020-01-01", 2019, 02, 31, 999, "2019-03-03 00:00:00.999"},
|
||||
}
|
||||
|
||||
for index, test := range tests {
|
||||
c := Parse(test.input).SetDateMilli(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))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_SetDateMicro(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
tests := []struct {
|
||||
input string // 输入值
|
||||
year, month, day, microsecond int // 输入参数
|
||||
expected string // 期望值
|
||||
}{
|
||||
{"2020-01-01", 2019, 02, 02, 999999, "2019-02-02 00:00:00.999999"},
|
||||
{"2020-01-01", 2019, 02, 31, 999999, "2019-03-03 00:00:00.999999"},
|
||||
}
|
||||
|
||||
for index, test := range tests {
|
||||
c := Parse(test.input).SetDateMicro(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))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_SetDateNano(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
tests := []struct {
|
||||
input string // 输入值
|
||||
year, month, day, nanosecond int // 输入参数
|
||||
expected string // 期望值
|
||||
}{
|
||||
{"2020-01-01", 2019, 02, 02, 999999999, "2019-02-02 00:00:00.999999999"},
|
||||
{"2020-01-01", 2019, 02, 31, 999999999, "2019-03-03 00:00:00.999999999"},
|
||||
}
|
||||
|
||||
for index, test := range tests {
|
||||
c := Parse(test.input).SetDateNano(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))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_SetTime(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
@ -201,6 +258,63 @@ func TestCarbon_SetTime(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_SetTimeMilli(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
tests := []struct {
|
||||
input string // 输入值
|
||||
hour, minute, second, millisecond int // 输入参数
|
||||
expected string // 期望值
|
||||
}{
|
||||
{"2020-08-05", 13, 14, 15, 999, "2020-08-05 13:14:15.999"},
|
||||
{"2020-08-05", 13, 14, 90, 999, "2020-08-05 13:15:30.999"},
|
||||
}
|
||||
|
||||
for index, test := range tests {
|
||||
c := Parse(test.input).SetTimeMilli(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))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_SetTimeMicro(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
tests := []struct {
|
||||
input string // 输入值
|
||||
hour, minute, second, microsecond int // 输入参数
|
||||
expected string // 期望值
|
||||
}{
|
||||
{"2020-08-05", 13, 14, 15, 999999, "2020-08-05 13:14:15.999999"},
|
||||
{"2020-08-05", 13, 14, 90, 999999, "2020-08-05 13:15:30.999999"},
|
||||
}
|
||||
|
||||
for index, test := range tests {
|
||||
c := Parse(test.input).SetTimeMicro(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))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_SetTimeNano(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
tests := []struct {
|
||||
input string // 输入值
|
||||
hour, minute, second, nanosecond int // 输入参数
|
||||
expected string // 期望值
|
||||
}{
|
||||
{"2020-08-05", 13, 14, 15, 999999999, "2020-08-05 13:14:15.999999999"},
|
||||
{"2020-08-05", 13, 14, 90, 999999999, "2020-08-05 13:15:30.999999999"},
|
||||
}
|
||||
|
||||
for index, test := range tests {
|
||||
c := Parse(test.input).SetTimeNano(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))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_SetYear(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
@ -491,17 +605,21 @@ func TestError_Setter(t *testing.T) {
|
||||
assert.NotNil(t, lang1.Error, "It should catch an exception in SetLanguage()")
|
||||
assert.NotNil(t, lang2.SetLanguage(lang).Error, "It should catch an exception in SetLanguage()")
|
||||
|
||||
dt1 := c.SetDateTime(year, month, day, hour, minute, second)
|
||||
assert.NotNil(t, dt1.Error, "It should catch an exception in SetDateTime()")
|
||||
dt2 := c.SetDateTimeMilli(year, month, day, hour, minute, second, millisecond)
|
||||
assert.NotNil(t, dt2.Error, "It should catch an exception in SetDateTimeMilli()")
|
||||
dt3 := c.SetDateTimeMicro(year, month, day, hour, minute, second, microsecond)
|
||||
assert.NotNil(t, dt3.Error, "It should catch an exception in SetDateTimeMicro()")
|
||||
dt4 := c.SetDateTimeNano(year, month, day, hour, minute, second, nanosecond)
|
||||
assert.NotNil(t, dt4.Error, "It should catch an exception in SetDateTimeNano()")
|
||||
assert.NotNil(t, c.SetDateTime(year, month, day, hour, minute, second).Error, "It should catch an exception in SetDateTime()")
|
||||
assert.NotNil(t, c.SetDateTimeMilli(year, month, day, hour, minute, second, millisecond).Error, "It should catch an exception in SetDateTimeMilli()")
|
||||
assert.NotNil(t, c.SetDateTimeMicro(year, month, day, hour, minute, second, microsecond).Error, "It should catch an exception in SetDateTimeMicro()")
|
||||
assert.NotNil(t, c.SetDateTimeNano(year, month, day, hour, minute, second, nanosecond).Error, "It should catch an exception in SetDateTimeNano()")
|
||||
|
||||
assert.NotNil(t, c.SetDate(year, month, day).Error, "It should catch an exception in SeDate()")
|
||||
assert.NotNil(t, c.SetDateMilli(year, month, day, millisecond).Error, "It should catch an exception in SetDateMilli()")
|
||||
assert.NotNil(t, c.SetDateMicro(year, month, day, microsecond).Error, "It should catch an exception in SetDateMicro()")
|
||||
assert.NotNil(t, c.SetDateNano(year, month, day, nanosecond).Error, "It should catch an exception in SetDateNano()")
|
||||
|
||||
assert.NotNil(t, c.SetTime(hour, minute, second).Error, "It should catch an exception in SetTime()")
|
||||
assert.NotNil(t, c.SetTimeMilli(hour, minute, second, millisecond).Error, "It should catch an exception in SetTimeMilli()")
|
||||
assert.NotNil(t, c.SetTimeMicro(hour, minute, second, microsecond).Error, "It should catch an exception in SetTimeMicro()")
|
||||
assert.NotNil(t, c.SetTimeNano(hour, minute, second, nanosecond).Error, "It should catch an exception in SetTimeNano()")
|
||||
|
||||
assert.NotNil(t, c.SetYear(year).Error, "It should catch an exception in SetYear()")
|
||||
assert.NotNil(t, c.SetYearNoOverflow(year).Error, "It should catch an exception in SetYearNoOverflow()")
|
||||
assert.NotNil(t, c.SetMonth(month).Error, "It should catch an exception in SetMonth()")
|
||||
|
Loading…
Reference in New Issue
Block a user