mirror of
https://gitee.com/dromara/carbon.git
synced 2024-11-29 18:57:37 +08:00
Duration()拆分成AddDuration()和SubDuration()
This commit is contained in:
parent
84c388e120
commit
5e73299843
14
README.en.md
14
README.en.md
@ -263,39 +263,39 @@ carbon.Parse("2020-08-05 13:14:15").SubDay().ToDateTimeString() // 2020-08-04 13
|
||||
// Add three hours
|
||||
carbon.Parse("2020-08-05 13:14:15").AddHours(3).ToDateTimeString() // 2020-08-05 16:14:15
|
||||
// Add two and a half hours
|
||||
carbon.Parse("2020-08-05 13:14:15").Duration("2.5h").ToDateTimeString() // 2020-08-05 15:44:15
|
||||
carbon.Parse("2020-08-05 13:14:15").AddDuration("2.5h").ToDateTimeString() // 2020-08-05 15:44:15
|
||||
// Add one hour
|
||||
carbon.Parse("2020-08-05 13:14:15").AddHour().ToDateTimeString() // 2020-08-05 14:14:15
|
||||
// Subtract three hours
|
||||
carbon.Parse("2020-08-05 13:14:15").SubHours(3).ToDateTimeString() // 2020-08-05 10:14:15
|
||||
// Subtract two and a half hours
|
||||
carbon.Parse("2020-08-05 13:14:15").Duration("-2.5h").ToDateTimeString() // 2020-08-05 10:44:15
|
||||
carbon.Parse("2020-08-05 13:14:15").SubDuration("2.5h").ToDateTimeString() // 2020-08-05 10:44:15
|
||||
// Subtract one hour
|
||||
carbon.Parse("2020-08-05 13:14:15").SubHour().ToDateTimeString() // 2020-08-05 12:14:15
|
||||
|
||||
// Add three minutes
|
||||
carbon.Parse("2020-08-05 13:14:15").AddMinutes(3).ToDateTimeString() // 2020-08-05 13:17:15
|
||||
// Add two and a half minutes
|
||||
carbon.Parse("2020-08-05 13:14:15").Duration("2.5m").ToDateTimeString() // 2020-08-05 13:16:45
|
||||
carbon.Parse("2020-08-05 13:14:15").AddDuration("2.5m").ToDateTimeString() // 2020-08-05 13:16:45
|
||||
// Add one minute
|
||||
carbon.Parse("2020-08-05 13:14:15").AddMinute().ToDateTimeString() // 2020-08-05 13:15:15
|
||||
// Subtract three minutes
|
||||
carbon.Parse("2020-08-05 13:14:15").SubMinutes(3).ToDateTimeString() // 2020-08-05 13:11:15
|
||||
// Subtract two and a half minutes
|
||||
carbon.Parse("2020-08-05 13:14:15").Duration("-2.5m").ToDateTimeString() // 2020-08-05 13:11:45
|
||||
carbon.Parse("2020-08-05 13:14:15").SubDuration("2.5m").ToDateTimeString() // 2020-08-05 13:11:45
|
||||
// Subtract one minute
|
||||
carbon.Parse("2020-08-05 13:14:15").SubMinute().ToDateTimeString() // 2020-08-05 13:13:15
|
||||
|
||||
// Add three seconds
|
||||
carbon.Parse("2020-08-05 13:14:15").AddSeconds(3).ToDateTimeString() // 2020-08-05 13:14:18
|
||||
// Add two and a half seconds
|
||||
carbon.Parse("2020-08-05 13:14:15").Duration("2.5s").ToDateTimeString() // 2020-08-05 13:14:17
|
||||
carbon.Parse("2020-08-05 13:14:15").AddDuration("2.5s").ToDateTimeString() // 2020-08-05 13:14:17
|
||||
// Add one second
|
||||
carbon.Parse("2020-08-05 13:14:15").AddSecond().ToDateTimeString() // 2020-08-05 13:14:16
|
||||
// Subtract three seconds
|
||||
carbon.Parse("2020-08-05 13:14:15").SubSeconds(3).ToDateTimeString() // 2020-08-05 13:14:12
|
||||
// Subtract two and a half seconds
|
||||
carbon.Parse("2020-08-05 13:14:15").Duration("-2.5s").ToDateTimeString() // 2020-08-05 13:14:12
|
||||
carbon.Parse("2020-08-05 13:14:15").SubDuration("2.5s").ToDateTimeString() // 2020-08-05 13:14:12
|
||||
// Subtract one second
|
||||
carbon.Parse("2020-08-05 13:14:15").SubSecond().ToDateTimeString() // 2020-08-05 13:14:14
|
||||
```
|
||||
@ -621,7 +621,7 @@ type UserModel struct {
|
||||
|
||||
// Instantiate model
|
||||
user := UserModel {
|
||||
Birthday: Birthday: ToRssString{carbon.Now()},
|
||||
Birthday: ToRssString{carbon.Now()},
|
||||
}
|
||||
|
||||
// Overload MarshalJSON method
|
||||
|
16
README.md
16
README.md
@ -260,42 +260,42 @@ carbon.Parse("2020-08-05 13:14:15").SubDay().ToDateTimeString() // 2020-08-04 13
|
||||
// 三小时后
|
||||
carbon.Parse("2020-08-05 13:14:15").AddHours(3).ToDateTimeString() // 2020-08-05 16:14:15
|
||||
// 二小时半后
|
||||
carbon.Parse("2020-08-05 13:14:15").Duration("2.5h").ToDateTimeString() // 2020-08-05 15:44:15
|
||||
carbon.Parse("2020-08-05 13:14:15").AddDuration("2.5h").ToDateTimeString() // 2020-08-05 15:44:15
|
||||
// 一小时后
|
||||
carbon.Parse("2020-08-05 13:14:15").AddHour().ToDateTimeString() // 2020-08-05 14:14:15
|
||||
|
||||
// 三小时前
|
||||
carbon.Parse("2020-08-05 13:14:15").SubHours(3).ToDateTimeString() // 2020-08-05 10:14:15
|
||||
// 二小时半前
|
||||
carbon.Parse("2020-08-05 13:14:15").Duration("-2.5h").ToDateTimeString() // 2020-08-05 10:44:15
|
||||
carbon.Parse("2020-08-05 13:14:15").SubDuration("2.5h").ToDateTimeString() // 2020-08-05 10:44:15
|
||||
// 一小时前
|
||||
carbon.Parse("2020-08-05 13:14:15").SubHour().ToDateTimeString() // 2020-08-05 12:14:15
|
||||
|
||||
// 三分钟后
|
||||
carbon.Parse("2020-08-05 13:14:15").AddMinutes(3).ToDateTimeString() // 2020-08-05 13:17:15
|
||||
// 二分钟半后
|
||||
carbon.Parse("2020-08-05 13:14:15").Duration("2.5m").ToDateTimeString() // 2020-08-05 13:16:45
|
||||
carbon.Parse("2020-08-05 13:14:15").AddDuration("2.5m").ToDateTimeString() // 2020-08-05 13:16:45
|
||||
// 一分钟后
|
||||
carbon.Parse("2020-08-05 13:14:15").AddMinute().ToDateTimeString() // 2020-08-05 13:15:15
|
||||
|
||||
// 三分钟前
|
||||
carbon.Parse("2020-08-05 13:14:15").SubMinutes(3).ToDateTimeString() // 2020-08-05 13:11:15
|
||||
// 二分钟半前
|
||||
carbon.Parse("2020-08-05 13:14:15").Duration("-2.5m").ToDateTimeString() // 2020-08-05 13:11:45
|
||||
carbon.Parse("2020-08-05 13:14:15").SubDuration("2.5m").ToDateTimeString() // 2020-08-05 13:11:45
|
||||
// 一分钟前
|
||||
carbon.Parse("2020-08-05 13:14:15").SubMinute().ToDateTimeString() // 2020-08-05 13:13:15
|
||||
|
||||
// 三秒钟后
|
||||
carbon.Parse("2020-08-05 13:14:15").AddSeconds(3).ToDateTimeString() // 2020-08-05 13:14:18
|
||||
// 二秒钟半后
|
||||
carbon.Parse("2020-08-05 13:14:15").Duration("2.5s").ToDateTimeString() // 2020-08-05 13:14:17
|
||||
carbon.Parse("2020-08-05 13:14:15").AddDuration("2.5s").ToDateTimeString() // 2020-08-05 13:14:17
|
||||
// 一秒钟后
|
||||
carbon.Parse("2020-08-05 13:14:15").AddSecond().ToDateTimeString() // 2020-08-05 13:14:16
|
||||
|
||||
// 三秒钟前
|
||||
carbon.Parse("2020-08-05 13:14:15").SubSeconds(3).ToDateTimeString() // 2020-08-05 13:14:12
|
||||
// 二秒钟半前
|
||||
carbon.Parse("2020-08-05 13:14:15").Duration("-2.5s").ToDateTimeString() // 2020-08-05 13:14:12
|
||||
carbon.Parse("2020-08-05 13:14:15").SubDuration("2.5s").ToDateTimeString() // 2020-08-05 13:14:12
|
||||
// 一秒钟前
|
||||
carbon.Parse("2020-08-05 13:14:15").SubSecond().ToDateTimeString() // 2020-08-05 13:14:14
|
||||
```
|
||||
@ -415,7 +415,7 @@ carbon.Parse("2020-08-05 13:14:15").DayOfWeek() // 3
|
||||
|
||||
// 获取当前年
|
||||
carbon.Parse("2020-08-05 13:14:15").Year() // 2020
|
||||
// 获取当前季节
|
||||
// 获取当前季度
|
||||
carbon.Parse("2020-08-05 13:14:15").Quarter() // 3
|
||||
// 获取当前月
|
||||
carbon.Parse("2020-08-05 13:14:15").Month() // 8
|
||||
@ -625,7 +625,7 @@ type UserModel struct {
|
||||
|
||||
// 实例化模型
|
||||
user := UserModel {
|
||||
Birthday: Birthday: ToRssString{carbon.Now()},
|
||||
Birthday: ToRssString{carbon.Now()},
|
||||
}
|
||||
|
||||
// 重写MarshalJSON方法
|
||||
|
13
carbon.go
13
carbon.go
@ -161,13 +161,20 @@ func (c Carbon) ParseByDuration(duration string) Carbon {
|
||||
return newCarbon(ParseByDuration(duration).Time.In(c.loc))
|
||||
}
|
||||
|
||||
// Duration 按照持续时间字符串改变时间(指定时区)
|
||||
// 支持正负整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合
|
||||
func (c Carbon) Duration(duration string) Carbon {
|
||||
// AddDurations 按照持续时间字符串增加时间
|
||||
// 支持整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合
|
||||
func (c Carbon) AddDuration(duration string) Carbon {
|
||||
c.Time = c.Time.Add(parseByDuration(duration))
|
||||
return c
|
||||
}
|
||||
|
||||
// SubDurations 按照持续时间字符串减少时间
|
||||
// 支持整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合
|
||||
func (c Carbon) SubDuration(duration string) Carbon {
|
||||
c.Time = c.Time.Add(parseByDuration("-" + duration))
|
||||
return c
|
||||
}
|
||||
|
||||
// AddYears N年后
|
||||
func (c Carbon) AddYears(years int) Carbon {
|
||||
c.Time = c.Time.AddDate(years, 0, 0)
|
||||
|
@ -754,38 +754,32 @@ func TestCarbon_ParseByDuration2(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_Duration(t *testing.T) {
|
||||
func TestCarbon_AddDuration(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input string // 输入值
|
||||
duration string // 输入参数
|
||||
output string // 期望输出值
|
||||
}{
|
||||
{"2020-01-01 13:14:15", "10h", "2020-01-01 23:14:15"},
|
||||
{"2020-01-01 13:14:15", "-10h", "2020-01-01 03:14:15"},
|
||||
{"2020-01-01 13:14:15", "10.5h", "2020-01-01 23:44:15"},
|
||||
{"2020-01-01 13:14:15", "-10.5h", "2020-01-01 02:44:15"},
|
||||
|
||||
{"2020-01-01 13:14:15", "10m", "2020-01-01 13:24:15"},
|
||||
{"2020-01-01 13:14:15", "-10m", "2020-01-01 13:04:15"},
|
||||
{"2020-01-01 13:14:15", "10.5m", "2020-01-01 13:24:45"},
|
||||
{"2020-01-01 13:14:15", "-10.5m", "2020-01-01 13:03:45"},
|
||||
|
||||
{"2020-01-01 13:14:15", "10s", "2020-01-01 13:14:25"},
|
||||
{"2020-01-01 13:14:15", "-10s", "2020-01-01 13:14:05"},
|
||||
{"2020-01-01 13:14:15", "10.5s", "2020-01-01 13:14:25"},
|
||||
{"2020-01-01 13:14:15", "-10.5s", "2020-01-01 13:14:04"},
|
||||
|
||||
{"2020-01-01 13:14:15", "-10x", ""},
|
||||
{"2020-01-01 13:14:15", "10x", ""},
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
fmt.Printf("catch an exception in Duration():%s\n", r)
|
||||
fmt.Printf("catch an exception in AddDuration():%s\n", r)
|
||||
}
|
||||
}()
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input).Duration(v.duration).ToDateTimeString()
|
||||
output := Parse(v.input).AddDuration(v.duration).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -793,7 +787,48 @@ func TestCarbon_Duration(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := SetTimezone(PRC).Parse(v.input).Duration(v.duration).ToDateTimeString()
|
||||
output := SetTimezone(PRC).Parse(v.input).AddDuration(v.duration).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_SubDuration(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input string // 输入值
|
||||
duration string // 输入参数
|
||||
output string // 期望输出值
|
||||
}{
|
||||
{"2020-01-01 13:14:15", "10h", "2020-01-01 03:14:15"},
|
||||
{"2020-01-01 13:14:15", "10.5h", "2020-01-01 02:44:15"},
|
||||
|
||||
{"2020-01-01 13:14:15", "10m", "2020-01-01 13:04:15"},
|
||||
{"2020-01-01 13:14:15", "10.5m", "2020-01-01 13:03:45"},
|
||||
|
||||
{"2020-01-01 13:14:15", "10s", "2020-01-01 13:14:05"},
|
||||
{"2020-01-01 13:14:15", "10.5s", "2020-01-01 13:14:04"},
|
||||
|
||||
{"2020-01-01 13:14:15", "10x", ""},
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
fmt.Printf("catch an exception in SubDuration():%s\n", r)
|
||||
}
|
||||
}()
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input).SubDuration(v.duration).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
}
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := SetTimezone(PRC).Parse(v.input).SubDuration(v.duration).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
|
@ -58,7 +58,7 @@
|
||||
|
||||
<option value="file1">github.com/golang-module/carbon/carbon.go (100.0%)</option>
|
||||
|
||||
<option value="file2">github.com/golang-module/carbon/database.go (33.3%)</option>
|
||||
<option value="file2">github.com/golang-module/carbon/database.go (26.3%)</option>
|
||||
|
||||
<option value="file3">github.com/golang-module/carbon/final.go (100.0%)</option>
|
||||
|
||||
@ -379,13 +379,20 @@ func (c Carbon) ParseByDuration(duration string) Carbon <span class="cov8" title
|
||||
return newCarbon(ParseByDuration(duration).Time.In(c.loc))
|
||||
}</span>
|
||||
|
||||
// Duration 按照持续时间字符串改变时间(指定时区)
|
||||
// 支持正负整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合
|
||||
func (c Carbon) Duration(duration string) Carbon <span class="cov8" title="1">{
|
||||
// AddDurations 按照持续时间字符串增加时间
|
||||
// 支持整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合
|
||||
func (c Carbon) AddDuration(duration string) Carbon <span class="cov8" title="1">{
|
||||
c.Time = c.Time.Add(parseByDuration(duration))
|
||||
return c
|
||||
}</span>
|
||||
|
||||
// SubDurations 按照持续时间字符串减少时间
|
||||
// 支持整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合
|
||||
func (c Carbon) SubDuration(duration string) Carbon <span class="cov8" title="1">{
|
||||
c.Time = c.Time.Add(parseByDuration("-" + duration))
|
||||
return c
|
||||
}</span>
|
||||
|
||||
// AddYears N年后
|
||||
func (c Carbon) AddYears(years int) Carbon <span class="cov8" title="1">{
|
||||
c.Time = c.Time.AddDate(years, 0, 0)
|
||||
@ -805,6 +812,22 @@ type ToTimestamp struct {
|
||||
Carbon
|
||||
}
|
||||
|
||||
type ToTimestampWithSecond struct {
|
||||
Carbon
|
||||
}
|
||||
|
||||
type ToTimestampWithMillisecond struct {
|
||||
Carbon
|
||||
}
|
||||
|
||||
type ToTimestampWithMicrosecond struct {
|
||||
Carbon
|
||||
}
|
||||
|
||||
type ToTimestampWithNanosecond struct {
|
||||
Carbon
|
||||
}
|
||||
|
||||
func (c *Carbon) Scan(v interface{}) error <span class="cov0" title="0">{
|
||||
value, ok := v.(time.Time)
|
||||
if ok </span><span class="cov0" title="0">{
|
||||
@ -842,6 +865,22 @@ func (c ToTimeString) MarshalJSON() ([]byte, error) <span class="cov8" title="1"
|
||||
func (c ToTimestamp) MarshalJSON() ([]byte, error) <span class="cov8" title="1">{
|
||||
return []byte(fmt.Sprintf(`%d`, c.ToTimestamp())), nil
|
||||
}</span>
|
||||
|
||||
func (c ToTimestampWithSecond) MarshalJSON() ([]byte, error) <span class="cov0" title="0">{
|
||||
return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithSecond())), nil
|
||||
}</span>
|
||||
|
||||
func (c ToTimestampWithMillisecond) MarshalJSON() ([]byte, error) <span class="cov0" title="0">{
|
||||
return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithMillisecond())), nil
|
||||
}</span>
|
||||
|
||||
func (c ToTimestampWithMicrosecond) MarshalJSON() ([]byte, error) <span class="cov0" title="0">{
|
||||
return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithMicrosecond())), nil
|
||||
}</span>
|
||||
|
||||
func (c ToTimestampWithNanosecond) MarshalJSON() ([]byte, error) <span class="cov0" title="0">{
|
||||
return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithNanosecond())), nil
|
||||
}</span>
|
||||
</pre>
|
||||
|
||||
<pre class="file" id="file3" style="display: none">package carbon
|
||||
|
Loading…
Reference in New Issue
Block a user