From 00ac61a57b4443cbd240a05406e0c75d19dc1aec Mon Sep 17 00:00:00 2001 From: Peleus <245629560@qq.com> Date: Tue, 20 Feb 2024 11:09:54 +0800 Subject: [PATCH] Add `DiffInDuration` and `DiffAbsInDuration` methods --- README.cn.md | 20 ++- README.jp.md | 10 ++ README.md | 20 ++- difference.go | 27 ++++ difference_bench_test.go | 14 +++ difference_unit_test.go | 266 ++++++++++++++++++++++++++------------- test_unit_test.go | 10 +- 7 files changed, 267 insertions(+), 100 deletions(-) diff --git a/README.cn.md b/README.cn.md index 25bb6412..6f41afea 100644 --- a/README.cn.md +++ b/README.cn.md @@ -65,7 +65,7 @@ carbon.SetDefault(carbon.Default{ > 如果没有设置,默认布局模板是 `2006-01-02 15:04:05`, 默认时区是 `Local`, 默认一周开始日期是 `Sunday(周日)`, 默认语言是 `en` -##### Carbon 和 time.Time 互转 +##### `Carbon` 和 `time.Time` 互转 ```go // 将标准 time.Time 转换成 Carbon @@ -142,7 +142,7 @@ carbon.Tomorrow().TimestampMicro() // 1596690855999999 carbon.Tomorrow().TimestampNano() // 1596690855999999999 ``` -##### 创建 Carbon 实例 +##### 创建 `Carbon` 实例 ```go // 从秒级时间戳创建 Carbon 实例 @@ -185,7 +185,7 @@ carbon.CreateFromTimeMicro(13, 14, 15, 999999).ToString() // 2020-08-05 13:14:15 carbon.CreateFromTimeNano(13, 14, 15, 999999999).ToString() // 2020-08-05 13:14:15.999999999 +0800 CST ``` -##### 将时间字符串解析成 Carbon 实例 +##### 将 `时间字符串` 解析成 `Carbon` 实例 ```go carbon.Parse("").ToDateTimeString() // 空字符串 @@ -234,7 +234,7 @@ carbon.Parse("20200805131415.999999999+08:00").ToString() // 2020-08-05 13:14:15 ``` -##### 通过格式模板将时间字符串解析成 Carbon 实例 +##### 通过 `格式模板` 将时间字符串解析成 `Carbon` 实例 > 如果使用的字母与格式模板冲突时,请使用转义符转义该字母 @@ -245,7 +245,7 @@ carbon.ParseByFormat("今天是 2020年08月05日13时14分15秒", "今天是 Y carbon.ParseByFormat("2020-08-05 13:14:15", "Y-m-d H:i:s", carbon.Tokyo).ToDateTimeString() // 2020-08-05 14:14:15 ``` -##### 通过布局模板将时间字符串解析成 Carbon 实例 +##### 通过 `布局模板` 将时间字符串解析成 `Carbon` 实例 ```go carbon.ParseByLayout("2020|08|05 13|14|15", "2006|01|02 15|04|05").ToDateTimeString() // 2020-08-05 13:14:15 @@ -540,6 +540,16 @@ carbon.Now().DiffAbsInString(carbon.Now()) // just now carbon.Now().AddYearsNoOverflow(1).DiffAbsInString(carbon.Now()) // 1 year carbon.Now().SubYearsNoOverflow(1).DiffAbsInString(carbon.Now()) // 1 year +// 相差时长 +now := carbon.Now() +now.DiffInDuration(now).String() // 0s +now.AddHour().DiffInDuration(now).String() // 1h0m0s +now.SubHour().DiffInDuration(now).String() // -1h0m0s +// 相差时长(绝对值) +now.DiffAbsInDuration(now) // 0s +now.AddHour().DiffAbsInDuration(carbon.Now()) // 1h0m0s +now.SubHour().DiffAbsInDuration(carbon.Now()) // 1h0m0s + // 对人类友好的可读格式时间差 carbon.Parse("2020-08-05 13:14:15").DiffForHumans() // just now carbon.Parse("2019-08-05 13:14:15").DiffForHumans() // 1 year ago diff --git a/README.jp.md b/README.jp.md index 83e72395..df7493a6 100644 --- a/README.jp.md +++ b/README.jp.md @@ -540,6 +540,16 @@ carbon.Now().DiffAbsInString(carbon.Now()) // just now carbon.Now().AddYearsNoOverflow(1).DiffAbsInString(carbon.Now()) // 1 year carbon.Now().SubYearsNoOverflow(1).DiffAbsInString(carbon.Now()) // 1 year +// 相差時間長 +now := carbon.Now() +now.DiffInDuration(now).String() // 0s +now.AddHour().DiffInDuration(now).String() // 1h0m0s +now.SubHour().DiffInDuration(now).String() // -1h0m0s +// 相差時間長(絶対値) +now.DiffAbsInDuration(now) // 0s +now.AddHour().DiffAbsInDuration(carbon.Now()) // 1h0m0s +now.SubHour().DiffAbsInDuration(carbon.Now()) // 1h0m0s + // 人間に優しい読み取り可能なフォーマットの時間差を取得します carbon.Parse("2020-08-05 13:14:15").DiffForHumans() // just now carbon.Parse("2019-08-05 13:14:15").DiffForHumans() // 1 year ago diff --git a/README.md b/README.md index 7044dc25..4e23aa23 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ carbon.SetDefault(carbon.Default{ > If not set, the default layout is `2006-01-02 15:04:05`, the default timezone is `Local`, the default week start date is `Sunday` and the default language locale is `en` -##### Convert between Carbon and Time +##### Convert between `Carbon` and `time.Time` ```go // Convert standard Time.time to Carbon @@ -145,7 +145,7 @@ carbon.Tomorrow().TimestampMicro() // 1596690855999999 carbon.Tomorrow().TimestampNano() // 1596690855999999999 ``` -##### Create a Carbon instance +##### Create a `Carbon` instance ```go // Create a Carbon instance from a given timestamp with second @@ -188,7 +188,7 @@ carbon.CreateFromTimeMicro(13, 14, 15, 999999).ToString() // 2020-08-05 13:14:15 carbon.CreateFromTimeNano(13, 14, 15, 999999999).ToString() // 2020-08-05 13:14:15.999999999 +0800 CST ``` -##### Parse a time string as a Carbon instance +##### Parse a time string as a `Carbon` instance ```go carbon.Parse("").ToDateTimeString() // empty string @@ -236,7 +236,7 @@ carbon.Parse("20200805131415.999999+08:00").ToString() // 2020-08-05 13:14:15.99 carbon.Parse("20200805131415.999999999+08:00").ToString() // 2020-08-05 13:14:15.999999999 +0800 CST ``` -##### Parse a time string as a Carbon instance by format +##### Parse a time string as a `Carbon` instance by format ```go carbon.ParseByFormat("2020|08|05 13|14|15", "Y|m|d H|i|s").ToDateTimeString() // 2020-08-05 13:14:15 @@ -244,7 +244,7 @@ carbon.ParseByFormat("It is 2020-08-05 13:14:15", "\\I\\t \\i\\s Y-m-d H:i:s").T carbon.ParseByFormat("今天是 2020年08月05日13时14分15秒", "今天是 Y年m月d日H时i分s秒").ToDateTimeString() // 2020-08-05 13:14:15 ``` -##### Parse a time string as a Carbon instance by layout +##### Parse a time string as a `Carbon` instance by layout ```go carbon.ParseByLayout("2020|08|05 13|14|15", "2006|01|02 15|04|05").ToDateTimeString() // 2020-08-05 13:14:15 @@ -537,6 +537,16 @@ carbon.Now().DiffAbsInString(carbon.Now()) // just now carbon.Now().AddYearsNoOverflow(1).DiffAbsInString(carbon.Now()) // 1 year carbon.Now().SubYearsNoOverflow(1).DiffAbsInString(carbon.Now()) // 1 year +// Difference in duration +now := carbon.Now() +now.DiffInDuration(now).String() // 0s +now.AddHour().DiffInDuration(now).String() // 1h0m0s +now.SubHour().DiffInDuration(now).String() // -1h0m0s +// Difference in duration with absolute value +now.DiffAbsInDuration(now) // 0s +now.AddHour().DiffAbsInDuration(carbon.Now()) // 1h0m0s +now.SubHour().DiffAbsInDuration(carbon.Now()) // 1h0m0s + // Difference in a human-readable format carbon.Parse("2020-08-05 13:14:15").DiffForHumans() // just now carbon.Parse("2019-08-05 13:14:15").DiffForHumans() // 1 year ago diff --git a/difference.go b/difference.go index 4673e40f..9e3acd1c 100755 --- a/difference.go +++ b/difference.go @@ -3,6 +3,7 @@ package carbon import ( "math" "strings" + "time" ) // DiffInYears gets the difference in years. @@ -185,6 +186,32 @@ func (c Carbon) DiffAbsInString(carbon ...Carbon) string { return c.lang.translate(unit, getAbsValue(value)) } +// DiffInDuration gets the difference in duration. +// 相差时长 +func (c Carbon) DiffInDuration(carbon ...Carbon) time.Duration { + end := c.Now() + if c.IsSetTestNow() { + end = CreateFromTimestampNano(c.testNow, c.Location()) + } + if len(carbon) > 0 { + end = carbon[0] + } + return end.StdTime().Sub(c.StdTime()) +} + +// DiffAbsInDuration gets the difference in duration with absolute value. +// 相差时长(绝对值) +func (c Carbon) DiffAbsInDuration(carbon ...Carbon) time.Duration { + end := c.Now() + if c.IsSetTestNow() { + end = CreateFromTimestampNano(c.testNow, c.Location()) + } + if len(carbon) > 0 { + end = carbon[0] + } + return end.StdTime().Sub(c.StdTime()).Abs() +} + // DiffForHumans gets the difference in a human-readable format, i18n is supported. // 获取对人类友好的可读格式时间差,支持i18n func (c Carbon) DiffForHumans(carbon ...Carbon) string { diff --git a/difference_bench_test.go b/difference_bench_test.go index 38b2820b..1e577cea 100755 --- a/difference_bench_test.go +++ b/difference_bench_test.go @@ -114,6 +114,20 @@ func BenchmarkCarbon_DiffAbsInString(b *testing.B) { } } +func BenchmarkCarbon_DiffInDuration(b *testing.B) { + now := Now() + for n := 0; n < b.N; n++ { + now.DiffInDuration(Yesterday()) + } +} + +func BenchmarkCarbon_DiffAbsInDuration(b *testing.B) { + now := Now() + for n := 0; n < b.N; n++ { + now.DiffAbsInDuration(Yesterday()) + } +} + func BenchmarkCarbon_DiffForHumans(b *testing.B) { now := Now() for n := 0; n < b.N; n++ { diff --git a/difference_unit_test.go b/difference_unit_test.go index 662639bf..c0c86566 100755 --- a/difference_unit_test.go +++ b/difference_unit_test.go @@ -358,48 +358,44 @@ func TestCarbon_DiffAbsInSeconds(t *testing.T) { func TestCarbon_DiffInString(t *testing.T) { assert := assert.New(t) + now := Now() tests := []struct { input Carbon expected string }{ 0: {Now(), "just now"}, - 1: {Now().AddYearsNoOverflow(1), "-1 year"}, - 2: {Now().SubYearsNoOverflow(1), "1 year"}, - 3: {Now().AddYearsNoOverflow(10), "-10 years"}, - 4: {Now().SubYearsNoOverflow(10), "10 years"}, + 1: {now.AddYearsNoOverflow(1), "-1 year"}, + 2: {now.SubYearsNoOverflow(1), "1 year"}, + 3: {now.AddYearsNoOverflow(10), "-10 years"}, + 4: {now.SubYearsNoOverflow(10), "10 years"}, - 5: {Now().AddMonthsNoOverflow(1), "-1 month"}, - 6: {Now().SubMonthsNoOverflow(1), "1 month"}, - 7: {Now().AddMonthsNoOverflow(10), "-10 months"}, - 8: {Now().SubMonthsNoOverflow(10), "10 months"}, + 5: {now.AddMonthsNoOverflow(1), "-1 month"}, + 6: {now.SubMonthsNoOverflow(1), "1 month"}, + 7: {now.AddMonthsNoOverflow(10), "-10 months"}, + 8: {now.SubMonthsNoOverflow(10), "10 months"}, - 9: {Now().AddDays(1), "-1 day"}, - 10: {Now().SubDays(1), "1 day"}, - 11: {Now().AddDays(10), "-1 week"}, - 12: {Now().SubDays(10), "1 week"}, + 9: {now.AddDays(1), "-1 day"}, + 10: {now.SubDays(1), "1 day"}, + 11: {now.AddDays(10), "-1 week"}, + 12: {now.SubDays(10), "1 week"}, - 13: {Now().AddHours(1), "-1 hour"}, - 14: {Now().SubHours(1), "1 hour"}, - 15: {Now().AddHours(10), "-10 hours"}, - 16: {Now().SubHours(10), "10 hours"}, + 13: {now.AddHours(1), "-1 hour"}, + 14: {now.SubHours(1), "1 hour"}, + 15: {now.AddHours(10), "-10 hours"}, + 16: {now.SubHours(10), "10 hours"}, - 17: {Now().AddMinutes(1), "-1 minute"}, - 18: {Now().SubMinutes(1), "1 minute"}, - 19: {Now().AddMinutes(10), "-10 minutes"}, - 20: {Now().SubMinutes(10), "10 minutes"}, + 17: {now.AddMinutes(1), "-1 minute"}, + 18: {now.SubMinutes(1), "1 minute"}, + 19: {now.AddMinutes(10), "-10 minutes"}, + 20: {now.SubMinutes(10), "10 minutes"}, - 21: {Now().AddSeconds(1), "-1 second"}, - 22: {Now().SubSeconds(1), "1 second"}, - 23: {Now().AddSeconds(10), "-10 seconds"}, - 24: {Now().SubSeconds(10), "10 seconds"}, + 21: {now.AddSeconds(1), "-1 second"}, + 22: {now.SubSeconds(1), "1 second"}, + 23: {now.AddSeconds(10), "-10 seconds"}, + 24: {now.SubSeconds(10), "10 seconds"}, } - for index, test := range tests { - c := test.input - assert.Nil(c.Error) - assert.Equal(test.expected, c.DiffInString(), "Current test index is "+strconv.Itoa(index)) - } for index, test := range tests { c := test.input assert.Nil(c.Error) @@ -418,47 +414,42 @@ func TestLangError_DiffInString(t *testing.T) { func TestCarbon_DiffAbsInString(t *testing.T) { assert := assert.New(t) + now := Now() tests := []struct { input Carbon expected string }{ 0: {Now(), "just now"}, - 1: {Now().AddYearsNoOverflow(1), "1 year"}, - 2: {Now().SubYearsNoOverflow(1), "1 year"}, - 3: {Now().AddYearsNoOverflow(10), "10 years"}, - 4: {Now().SubYearsNoOverflow(10), "10 years"}, + 1: {now.AddYearsNoOverflow(1), "1 year"}, + 2: {now.SubYearsNoOverflow(1), "1 year"}, + 3: {now.AddYearsNoOverflow(10), "10 years"}, + 4: {now.SubYearsNoOverflow(10), "10 years"}, - 5: {Now().AddMonthsNoOverflow(1), "1 month"}, - 6: {Now().SubMonthsNoOverflow(1), "1 month"}, - 7: {Now().AddMonthsNoOverflow(10), "10 months"}, - 8: {Now().SubMonthsNoOverflow(10), "10 months"}, + 5: {now.AddMonthsNoOverflow(1), "1 month"}, + 6: {now.SubMonthsNoOverflow(1), "1 month"}, + 7: {now.AddMonthsNoOverflow(10), "10 months"}, + 8: {now.SubMonthsNoOverflow(10), "10 months"}, - 9: {Now().AddDays(1), "1 day"}, - 10: {Now().SubDays(1), "1 day"}, - 11: {Now().AddDays(10), "1 week"}, - 12: {Now().SubDays(10), "1 week"}, + 9: {now.AddDays(1), "1 day"}, + 10: {now.SubDays(1), "1 day"}, + 11: {now.AddDays(10), "1 week"}, + 12: {now.SubDays(10), "1 week"}, - 13: {Now().AddHours(1), "1 hour"}, - 14: {Now().SubHours(1), "1 hour"}, - 15: {Now().AddHours(10), "10 hours"}, - 16: {Now().SubHours(10), "10 hours"}, + 13: {now.AddHours(1), "1 hour"}, + 14: {now.SubHours(1), "1 hour"}, + 15: {now.AddHours(10), "10 hours"}, + 16: {now.SubHours(10), "10 hours"}, - 17: {Now().AddMinutes(1), "1 minute"}, - 18: {Now().SubMinutes(1), "1 minute"}, - 19: {Now().AddMinutes(10), "10 minutes"}, - 20: {Now().SubMinutes(10), "10 minutes"}, + 17: {now.AddMinutes(1), "1 minute"}, + 18: {now.SubMinutes(1), "1 minute"}, + 19: {now.AddMinutes(10), "10 minutes"}, + 20: {now.SubMinutes(10), "10 minutes"}, - 21: {Now().AddSeconds(1), "1 second"}, - 22: {Now().SubSeconds(1), "1 second"}, - 23: {Now().AddSeconds(10), "10 seconds"}, - 24: {Now().SubSeconds(10), "10 seconds"}, - } - - for index, test := range tests { - c := test.input - assert.Nil(c.Error) - assert.Equal(test.expected, c.DiffAbsInString(), "Current test index is "+strconv.Itoa(index)) + 21: {now.AddSeconds(1), "1 second"}, + 22: {now.SubSeconds(1), "1 second"}, + 23: {now.AddSeconds(10), "10 seconds"}, + 24: {now.SubSeconds(10), "10 seconds"}, } for index, test := range tests { @@ -472,48 +463,145 @@ func TestLangError_DiffAbsInString(t *testing.T) { lang := NewLanguage() lang.SetLocale("xxx") c := SetLanguage(lang).Now().AddMonths(1) - assert.NotNil(t, c.Error, "It should catch an exception in DiffInStringWithAbs()") + assert.NotNil(t, c.Error, "It should catch an exception in DiffAbsInString()") assert.Equal(t, "", c.DiffAbsInString()) } +func TestCarbon_DiffInDuration(t *testing.T) { + assert := assert.New(t) + + now := Now() + tests := []struct { + input Carbon + expected string + }{ + 0: {now, "0s"}, + + 1: {now.AddYearsNoOverflow(1), "-8784h0m0s"}, + 2: {now.SubYearsNoOverflow(1), "8760h0m0s"}, + 3: {now.AddYearsNoOverflow(10), "-87672h0m0s"}, + 4: {now.SubYearsNoOverflow(10), "87648h0m0s"}, + + 5: {now.AddMonthsNoOverflow(1), "-696h0m0s"}, + 6: {now.SubMonthsNoOverflow(1), "744h0m0s"}, + 7: {now.AddMonthsNoOverflow(10), "-7296h0m0s"}, + 8: {now.SubMonthsNoOverflow(10), "7344h0m0s"}, + + 9: {now.AddDays(1), "-24h0m0s"}, + 10: {now.SubDays(1), "24h0m0s"}, + 11: {now.AddDays(10), "-240h0m0s"}, + 12: {now.SubDays(10), "240h0m0s"}, + + 13: {now.AddHours(1), "-1h0m0s"}, + 14: {now.SubHours(1), "1h0m0s"}, + 15: {now.AddHours(10), "-10h0m0s"}, + 16: {now.SubHours(10), "10h0m0s"}, + + 17: {now.AddMinutes(1), "-1m0s"}, + 18: {now.SubMinutes(1), "1m0s"}, + 19: {now.AddMinutes(10), "-10m0s"}, + 20: {now.SubMinutes(10), "10m0s"}, + + 21: {now.AddSeconds(1), "-1s"}, + 22: {now.SubSeconds(1), "1s"}, + 23: {now.AddSeconds(10), "-10s"}, + 24: {now.SubSeconds(10), "10s"}, + } + + for index, test := range tests { + c := test.input + assert.Nil(c.Error) + assert.Equal(test.expected, c.DiffInDuration(now).String(), "Current test index is "+strconv.Itoa(index)) + } +} + +func TestCarbon_DiffAbsInDuration(t *testing.T) { + assert := assert.New(t) + + now := Now() + tests := []struct { + input Carbon + expected string + }{ + 0: {now, "0s"}, + + 1: {now.AddYearsNoOverflow(1), "8784h0m0s"}, + 2: {now.SubYearsNoOverflow(1), "8760h0m0s"}, + 3: {now.AddYearsNoOverflow(10), "87672h0m0s"}, + 4: {now.SubYearsNoOverflow(10), "87648h0m0s"}, + + 5: {now.AddMonthsNoOverflow(1), "696h0m0s"}, + 6: {now.SubMonthsNoOverflow(1), "744h0m0s"}, + 7: {now.AddMonthsNoOverflow(10), "7296h0m0s"}, + 8: {now.SubMonthsNoOverflow(10), "7344h0m0s"}, + + 9: {now.AddDays(1), "24h0m0s"}, + 10: {now.SubDays(1), "24h0m0s"}, + 11: {now.AddDays(10), "240h0m0s"}, + 12: {now.SubDays(10), "240h0m0s"}, + + 13: {now.AddHours(1), "1h0m0s"}, + 14: {now.SubHours(1), "1h0m0s"}, + 15: {now.AddHours(10), "10h0m0s"}, + 16: {now.SubHours(10), "10h0m0s"}, + + 17: {now.AddMinutes(1), "1m0s"}, + 18: {now.SubMinutes(1), "1m0s"}, + 19: {now.AddMinutes(10), "10m0s"}, + 20: {now.SubMinutes(10), "10m0s"}, + + 21: {now.AddSeconds(1), "1s"}, + 22: {now.SubSeconds(1), "1s"}, + 23: {now.AddSeconds(10), "10s"}, + 24: {now.SubSeconds(10), "10s"}, + } + + for index, test := range tests { + c := test.input + assert.Nil(c.Error) + assert.Equal(test.expected, c.DiffAbsInDuration(now).String(), "Current test index is "+strconv.Itoa(index)) + } +} + func TestCarbon_DiffForHumans(t *testing.T) { assert := assert.New(t) + now := Now() tests := []struct { input Carbon expected string }{ 0: {Now(), "just now"}, - 1: {Now().AddYearsNoOverflow(1), "1 year from now"}, - 2: {Now().SubYearsNoOverflow(1), "1 year ago"}, - 3: {Now().AddYearsNoOverflow(10), "10 years from now"}, - 4: {Now().SubYearsNoOverflow(10), "10 years ago"}, - - 5: {Now().AddMonthsNoOverflow(1), "1 month from now"}, - 6: {Now().SubMonthsNoOverflow(1), "1 month ago"}, - 7: {Now().AddMonthsNoOverflow(10), "10 months from now"}, - 8: {Now().SubMonthsNoOverflow(10), "10 months ago"}, - - 9: {Now().AddDays(1), "1 day from now"}, - 10: {Now().SubDays(1), "1 day ago"}, - 11: {Now().AddDays(10), "1 week from now"}, - 12: {Now().SubDays(10), "1 week ago"}, - - 13: {Now().AddHours(1), "1 hour from now"}, - 14: {Now().SubHours(1), "1 hour ago"}, - 15: {Now().AddHours(10), "10 hours from now"}, - 16: {Now().SubHours(10), "10 hours ago"}, - - 17: {Now().AddMinutes(1), "1 minute from now"}, - 18: {Now().SubMinutes(1), "1 minute ago"}, - 19: {Now().AddMinutes(10), "10 minutes from now"}, - 20: {Now().SubMinutes(10), "10 minutes ago"}, - - 21: {Now().AddSeconds(1), "1 second from now"}, - 22: {Now().SubSeconds(1), "1 second ago"}, - 23: {Now().AddSeconds(10), "10 seconds from now"}, - 24: {Now().SubSeconds(10), "10 seconds ago"}, + 1: {now.AddYearsNoOverflow(1), "1 year from now"}, + 2: {now.SubYearsNoOverflow(1), "1 year ago"}, + 3: {now.AddYearsNoOverflow(10), "10 years from now"}, + 4: {now.SubYearsNoOverflow(10), "10 years ago"}, + + 5: {now.AddMonthsNoOverflow(1), "1 month from now"}, + 6: {now.SubMonthsNoOverflow(1), "1 month ago"}, + 7: {now.AddMonthsNoOverflow(10), "10 months from now"}, + 8: {now.SubMonthsNoOverflow(10), "10 months ago"}, + + 9: {now.AddDays(1), "1 day from now"}, + 10: {now.SubDays(1), "1 day ago"}, + 11: {now.AddDays(10), "1 week from now"}, + 12: {now.SubDays(10), "1 week ago"}, + + 13: {now.AddHours(1), "1 hour from now"}, + 14: {now.SubHours(1), "1 hour ago"}, + 15: {now.AddHours(10), "10 hours from now"}, + 16: {now.SubHours(10), "10 hours ago"}, + + 17: {now.AddMinutes(1), "1 minute from now"}, + 18: {now.SubMinutes(1), "1 minute ago"}, + 19: {now.AddMinutes(10), "10 minutes from now"}, + 20: {now.SubMinutes(10), "10 minutes ago"}, + + 21: {now.AddSeconds(1), "1 second from now"}, + 22: {now.SubSeconds(1), "1 second ago"}, + 23: {now.AddSeconds(10), "10 seconds from now"}, + 24: {now.SubSeconds(10), "10 seconds ago"}, } for index, test := range tests { diff --git a/test_unit_test.go b/test_unit_test.go index bf4331e3..34d373d9 100755 --- a/test_unit_test.go +++ b/test_unit_test.go @@ -16,13 +16,21 @@ func TestCarbon_SetTestNow(t *testing.T) { assert.Equal("2020-08-06", carbon.Tomorrow().ToDateString(), "It should be equal to 2020-08-06") assert.Equal(31, carbon.Parse("1989-08-05").Age(), "It should be equal to 31") assert.Equal(int64(1), carbon.Parse("2020-07-05").DiffInMonths(), "It should be equal to 1") + assert.Equal(int64(1), carbon.Parse("2020-07-05").DiffAbsInMonths(), "It should be equal to 1") assert.Equal(int64(4), carbon.Parse("2020-07-05").DiffInWeeks(), "It should be equal to 4") + assert.Equal(int64(4), carbon.Parse("2020-07-05").DiffAbsInWeeks(), "It should be equal to 4") assert.Equal(int64(31), carbon.Parse("2020-07-05").DiffInDays(), "It should be equal to 31") + assert.Equal(int64(31), carbon.Parse("2020-07-05").DiffAbsInDays(), "It should be equal to 31") assert.Equal(int64(-13), carbon.Parse("2020-08-05 13:14:15").DiffInHours(), "It should be equal to -13") + assert.Equal(int64(13), carbon.Parse("2020-08-05 13:14:15").DiffAbsInHours(), "It should be equal to 13") assert.Equal(int64(-794), carbon.Parse("2020-08-05 13:14:15").DiffInMinutes(), "It should be equal to -794") + assert.Equal(int64(794), carbon.Parse("2020-08-05 13:14:15").DiffAbsInMinutes(), "It should be equal to 794") assert.Equal(int64(-47655), carbon.Parse("2020-08-05 13:14:15").DiffInSeconds(), "It should be equal to -47655") + assert.Equal(int64(47655), carbon.Parse("2020-08-05 13:14:15").DiffAbsInSeconds(), "It should be equal to 47655") assert.Equal("-13 hours", carbon.Parse("2020-08-05 13:14:15").DiffInString(), "It should be -13 hours") - assert.Equal("13 hours", carbon.Parse("2020-08-05 13:14:15").DiffAbsInString(), "It should be -13 hours") + assert.Equal("13 hours", carbon.Parse("2020-08-05 13:14:15").DiffAbsInString(), "It should be 13 hours") + assert.Equal("-13h0m0s", carbon.Parse("2020-08-05 13:00:00").DiffInDuration().String(), "It should be -13h0m0s") + assert.Equal("13h0m0s", carbon.Parse("2020-08-05 13:00:00").DiffAbsInDuration().String(), "It should be 13h0m0s") assert.Equal("13 hours from now", carbon.Parse("2020-08-05 13:14:15").DiffForHumans(), "It should be 13 hours from now") }