From a708975dc5ba7b747027440efaec43822d353a65 Mon Sep 17 00:00:00 2001
From: gouguoyin <245629560@qq.com>
Date: Tue, 3 Nov 2020 07:42:19 +0800
Subject: [PATCH] =?UTF-8?q?Duration()=E6=8B=86=E5=88=86=E6=88=90AddDuratio?=
=?UTF-8?q?n()=E5=92=8CSubDuration()?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.en.md | 12 +++++------
README.md | 12 +++++------
carbon.go | 13 +++++++++---
carbon_test.go | 57 ++++++++++++++++++++++++++++++++++++++++----------
coverage.html | 47 +++++++++++++++++++++++++++++++++++++----
5 files changed, 111 insertions(+), 30 deletions(-)
diff --git a/README.en.md b/README.en.md
index 29bcce97..4c25b2d9 100755
--- a/README.en.md
+++ b/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
```
diff --git a/README.md b/README.md
index 552b20e0..fcb5e7c6 100755
--- a/README.md
+++ b/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
```
diff --git a/carbon.go b/carbon.go
index 9827d14a..4e4c35ab 100755
--- a/carbon.go
+++ b/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)
diff --git a/carbon_test.go b/carbon_test.go
index 3649af13..81adf2b6 100755
--- a/carbon_test.go
+++ b/carbon_test.go
@@ -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)
diff --git a/coverage.html b/coverage.html
index 970bcfea..4492b423 100644
--- a/coverage.html
+++ b/coverage.html
@@ -58,7 +58,7 @@
-
+
@@ -379,13 +379,20 @@
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)
@@ -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 {
value, ok := v.(time.Time)
if ok {
@@ -842,6 +865,22 @@
func (c ToTimestamp) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`%d`, c.ToTimestamp())), nil
}
+
+func (c ToTimestampWithSecond) MarshalJSON() ([]byte, error) {
+ return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithSecond())), nil
+}
+
+func (c ToTimestampWithMillisecond) MarshalJSON() ([]byte, error) {
+ return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithMillisecond())), nil
+}
+
+func (c ToTimestampWithMicrosecond) MarshalJSON() ([]byte, error) {
+ return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithMicrosecond())), nil
+}
+
+func (c ToTimestampWithNanosecond) MarshalJSON() ([]byte, error) {
+ return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithNanosecond())), nil
+}
package carbon