mirror of
https://gitee.com/dromara/carbon.git
synced 2024-11-29 18:57:37 +08:00
增加是否是测试时间的判断
This commit is contained in:
parent
ee1915290f
commit
35cc193de5
@ -1,6 +1,7 @@
|
||||
package carbon
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -29,6 +30,8 @@ func (c Carbon) IsInvalid() bool {
|
||||
// 是否是当前时间
|
||||
func (c Carbon) IsNow() bool {
|
||||
if c.IsInvalid() {
|
||||
fmt.Println("bbb")
|
||||
|
||||
return false
|
||||
}
|
||||
return c.Timestamp() == c.Now().Timestamp()
|
||||
|
@ -9,6 +9,9 @@ import (
|
||||
// 相差多少年
|
||||
func (c Carbon) DiffInYears(carbon ...Carbon) int64 {
|
||||
start, end := c, c.Now()
|
||||
if c.HasTestNow() {
|
||||
end = CreateFromTimestampNano(c.testNow, c.Location())
|
||||
}
|
||||
if len(carbon) > 0 {
|
||||
end = carbon[len(carbon)-1]
|
||||
}
|
||||
@ -33,6 +36,9 @@ func (c Carbon) DiffAbsInYears(carbon ...Carbon) int64 {
|
||||
// 相差多少月
|
||||
func (c Carbon) DiffInMonths(carbon ...Carbon) int64 {
|
||||
end := c.Now()
|
||||
if c.HasTestNow() {
|
||||
end = CreateFromTimestampNano(c.testNow, c.Location())
|
||||
}
|
||||
if len(carbon) > 0 {
|
||||
end = carbon[len(carbon)-1]
|
||||
}
|
||||
@ -65,6 +71,9 @@ func (c Carbon) DiffAbsInMonths(carbon ...Carbon) int64 {
|
||||
// 相差多少周
|
||||
func (c Carbon) DiffInWeeks(carbon ...Carbon) int64 {
|
||||
start, end := c, c.Now()
|
||||
if c.HasTestNow() {
|
||||
end = CreateFromTimestampNano(c.testNow, c.Location())
|
||||
}
|
||||
if len(carbon) > 0 {
|
||||
end = carbon[len(carbon)-1]
|
||||
}
|
||||
@ -74,17 +83,16 @@ func (c Carbon) DiffInWeeks(carbon ...Carbon) int64 {
|
||||
// DiffAbsInWeeks gets the difference in weeks with absolute value.
|
||||
// 相差多少周(绝对值)
|
||||
func (c Carbon) DiffAbsInWeeks(carbon ...Carbon) int64 {
|
||||
end := c.Now()
|
||||
if len(carbon) > 0 {
|
||||
end = carbon[len(carbon)-1]
|
||||
}
|
||||
return getAbsValue(c.DiffInWeeks(end))
|
||||
return getAbsValue(c.DiffInWeeks(carbon...))
|
||||
}
|
||||
|
||||
// DiffInDays gets the difference in days.
|
||||
// 相差多少天
|
||||
func (c Carbon) DiffInDays(carbon ...Carbon) int64 {
|
||||
start, end := c, c.Now()
|
||||
if c.HasTestNow() {
|
||||
end = CreateFromTimestampNano(c.testNow, c.Location())
|
||||
}
|
||||
if len(carbon) > 0 {
|
||||
end = carbon[len(carbon)-1]
|
||||
}
|
||||
@ -94,17 +102,16 @@ func (c Carbon) DiffInDays(carbon ...Carbon) int64 {
|
||||
// DiffAbsInDays gets the difference in days with absolute value.
|
||||
// 相差多少天(绝对值)
|
||||
func (c Carbon) DiffAbsInDays(carbon ...Carbon) int64 {
|
||||
end := c.Now()
|
||||
if len(carbon) > 0 {
|
||||
end = carbon[len(carbon)-1]
|
||||
}
|
||||
return getAbsValue(c.DiffInDays(end))
|
||||
return getAbsValue(c.DiffInDays(carbon...))
|
||||
}
|
||||
|
||||
// DiffInHours gets the difference in hours.
|
||||
// 相差多少小时
|
||||
func (c Carbon) DiffInHours(carbon ...Carbon) int64 {
|
||||
end := c.Now()
|
||||
if c.HasTestNow() {
|
||||
end = CreateFromTimestampNano(c.testNow, c.Location())
|
||||
}
|
||||
if len(carbon) > 0 {
|
||||
end = carbon[len(carbon)-1]
|
||||
}
|
||||
@ -114,17 +121,16 @@ func (c Carbon) DiffInHours(carbon ...Carbon) int64 {
|
||||
// DiffAbsInHours gets the difference in hours with absolute value.
|
||||
// 相差多少小时(绝对值)
|
||||
func (c Carbon) DiffAbsInHours(carbon ...Carbon) int64 {
|
||||
end := c.Now()
|
||||
if len(carbon) > 0 {
|
||||
end = carbon[len(carbon)-1]
|
||||
}
|
||||
return getAbsValue(c.DiffInHours(end))
|
||||
return getAbsValue(c.DiffInHours(carbon...))
|
||||
}
|
||||
|
||||
// DiffInMinutes gets the difference in minutes.
|
||||
// 相差多少分钟
|
||||
func (c Carbon) DiffInMinutes(carbon ...Carbon) int64 {
|
||||
end := c.Now()
|
||||
if c.HasTestNow() {
|
||||
end = CreateFromTimestampNano(c.testNow, c.Location())
|
||||
}
|
||||
if len(carbon) > 0 {
|
||||
end = carbon[len(carbon)-1]
|
||||
}
|
||||
@ -134,17 +140,16 @@ func (c Carbon) DiffInMinutes(carbon ...Carbon) int64 {
|
||||
// DiffAbsInMinutes gets the difference in minutes with absolute value.
|
||||
// 相差多少分钟(绝对值)
|
||||
func (c Carbon) DiffAbsInMinutes(carbon ...Carbon) int64 {
|
||||
end := c.Now()
|
||||
if len(carbon) > 0 {
|
||||
end = carbon[len(carbon)-1]
|
||||
}
|
||||
return getAbsValue(c.DiffInMinutes(end))
|
||||
return getAbsValue(c.DiffInMinutes(carbon...))
|
||||
}
|
||||
|
||||
// DiffInSeconds gets the difference in seconds.
|
||||
// 相差多少秒
|
||||
func (c Carbon) DiffInSeconds(carbon ...Carbon) int64 {
|
||||
end := c.Now()
|
||||
if c.HasTestNow() {
|
||||
end = CreateFromTimestampNano(c.testNow, c.Location())
|
||||
}
|
||||
if len(carbon) > 0 {
|
||||
end = carbon[len(carbon)-1]
|
||||
}
|
||||
@ -154,17 +159,16 @@ func (c Carbon) DiffInSeconds(carbon ...Carbon) int64 {
|
||||
// DiffAbsInSeconds gets the difference in seconds with absolute value.
|
||||
// 相差多少秒(绝对值)
|
||||
func (c Carbon) DiffAbsInSeconds(carbon ...Carbon) int64 {
|
||||
end := c.Now()
|
||||
if len(carbon) > 0 {
|
||||
end = carbon[len(carbon)-1]
|
||||
}
|
||||
return getAbsValue(c.DiffInSeconds(end))
|
||||
return getAbsValue(c.DiffInSeconds(carbon...))
|
||||
}
|
||||
|
||||
// DiffInString gets the difference in string, i18n is supported.
|
||||
// 相差字符串,支持i18n
|
||||
func (c Carbon) DiffInString(carbon ...Carbon) string {
|
||||
end := c.Now()
|
||||
if c.HasTestNow() {
|
||||
end = CreateFromTimestampNano(c.testNow, c.Location())
|
||||
}
|
||||
if len(carbon) > 0 {
|
||||
end = carbon[len(carbon)-1]
|
||||
}
|
||||
@ -179,6 +183,9 @@ func (c Carbon) DiffInString(carbon ...Carbon) string {
|
||||
// 相差字符串,支持i18n(绝对值)
|
||||
func (c Carbon) DiffAbsInString(carbon ...Carbon) string {
|
||||
end := c.Now()
|
||||
if c.HasTestNow() {
|
||||
end = CreateFromTimestampNano(c.testNow, c.Location())
|
||||
}
|
||||
if len(carbon) > 0 {
|
||||
end = carbon[len(carbon)-1]
|
||||
}
|
||||
@ -193,6 +200,9 @@ func (c Carbon) DiffAbsInString(carbon ...Carbon) string {
|
||||
// 获取对人类友好的可读格式时间差,支持i18n
|
||||
func (c Carbon) DiffForHumans(carbon ...Carbon) string {
|
||||
end := c.Now()
|
||||
if c.HasTestNow() {
|
||||
end = CreateFromTimestampNano(c.testNow, c.Location())
|
||||
}
|
||||
if len(carbon) > 0 {
|
||||
end = carbon[len(carbon)-1]
|
||||
}
|
||||
|
@ -402,7 +402,10 @@ func (c Carbon) Age() int {
|
||||
return 0
|
||||
}
|
||||
now := c.Now()
|
||||
if c.Timestamp() > now.Timestamp() {
|
||||
if c.HasTestNow() {
|
||||
now = CreateFromTimestampNano(c.testNow, c.Location())
|
||||
}
|
||||
if c.TimestampNano() > now.TimestampNano() {
|
||||
return 0
|
||||
}
|
||||
return int(c.DiffInYears(now))
|
||||
|
12
test.go
12
test.go
@ -7,21 +7,17 @@ func SetTestNow(carbon Carbon) Carbon {
|
||||
|
||||
// SetTestNow sets a test Carbon instance (real or mock) to be returned when a "now" instance is created.
|
||||
func (c Carbon) SetTestNow(carbon Carbon) Carbon {
|
||||
c.time = carbon.time
|
||||
c.loc = carbon.loc
|
||||
c.weekStartsAt = carbon.weekStartsAt
|
||||
c.lang = carbon.lang
|
||||
c.isTestNow = true
|
||||
c.testNow = carbon.TimestampNano()
|
||||
return c
|
||||
}
|
||||
|
||||
// ClearTestNow clears a test Carbon instance (real or mock) to be returned when a "now" instance is created.
|
||||
func (c Carbon) ClearTestNow() Carbon {
|
||||
c.isTestNow = false
|
||||
c.testNow = 0
|
||||
return c
|
||||
}
|
||||
|
||||
// HasTestNow reports whether has the test now time.
|
||||
// HasTestNow report whether there is testing time now.
|
||||
func (c Carbon) HasTestNow() bool {
|
||||
return c.isTestNow
|
||||
return c.testNow > 0
|
||||
}
|
||||
|
14
test_test.go
14
test_test.go
@ -10,9 +10,19 @@ func TestCarbon_SetTestNow(t *testing.T) {
|
||||
|
||||
testNow := Parse("2020-08-05")
|
||||
|
||||
assert.Equal("2020-08-04", SetTestNow(testNow).Yesterday().ToDateString(), "It should be equal to 2020-08-05")
|
||||
assert.Equal("2020-08-04", SetTestNow(testNow).Yesterday().ToDateString(), "It should be equal to 2020-08-04")
|
||||
assert.Equal("2020-08-05", SetTestNow(testNow).Now().ToDateString(), "It should be equal to 2020-08-05")
|
||||
assert.Equal("2020-08-06", SetTestNow(testNow).Tomorrow().ToDateString(), "It should be equal to 2020-08-05")
|
||||
assert.Equal("2020-08-06", SetTestNow(testNow).Tomorrow().ToDateString(), "It should be equal to 2020-08-06")
|
||||
assert.Equal(31, SetTestNow(testNow).Parse("1989-08-05").Age(), "It should be equal to 31")
|
||||
assert.Equal(int64(1), SetTestNow(testNow).Parse("2020-07-05").DiffInMonths(), "It should be equal to 1")
|
||||
assert.Equal(int64(4), SetTestNow(testNow).Parse("2020-07-05").DiffInWeeks(), "It should be equal to 4")
|
||||
assert.Equal(int64(31), SetTestNow(testNow).Parse("2020-07-05").DiffInDays(), "It should be equal to 31")
|
||||
assert.Equal(int64(-13), SetTestNow(testNow).Parse("2020-08-05 13:14:15").DiffInHours(), "It should be equal to -13")
|
||||
assert.Equal(int64(-794), SetTestNow(testNow).Parse("2020-08-05 13:14:15").DiffInMinutes(), "It should be equal to -794")
|
||||
assert.Equal(int64(-47655), SetTestNow(testNow).Parse("2020-08-05 13:14:15").DiffInSeconds(), "It should be equal to -47655")
|
||||
assert.Equal("-13 hours", SetTestNow(testNow).Parse("2020-08-05 13:14:15").DiffInString(), "It should be -13 hours")
|
||||
assert.Equal("13 hours", SetTestNow(testNow).Parse("2020-08-05 13:14:15").DiffAbsInString(), "It should be -13 hours")
|
||||
assert.Equal("13 hours from now", SetTestNow(testNow).Parse("2020-08-05 13:14:15").DiffForHumans(), "It should be 13 hours from now")
|
||||
}
|
||||
|
||||
func TestCarbon_ClearTestNow(t *testing.T) {
|
||||
|
12
traveler.go
12
traveler.go
@ -13,8 +13,8 @@ func (c Carbon) Now(timezone ...string) Carbon {
|
||||
if c.Error != nil {
|
||||
return c
|
||||
}
|
||||
if c.isTestNow && !c.IsZero() {
|
||||
return c
|
||||
if c.HasTestNow() {
|
||||
return CreateFromTimestampNano(c.testNow, c.Location())
|
||||
}
|
||||
c.time = time.Now().In(c.loc)
|
||||
return c
|
||||
@ -35,8 +35,8 @@ func (c Carbon) Tomorrow(timezone ...string) Carbon {
|
||||
if c.Error != nil {
|
||||
return c
|
||||
}
|
||||
if c.isTestNow {
|
||||
return c.AddDay()
|
||||
if c.HasTestNow() {
|
||||
return CreateFromTimestampNano(c.testNow, c.Location()).AddDay()
|
||||
}
|
||||
if !c.IsZero() {
|
||||
return c.AddDay()
|
||||
@ -59,8 +59,8 @@ func (c Carbon) Yesterday(timezone ...string) Carbon {
|
||||
if c.Error != nil {
|
||||
return c
|
||||
}
|
||||
if c.isTestNow {
|
||||
return c.SubDay()
|
||||
if c.HasTestNow() {
|
||||
return CreateFromTimestampNano(c.testNow, c.Location()).SubDay()
|
||||
}
|
||||
if !c.IsZero() {
|
||||
return c.SubDay()
|
||||
|
Loading…
Reference in New Issue
Block a user