{
+func (start Carbon) DiffInDaysWithAbs(end Carbon) int64 {
diff := start.DiffInDays(end)
if diff < 0 {
diff = -diff
@@ -1134,7 +1130,7 @@
}
// DiffAbsInHours 相差多少小时(绝对值)
-func (start Carbon) DiffAbsInHours(end Carbon) int64 {
+func (start Carbon) DiffInHoursWithAbs(end Carbon) int64 {
diff := start.DiffInHours(end)
if diff < 0 {
diff = -diff
@@ -1148,7 +1144,7 @@
}
// DiffAbsInMinutes 相差多少分钟(绝对值)
-func (start Carbon) DiffAbsInMinutes(end Carbon) int64 {
+func (start Carbon) DiffInMinutesWithAbs(end Carbon) int64 {
diff := start.DiffInMinutes(end)
if diff < 0 {
diff = -diff
@@ -1172,7 +1168,7 @@
}
// DiffAbsInSeconds 相差多少秒(绝对值)
-func (start Carbon) DiffAbsInSeconds(end Carbon) int64 {
+func (start Carbon) DiffInSecondsWithAbs(end Carbon) int64 {
diff := start.DiffInSeconds(end)
if diff < 0 {
diff = -diff
@@ -1520,6 +1516,90 @@
func (c Carbon) IsTomorrow() bool {
return c.ToDateString() == c.Tomorrow().ToDateString()
}
+
+// Compare 时间比较
+func (c Carbon) Compare(operator string, t Carbon) bool {
+ switch operator {
+ case "=":
+ return c.Time.Equal(t.Time)
+ case "<>":
+ return !c.Time.Equal(t.Time)
+ case "!=":
+ return !c.Time.Equal(t.Time)
+ case ">":
+ return c.Time.After(t.Time)
+ case ">=":
+ return c.Time.After(t.Time) || c.Time.Equal(t.Time)
+ case "<":
+ return c.Time.Before(t.Time)
+ case "<=":
+ return c.Time.Before(t.Time) || c.Time.Equal(t.Time)
+ }
+
+ return false
+}
+
+// Gt 大于
+func (c Carbon) Gt(t Carbon) bool {
+ return c.Time.After(t.Time)
+}
+
+// Lt 小于
+func (c Carbon) Lt(t Carbon) bool {
+ return c.Time.Before(t.Time)
+}
+
+// Eq 等于
+func (c Carbon) Eq(operator string, t Carbon) bool {
+ return c.Time.Equal(t.Time)
+}
+
+// Ne 不等于
+func (c Carbon) Ne(t Carbon) bool {
+ return !c.Time.Equal(t.Time)
+}
+
+// Gte 大于等于
+func (c Carbon) Gte(t Carbon) bool {
+ return c.Time.After(t.Time) || c.Time.Equal(t.Time)
+}
+
+// Lte 小于等于
+func (c Carbon) Lte(t Carbon) bool {
+ return c.Time.Before(t.Time) || c.Time.Equal(t.Time)
+}
+
+// Between 是否在两个时间之间(不包括这两个时间)
+func (c Carbon) Between(start Carbon, end Carbon) bool {
+ if c.Time.After(start.Time) && c.Time.Before(end.Time) {
+ return true
+ }
+ return false
+}
+
+// BetweenIncludedStartTime 是否在两个时间之间(包括开始时间)
+func (c Carbon) BetweenIncludedStartTime(start Carbon, end Carbon) bool {
+ if (c.Time.After(start.Time) || c.Time.Equal(start.Time)) && c.Time.Before(end.Time) {
+ return true
+ }
+ return false
+}
+
+// BetweenIncludedEndTime 是否在两个时间之间(包括结束时间)
+func (c Carbon) BetweenIncludedEndTime(start Carbon, end Carbon) bool {
+ if c.Time.After(start.Time) && (c.Time.Before(end.Time) || c.Time.Equal(end.Time)) {
+ return true
+ }
+ return false
+}
+
+// BetweenIncludedBoth 是否在两个时间之间(包括这两个时间)
+func (c Carbon) BetweenIncludedBoth(start Carbon, end Carbon) bool {
+ if (c.Time.After(start.Time) || c.Time.Equal(start.Time)) && (c.Time.Before(end.Time) || c.Time.Equal(end.Time)) {
+ return true
+ }
+ return false
+}
package carbon
diff --git a/database.go b/database.go
index 9e0c0cde..568c0d48 100644
--- a/database.go
+++ b/database.go
@@ -56,10 +56,6 @@ func (c Carbon) Value() (driver.Value, error) {
return timeTime, nil
}
-func (c Carbon) MarshalJSON() ([]byte, error) {
- return []byte(fmt.Sprintf(`"%s"`, c.ToDateTimeString())), nil
-}
-
func (c ToDateTimeString) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, c.ToDateTimeString())), nil
}
diff --git a/database_test.go b/database_test.go
index 56c11c34..4c2eea44 100644
--- a/database_test.go
+++ b/database_test.go
@@ -7,24 +7,31 @@ import (
)
var user = struct {
- ID int64 `json:"id"`
- Name string `json:"name"`
- Age int `json:"age"`
- Birthday Carbon `json:"birthday"`
- CreatedAt ToDateTimeString `json:"created_at"`
- DeletedAt ToTimestamp `json:"deleted_at"`
- GraduatedAt ToDateString `json:"graduated_at"`
- UpdatedAt ToTimeString `json:"updated_at"`
-}{Name: "勾国印",
+ ID int64 `json:"id"`
+ Name string `json:"name"`
+ Age int `json:"age"`
+ Birthday ToDateTimeString `json:"birthday"`
+ GraduatedAt ToDateString `json:"graduated_at"`
+ CreatedAt ToTimeString `json:"created_at"`
+ UpdatedAt ToTimestamp `json:"updated_at"`
+ DateTime1 ToTimestampWithSecond `json:"date_time1"`
+ DateTime2 ToTimestampWithMillisecond `json:"date_time2"`
+ DateTime3 ToTimestampWithMicrosecond `json:"date_time3"`
+ DateTime4 ToTimestampWithNanosecond `json:"date_time4"`
+}{
+ Name: "勾国印",
Age: 18,
- Birthday: Now().SubYears(18),
- CreatedAt: ToDateTimeString{Now()},
- DeletedAt: ToTimestamp{Parse("2020-08-05 13:14:15")},
+ Birthday: ToDateTimeString{Now().SubYears(18)},
GraduatedAt: ToDateString{Parse("2012-09-09")},
- UpdatedAt: ToTimeString{Now()},
+ CreatedAt: ToTimeString{Now()},
+ UpdatedAt: ToTimestamp{Now()},
+ DateTime1: ToTimestampWithSecond{Now()},
+ DateTime2: ToTimestampWithMillisecond{Now()},
+ DateTime3: ToTimestampWithMicrosecond{Now()},
+ DateTime4: ToTimestampWithNanosecond{Now()},
}
func TestCarbon_MarshalJSON(t *testing.T) {
data, _ := json.Marshal(&user)
- fmt.Print("Model output by json:\n", string(data))
+ fmt.Print("Model output by json:\n", string(data)+"\n")
}
diff --git a/final.go b/final.go
index d4f4532a..efdc8e86 100755
--- a/final.go
+++ b/final.go
@@ -221,7 +221,7 @@ func (start Carbon) DiffInWeeks(end Carbon) int64 {
}
// DiffAbsInWeeks 相差多少周(绝对值)
-func (start Carbon) DiffAbsInWeeks(end Carbon) int64 {
+func (start Carbon) DiffInWeeksWithAbs(end Carbon) int64 {
diff := start.DiffInWeeks(end)
if diff < 0 {
diff = -diff
@@ -235,7 +235,7 @@ func (start Carbon) DiffInDays(end Carbon) int64 {
}
// DiffAbsInDays 相差多少天(绝对值)
-func (start Carbon) DiffAbsInDays(end Carbon) int64 {
+func (start Carbon) DiffInDaysWithAbs(end Carbon) int64 {
diff := start.DiffInDays(end)
if diff < 0 {
diff = -diff
@@ -249,7 +249,7 @@ func (start Carbon) DiffInHours(end Carbon) int64 {
}
// DiffAbsInHours 相差多少小时(绝对值)
-func (start Carbon) DiffAbsInHours(end Carbon) int64 {
+func (start Carbon) DiffInHoursWithAbs(end Carbon) int64 {
diff := start.DiffInHours(end)
if diff < 0 {
diff = -diff
@@ -263,7 +263,7 @@ func (start Carbon) DiffInMinutes(end Carbon) int64 {
}
// DiffAbsInMinutes 相差多少分钟(绝对值)
-func (start Carbon) DiffAbsInMinutes(end Carbon) int64 {
+func (start Carbon) DiffInMinutesWithAbs(end Carbon) int64 {
diff := start.DiffInMinutes(end)
if diff < 0 {
diff = -diff
@@ -287,7 +287,7 @@ func (start Carbon) DiffInSeconds(end Carbon) int64 {
}
// DiffAbsInSeconds 相差多少秒(绝对值)
-func (start Carbon) DiffAbsInSeconds(end Carbon) int64 {
+func (start Carbon) DiffInSecondsWithAbs(end Carbon) int64 {
diff := start.DiffInSeconds(end)
if diff < 0 {
diff = -diff
@@ -635,3 +635,87 @@ func (c Carbon) IsToday() bool {
func (c Carbon) IsTomorrow() bool {
return c.ToDateString() == c.Tomorrow().ToDateString()
}
+
+// Compare 时间比较
+func (c Carbon) Compare(operator string, t Carbon) bool {
+ switch operator {
+ case "=":
+ return c.Time.Equal(t.Time)
+ case "<>":
+ return !c.Time.Equal(t.Time)
+ case "!=":
+ return !c.Time.Equal(t.Time)
+ case ">":
+ return c.Time.After(t.Time)
+ case ">=":
+ return c.Time.After(t.Time) || c.Time.Equal(t.Time)
+ case "<":
+ return c.Time.Before(t.Time)
+ case "<=":
+ return c.Time.Before(t.Time) || c.Time.Equal(t.Time)
+ }
+
+ return false
+}
+
+// Gt 大于
+func (c Carbon) Gt(t Carbon) bool {
+ return c.Time.After(t.Time)
+}
+
+// Lt 小于
+func (c Carbon) Lt(t Carbon) bool {
+ return c.Time.Before(t.Time)
+}
+
+// Eq 等于
+func (c Carbon) Eq(t Carbon) bool {
+ return c.Time.Equal(t.Time)
+}
+
+// Ne 不等于
+func (c Carbon) Ne(t Carbon) bool {
+ return !c.Time.Equal(t.Time)
+}
+
+// Gte 大于等于
+func (c Carbon) Gte(t Carbon) bool {
+ return c.Time.After(t.Time) || c.Time.Equal(t.Time)
+}
+
+// Lte 小于等于
+func (c Carbon) Lte(t Carbon) bool {
+ return c.Time.Before(t.Time) || c.Time.Equal(t.Time)
+}
+
+// Between 是否在两个时间之间(不包括这两个时间)
+func (c Carbon) Between(start Carbon, end Carbon) bool {
+ if c.Time.After(start.Time) && c.Time.Before(end.Time) {
+ return true
+ }
+ return false
+}
+
+// BetweenIncludedStartTime 是否在两个时间之间(包括开始时间)
+func (c Carbon) BetweenIncludedStartTime(start Carbon, end Carbon) bool {
+ if (c.Time.After(start.Time) || c.Time.Equal(start.Time)) && c.Time.Before(end.Time) {
+ return true
+ }
+ return false
+}
+
+// BetweenIncludedEndTime 是否在两个时间之间(包括结束时间)
+func (c Carbon) BetweenIncludedEndTime(start Carbon, end Carbon) bool {
+ if c.Time.After(start.Time) && (c.Time.Before(end.Time) || c.Time.Equal(end.Time)) {
+ return true
+ }
+ return false
+}
+
+// BetweenIncludedBoth 是否在两个时间之间(包括这两个时间)
+func (c Carbon) BetweenIncludedBoth(start Carbon, end Carbon) bool {
+ if (c.Time.After(start.Time) || c.Time.Equal(start.Time)) && (c.Time.Before(end.Time) || c.Time.Equal(end.Time)) {
+ return true
+ }
+ return false
+}
diff --git a/final_test.go b/final_test.go
index 83efa746..8327d154 100755
--- a/final_test.go
+++ b/final_test.go
@@ -608,7 +608,7 @@ func TestCarbon_DiffInWeeks(t *testing.T) {
}
}
-func TestCarbon_DiffAbsInWeeks(t *testing.T) {
+func TestCarbon_DiffInWeeksWithAbs(t *testing.T) {
Tests := []struct {
input1 string // 输入值1
input2 string // 输入值2
@@ -631,7 +631,7 @@ func TestCarbon_DiffAbsInWeeks(t *testing.T) {
}
for _, v := range Tests {
- output := Parse(v.input1).DiffAbsInWeeks(Parse(v.input2))
+ output := Parse(v.input1).DiffInWeeksWithAbs(Parse(v.input2))
if output != v.output {
t.Fatalf("Input start time %s and end time %s, expected %d, but got %d", v.input1, v.input2, v.output, output)
@@ -670,7 +670,7 @@ func TestCarbon_DiffInDays(t *testing.T) {
}
}
-func TestCarbon_DiffAbsInDays(t *testing.T) {
+func TestCarbon_DiffInDaysWithAbs(t *testing.T) {
Tests := []struct {
input1 string // 输入值1
input2 string // 输入值2
@@ -693,7 +693,7 @@ func TestCarbon_DiffAbsInDays(t *testing.T) {
}
for _, v := range Tests {
- output := Parse(v.input1).DiffAbsInDays(Parse(v.input2))
+ output := Parse(v.input1).DiffInDaysWithAbs(Parse(v.input2))
if output != v.output {
t.Fatalf("Input start time %s and end time %s, expected %d, but got %d", v.input1, v.input2, v.output, output)
@@ -732,7 +732,7 @@ func TestCarbon_DiffInHours(t *testing.T) {
}
}
-func TestCarbon_DiffAbsInHours(t *testing.T) {
+func TestCarbon_DiffInHoursWithAbs(t *testing.T) {
Tests := []struct {
input1 string // 输入值1
input2 string // 输入值2
@@ -755,7 +755,7 @@ func TestCarbon_DiffAbsInHours(t *testing.T) {
}
for _, v := range Tests {
- output := Parse(v.input1).DiffAbsInHours(Parse(v.input2))
+ output := Parse(v.input1).DiffInHoursWithAbs(Parse(v.input2))
if output != v.output {
t.Fatalf("Input start time %s and end time %s, expected %d, but got %d", v.input1, v.input2, v.output, output)
@@ -794,7 +794,7 @@ func TestCarbon_DiffInMinutes(t *testing.T) {
}
}
-func TestCarbon_DiffAbsInMinutes(t *testing.T) {
+func TestCarbon_DiffInMinutesWithAbs(t *testing.T) {
Tests := []struct {
input1 string // 输入值1
input2 string // 输入值2
@@ -817,7 +817,7 @@ func TestCarbon_DiffAbsInMinutes(t *testing.T) {
}
for _, v := range Tests {
- output := Parse(v.input1).DiffAbsInMinutes(Parse(v.input2))
+ output := Parse(v.input1).DiffInMinutesWithAbs(Parse(v.input2))
if output != v.output {
t.Fatalf("Input start time %s and end time %s, expected %d, but got %d", v.input1, v.input2, v.output, output)
@@ -848,7 +848,7 @@ func TestCarbon_DiffInSeconds(t *testing.T) {
}
}
-func TestCarbon_DiffAbsInSeconds(t *testing.T) {
+func TestCarbon_DiffInSecondsWithAbs(t *testing.T) {
Tests := []struct {
input1 string // 输入值1
input2 string // 输入值2
@@ -863,7 +863,7 @@ func TestCarbon_DiffAbsInSeconds(t *testing.T) {
}
for _, v := range Tests {
- output := Parse(v.input1).DiffAbsInSeconds(Parse(v.input2))
+ output := Parse(v.input1).DiffInSecondsWithAbs(Parse(v.input2))
if output != v.output {
t.Fatalf("Input start time %s and end time %s, expected %d, but got %d", v.input1, v.input2, v.output, output)
@@ -2295,3 +2295,362 @@ func TestCarbon_IsTomorrow(t *testing.T) {
}
}
}
+
+func TestCarbon_Compare(t *testing.T) {
+ now := Now()
+ tomorrow := Tomorrow()
+ yesterday := Yesterday()
+ Tests := []struct {
+ input Carbon // 输入值
+ operator string // 输入参数
+ time Carbon // 输入参数
+ output bool // 期望输出值
+ }{
+ {now, ">", yesterday, true},
+ {now, "<", yesterday, false},
+ {now, "<", tomorrow, true},
+ {now, ">", tomorrow, false},
+ {now, "=", now, true},
+ {now, ">=", now, true},
+ {now, "<=", now, true},
+ {now, "!=", now, false},
+ {now, "<>", now, false},
+ {now, "!=", yesterday, true},
+ {now, "<>", yesterday, true},
+ {now, "+", yesterday, false},
+ }
+
+ for _, v := range Tests {
+ output := v.input.Compare(v.operator, v.time)
+
+ if output != v.output {
+ expected := "false"
+ if v.output == true {
+ expected = "true"
+ }
+
+ reality := "false"
+ if output == true {
+ reality = "true"
+ }
+ t.Fatalf("Input %s %s %s, expected %s, but got %s\n", v.input.ToDateTimeString(), v.operator, v.time.ToDateTimeString(), expected, reality)
+ }
+ }
+}
+
+func TestCarbon_Gt(t *testing.T) {
+ now := Now()
+ tomorrow := Tomorrow()
+ yesterday := Yesterday()
+ Tests := []struct {
+ input Carbon // 输入值
+ time Carbon // 输入参数
+ output bool // 期望输出值
+ }{
+ {now, now, false},
+ {now, yesterday, true},
+ {now, tomorrow, false},
+ }
+
+ for _, v := range Tests {
+ output := v.input.Gt(v.time)
+
+ if output != v.output {
+ expected := "false"
+ if v.output == true {
+ expected = "true"
+ }
+
+ reality := "false"
+ if output == true {
+ reality = "true"
+ }
+ t.Fatalf("Input %s > %s, expected %s, but got %s\n", v.input.ToDateTimeString(), v.time.ToDateTimeString(), expected, reality)
+ }
+ }
+}
+
+func TestCarbon_Lt(t *testing.T) {
+ now := Now()
+ tomorrow := Tomorrow()
+ yesterday := Yesterday()
+ Tests := []struct {
+ input Carbon // 输入值
+ time Carbon // 输入参数
+ output bool // 期望输出值
+ }{
+ {now, now, false},
+ {now, yesterday, false},
+ {now, tomorrow, true},
+ }
+
+ for _, v := range Tests {
+ output := v.input.Lt(v.time)
+
+ if output != v.output {
+ expected := "false"
+ if v.output == true {
+ expected = "true"
+ }
+
+ reality := "false"
+ if output == true {
+ reality = "true"
+ }
+ t.Fatalf("Input %s < %s, expected %s, but got %s\n", v.input.ToDateTimeString(), v.time.ToDateTimeString(), expected, reality)
+ }
+ }
+}
+
+func TestCarbon_Eq(t *testing.T) {
+ now := Now()
+ tomorrow := Tomorrow()
+ yesterday := Yesterday()
+ Tests := []struct {
+ input Carbon // 输入值
+ time Carbon // 输入参数
+ output bool // 期望输出值
+ }{
+ {now, now, true},
+ {now, yesterday, false},
+ {now, tomorrow, false},
+ }
+
+ for _, v := range Tests {
+ output := v.input.Eq(v.time)
+
+ if output != v.output {
+ expected := "false"
+ if v.output == true {
+ expected = "true"
+ }
+
+ reality := "false"
+ if output == true {
+ reality = "true"
+ }
+ t.Fatalf("Input %s = %s, expected %s, but got %s\n", v.input.ToDateTimeString(), v.time.ToDateTimeString(), expected, reality)
+ }
+ }
+}
+
+func TestCarbon_Ne(t *testing.T) {
+ now := Now()
+ tomorrow := Tomorrow()
+ yesterday := Yesterday()
+ Tests := []struct {
+ input Carbon // 输入值
+ time Carbon // 输入参数
+ output bool // 期望输出值
+ }{
+ {now, now, false},
+ {now, yesterday, true},
+ {now, tomorrow, true},
+ }
+
+ for _, v := range Tests {
+ output := v.input.Ne(v.time)
+
+ if output != v.output {
+ expected := "false"
+ if v.output == true {
+ expected = "true"
+ }
+
+ reality := "false"
+ if output == true {
+ reality = "true"
+ }
+ t.Fatalf("Input %s != %s, expected %s, but got %s\n", v.input.ToDateTimeString(), v.time.ToDateTimeString(), expected, reality)
+ }
+ }
+}
+
+func TestCarbon_Gte(t *testing.T) {
+ now := Now()
+ tomorrow := Tomorrow()
+ yesterday := Yesterday()
+ Tests := []struct {
+ input Carbon // 输入值
+ time Carbon // 输入参数
+ output bool // 期望输出值
+ }{
+ {now, now, true},
+ {now, yesterday, true},
+ {now, tomorrow, false},
+ }
+
+ for _, v := range Tests {
+ output := v.input.Gte(v.time)
+
+ if output != v.output {
+ expected := "false"
+ if v.output == true {
+ expected = "true"
+ }
+
+ reality := "false"
+ if output == true {
+ reality = "true"
+ }
+ t.Fatalf("Input %s >= %s, expected %s, but got %s\n", v.input.ToDateTimeString(), v.time.ToDateTimeString(), expected, reality)
+ }
+ }
+}
+
+func TestCarbon_Lte(t *testing.T) {
+ now := Now()
+ tomorrow := Tomorrow()
+ yesterday := Yesterday()
+ Tests := []struct {
+ input Carbon // 输入值
+ time Carbon // 输入参数
+ output bool // 期望输出值
+ }{
+ {now, now, true},
+ {now, yesterday, false},
+ {now, tomorrow, true},
+ }
+
+ for _, v := range Tests {
+ output := v.input.Lte(v.time)
+
+ if output != v.output {
+ expected := "false"
+ if v.output == true {
+ expected = "true"
+ }
+
+ reality := "false"
+ if output == true {
+ reality = "true"
+ }
+ t.Fatalf("Input %s <= %s, expected %s, but got %s\n", v.input.ToDateTimeString(), v.time.ToDateTimeString(), expected, reality)
+ }
+ }
+}
+
+func TestCarbon_Between(t *testing.T) {
+ Tests := []struct {
+ input Carbon // 输入值
+ time1 Carbon // 输入参数
+ time2 Carbon // 输入参数
+ output bool // 期望输出值
+ }{
+ {Parse("2020-08-05 13:14:15"), Parse("2020-08-05 13:14:15"), Parse("2020-08-05 13:14:15"), false},
+ {Parse("2020-08-05 13:14:15"), Parse("2020-08-05 13:14:15"), Parse("2020-08-06 13:14:15"), false},
+ {Parse("2020-08-05 13:14:15"), Parse("2020-08-04 13:14:15"), Parse("2020-08-05 13:14:15"), false},
+ {Parse("2020-08-05 13:14:15"), Parse("2020-08-04 13:14:15"), Parse("2020-08-06 13:14:15"), true},
+ }
+
+ for _, v := range Tests {
+ output := v.input.Between(v.time1, v.time2)
+
+ if output != v.output {
+ expected := "false"
+ if v.output == true {
+ expected = "true"
+ }
+
+ reality := "false"
+ if output == true {
+ reality = "true"
+ }
+ t.Fatalf("Input %s < %s < %s, expected %s, but got %s\n", v.time1.ToDateTimeString(), v.input.ToDateTimeString(), v.time2.ToDateTimeString(), expected, reality)
+ }
+ }
+}
+
+func TestCarbon_BetweenIncludedStartTime(t *testing.T) {
+ Tests := []struct {
+ input Carbon // 输入值
+ time1 Carbon // 输入参数
+ time2 Carbon // 输入参数
+ output bool // 期望输出值
+ }{
+ {Parse("2020-08-05 13:14:15"), Parse("2020-08-05 13:14:15"), Parse("2020-08-05 13:14:15"), false},
+ {Parse("2020-08-05 13:14:15"), Parse("2020-08-05 13:14:15"), Parse("2020-08-06 13:14:15"), true},
+ {Parse("2020-08-05 13:14:15"), Parse("2020-08-04 13:14:15"), Parse("2020-08-05 13:14:15"), false},
+ {Parse("2020-08-05 13:14:15"), Parse("2020-08-04 13:14:15"), Parse("2020-08-06 13:14:15"), true},
+ }
+
+ for _, v := range Tests {
+ output := v.input.BetweenIncludedStartTime(v.time1, v.time2)
+
+ if output != v.output {
+ expected := "false"
+ if v.output == true {
+ expected = "true"
+ }
+
+ reality := "false"
+ if output == true {
+ reality = "true"
+ }
+ t.Fatalf("Input %s <= %s < %s, expected %s, but got %s\n", v.time1.ToDateTimeString(), v.input.ToDateTimeString(), v.time2.ToDateTimeString(), expected, reality)
+ }
+ }
+}
+
+func TestCarbon_BetweenIncludedEndTime(t *testing.T) {
+ Tests := []struct {
+ input Carbon // 输入值
+ time1 Carbon // 输入参数
+ time2 Carbon // 输入参数
+ output bool // 期望输出值
+ }{
+ {Parse("2020-08-05 13:14:15"), Parse("2020-08-05 13:14:15"), Parse("2020-08-05 13:14:15"), false},
+ {Parse("2020-08-05 13:14:15"), Parse("2020-08-05 13:14:15"), Parse("2020-08-06 13:14:15"), false},
+ {Parse("2020-08-05 13:14:15"), Parse("2020-08-04 13:14:15"), Parse("2020-08-05 13:14:15"), true},
+ {Parse("2020-08-05 13:14:15"), Parse("2020-08-04 13:14:15"), Parse("2020-08-06 13:14:15"), true},
+ }
+
+ for _, v := range Tests {
+ output := v.input.BetweenIncludedEndTime(v.time1, v.time2)
+
+ if output != v.output {
+ expected := "false"
+ if v.output == true {
+ expected = "true"
+ }
+
+ reality := "false"
+ if output == true {
+ reality = "true"
+ }
+ t.Fatalf("Input %s < %s <= %s, expected %s, but got %s\n", v.time1.ToDateTimeString(), v.input.ToDateTimeString(), v.time2.ToDateTimeString(), expected, reality)
+ }
+ }
+}
+
+func TestCarbon_BetweenIncludedBoth(t *testing.T) {
+ Tests := []struct {
+ input Carbon // 输入值
+ time1 Carbon // 输入参数
+ time2 Carbon // 输入参数
+ output bool // 期望输出值
+ }{
+ {Parse("2020-08-05 13:14:15"), Parse("2020-08-05 13:14:15"), Parse("2020-08-05 13:14:15"), true},
+ {Parse("2020-08-05 13:14:15"), Parse("2020-08-05 13:14:15"), Parse("2020-08-06 13:14:15"), true},
+ {Parse("2020-08-05 13:14:15"), Parse("2020-08-04 13:14:15"), Parse("2020-08-05 13:14:15"), true},
+ {Parse("2020-08-05 13:14:15"), Parse("2020-08-04 13:14:15"), Parse("2020-08-06 13:14:15"), true},
+ {Parse("2020-08-05 13:14:15"), Parse("2020-08-06 13:14:15"), Parse("2020-08-06 13:14:15"), false},
+ }
+
+ for _, v := range Tests {
+ output := v.input.BetweenIncludedBoth(v.time1, v.time2)
+
+ if output != v.output {
+ expected := "false"
+ if v.output == true {
+ expected = "true"
+ }
+
+ reality := "false"
+ if output == true {
+ reality = "true"
+ }
+ t.Fatalf("Input %s <= %s <= %s, expected %s, but got %s\n", v.time1.ToDateTimeString(), v.input.ToDateTimeString(), v.time2.ToDateTimeString(), expected, reality)
+ }
+ }
+}