Skip to content

Commit

Permalink
Duration()拆分成AddDuration()和SubDuration()
Browse files Browse the repository at this point in the history
  • Loading branch information
gouguoyin committed Nov 2, 2020
1 parent 795e513 commit a708975
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 30 deletions.
12 changes: 6 additions & 6 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
13 changes: 10 additions & 3 deletions carbon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
57 changes: 46 additions & 11 deletions carbon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,46 +754,81 @@ 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)
}
}

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)
Expand Down
47 changes: 43 additions & 4 deletions coverage.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -379,13 +379,20 @@
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)
Expand Down Expand Up @@ -805,6 +812,22 @@
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">{
Expand Down Expand Up @@ -842,6 +865,22 @@
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
Expand Down

0 comments on commit a708975

Please sign in to comment.