Skip to content

Commit

Permalink
v1.2.4新增时间比较系列方法
Browse files Browse the repository at this point in the history
  • Loading branch information
gouguoyin committed Nov 6, 2020
1 parent a708975 commit c88284d
Show file tree
Hide file tree
Showing 8 changed files with 1,018 additions and 289 deletions.
335 changes: 217 additions & 118 deletions README.en.md

Large diffs are not rendered by default.

348 changes: 226 additions & 122 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion const.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "time"

// 版权信息
const (
Version = "v1.2.3"
Version = "v1.2.4"
Author = "gouguoyin"
Email = "[email protected]"
Blog = "www.gouguoyin.cn"
Expand Down
110 changes: 95 additions & 15 deletions coverage.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@

<option value="file1">github.com/golang-module/carbon/carbon.go (100.0%)</option>

<option value="file2">github.com/golang-module/carbon/database.go (26.3%)</option>
<option value="file2">github.com/golang-module/carbon/database.go (44.4%)</option>

<option value="file3">github.com/golang-module/carbon/final.go (100.0%)</option>
<option value="file3">github.com/golang-module/carbon/final.go (97.5%)</option>

<option value="file4">github.com/golang-module/carbon/private.go (100.0%)</option>

Expand Down Expand Up @@ -846,10 +846,6 @@
<span class="cov0" title="0">return timeTime, nil</span>
}

func (c Carbon) MarshalJSON() ([]byte, error) <span class="cov8" title="1">{
return []byte(fmt.Sprintf(`"%s"`, c.ToDateTimeString())), nil
}</span>

func (c ToDateTimeString) MarshalJSON() ([]byte, error) <span class="cov8" title="1">{
return []byte(fmt.Sprintf(`"%s"`, c.ToDateTimeString())), nil
}</span>
Expand All @@ -866,19 +862,19 @@
return []byte(fmt.Sprintf(`%d`, c.ToTimestamp())), nil
}</span>

func (c ToTimestampWithSecond) MarshalJSON() ([]byte, error) <span class="cov0" title="0">{
func (c ToTimestampWithSecond) MarshalJSON() ([]byte, error) <span class="cov8" title="1">{
return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithSecond())), nil
}</span>

func (c ToTimestampWithMillisecond) MarshalJSON() ([]byte, error) <span class="cov0" title="0">{
func (c ToTimestampWithMillisecond) MarshalJSON() ([]byte, error) <span class="cov8" title="1">{
return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithMillisecond())), nil
}</span>

func (c ToTimestampWithMicrosecond) MarshalJSON() ([]byte, error) <span class="cov0" title="0">{
func (c ToTimestampWithMicrosecond) MarshalJSON() ([]byte, error) <span class="cov8" title="1">{
return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithMicrosecond())), nil
}</span>

func (c ToTimestampWithNanosecond) MarshalJSON() ([]byte, error) <span class="cov0" title="0">{
func (c ToTimestampWithNanosecond) MarshalJSON() ([]byte, error) <span class="cov8" title="1">{
return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithNanosecond())), nil
}</span>
</pre>
Expand Down Expand Up @@ -1106,7 +1102,7 @@
}</span>

// DiffAbsInWeeks 相差多少周(绝对值)
func (start Carbon) DiffAbsInWeeks(end Carbon) int64 <span class="cov8" title="1">{
func (start Carbon) DiffInWeeksWithAbs(end Carbon) int64 <span class="cov8" title="1">{
diff := start.DiffInWeeks(end)
if diff &lt; 0 </span><span class="cov8" title="1">{
diff = -diff
Expand All @@ -1120,7 +1116,7 @@
}</span>

// DiffAbsInDays 相差多少天(绝对值)
func (start Carbon) DiffAbsInDays(end Carbon) int64 <span class="cov8" title="1">{
func (start Carbon) DiffInDaysWithAbs(end Carbon) int64 <span class="cov8" title="1">{
diff := start.DiffInDays(end)
if diff &lt; 0 </span><span class="cov8" title="1">{
diff = -diff
Expand All @@ -1134,7 +1130,7 @@
}</span>

// DiffAbsInHours 相差多少小时(绝对值)
func (start Carbon) DiffAbsInHours(end Carbon) int64 <span class="cov8" title="1">{
func (start Carbon) DiffInHoursWithAbs(end Carbon) int64 <span class="cov8" title="1">{
diff := start.DiffInHours(end)
if diff &lt; 0 </span><span class="cov8" title="1">{
diff = -diff
Expand All @@ -1148,7 +1144,7 @@
}</span>

// DiffAbsInMinutes 相差多少分钟(绝对值)
func (start Carbon) DiffAbsInMinutes(end Carbon) int64 <span class="cov8" title="1">{
func (start Carbon) DiffInMinutesWithAbs(end Carbon) int64 <span class="cov8" title="1">{
diff := start.DiffInMinutes(end)
if diff &lt; 0 </span><span class="cov8" title="1">{
diff = -diff
Expand All @@ -1172,7 +1168,7 @@
}

// DiffAbsInSeconds 相差多少秒(绝对值)
func (start Carbon) DiffAbsInSeconds(end Carbon) int64 <span class="cov8" title="1">{
func (start Carbon) DiffInSecondsWithAbs(end Carbon) int64 <span class="cov8" title="1">{
diff := start.DiffInSeconds(end)
if diff &lt; 0 </span><span class="cov8" title="1">{
diff = -diff
Expand Down Expand Up @@ -1520,6 +1516,90 @@
func (c Carbon) IsTomorrow() bool <span class="cov8" title="1">{
return c.ToDateString() == c.Tomorrow().ToDateString()
}</span>

// Compare 时间比较
func (c Carbon) Compare(operator string, t Carbon) bool <span class="cov8" title="1">{
switch operator </span>{
case "=":<span class="cov8" title="1">
return c.Time.Equal(t.Time)</span>
case "&lt;&gt;":<span class="cov8" title="1">
return !c.Time.Equal(t.Time)</span>
case "!=":<span class="cov8" title="1">
return !c.Time.Equal(t.Time)</span>
case "&gt;":<span class="cov8" title="1">
return c.Time.After(t.Time)</span>
case "&gt;=":<span class="cov8" title="1">
return c.Time.After(t.Time) || c.Time.Equal(t.Time)</span>
case "&lt;":<span class="cov8" title="1">
return c.Time.Before(t.Time)</span>
case "&lt;=":<span class="cov8" title="1">
return c.Time.Before(t.Time) || c.Time.Equal(t.Time)</span>
}

<span class="cov8" title="1">return false</span>
}

// Gt 大于
func (c Carbon) Gt(t Carbon) bool {
return c.Time.After(t.Time)
}</span>

// Lt 小于
func (c Carbon) Lt(t Carbon) bool {
return c.Time.Before(t.Time)
}</span>

// Eq 等于
func (c Carbon) Eq(operator string, t Carbon) bool <span class="cov0" title="0">{
return c.Time.Equal(t.Time)
}</span>

// Ne 不等于
func (c Carbon) Ne(t Carbon) bool {
return !c.Time.Equal(t.Time)
}</span>

// Gte 大于等于
func (c Carbon) Gte(t Carbon) bool {
return c.Time.After(t.Time) || c.Time.Equal(t.Time)
}</span>

// Lte 小于等于
func (c Carbon) Lte(t Carbon) bool {
return c.Time.Before(t.Time) || c.Time.Equal(t.Time)
}</span>

// Between 是否在两个时间之间(不包括这两个时间)
func (c Carbon) Between(start Carbon, end Carbon) bool <span class="cov8" title="1">{
if c.Time.After(start.Time) &amp;&amp; c.Time.Before(end.Time) </span><span class="cov8" title="1">{
return true
}</span>
<span class="cov8" title="1">return false</span>
}

// BetweenIncludedStartTime 是否在两个时间之间(包括开始时间)
func (c Carbon) BetweenIncludedStartTime(start Carbon, end Carbon) bool <span class="cov8" title="1">{
if (c.Time.After(start.Time) || c.Time.Equal(start.Time)) &amp;&amp; c.Time.Before(end.Time) </span><span class="cov8" title="1">{
return true
}</span>
<span class="cov8" title="1">return false</span>
}

// BetweenIncludedEndTime 是否在两个时间之间(包括结束时间)
func (c Carbon) BetweenIncludedEndTime(start Carbon, end Carbon) bool <span class="cov8" title="1">{
if c.Time.After(start.Time) &amp;&amp; (c.Time.Before(end.Time) || c.Time.Equal(end.Time)) </span><span class="cov8" title="1">{
return true
}</span>
<span class="cov8" title="1">return false</span>
}

// BetweenIncludedBoth 是否在两个时间之间(包括这两个时间)
func (c Carbon) BetweenIncludedBoth(start Carbon, end Carbon) bool <span class="cov8" title="1">{
if (c.Time.After(start.Time) || c.Time.Equal(start.Time)) &amp;&amp; (c.Time.Before(end.Time) || c.Time.Equal(end.Time)) </span><span class="cov8" title="1">{
return true
}</span>
<span class="cov8" title="1">return false</span>
}
</pre>

<pre class="file" id="file4" style="display: none">package carbon
Expand Down
4 changes: 0 additions & 4 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
35 changes: 21 additions & 14 deletions database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
94 changes: 89 additions & 5 deletions final.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Loading

0 comments on commit c88284d

Please sign in to comment.