From 69259933c158de96a9138871e7563913fe6449a3 Mon Sep 17 00:00:00 2001 From: Agung Hariadi Tedja Date: Wed, 3 Jan 2024 16:17:34 +0700 Subject: [PATCH 1/5] feature: support yesterday and tomorrow on en, id, jp, kr, and zh --- README.cn.md | 2 ++ README.jp.md | 2 ++ README.md | 2 ++ difference.go | 6 ++++++ difference_unit_test.go | 38 +++++++++++++++++++------------------- go.mod | 10 ++++++++-- go.sum | 7 ------- lang/en.json | 4 +++- lang/id.json | 4 +++- lang/jp.json | 4 +++- lang/kr.json | 4 +++- lang/zh-CN.json | 4 +++- lang/zh-TW.json | 4 +++- language_bench_test.go | 28 +++++++++++++++------------- language_unit_test.go | 32 +++++++++++++++++--------------- 15 files changed, 89 insertions(+), 62 deletions(-) diff --git a/README.cn.md b/README.cn.md index 86a29b9c..4df5c5a6 100644 --- a/README.cn.md +++ b/README.cn.md @@ -1512,6 +1512,8 @@ resources := map[string]string { "from_now": "in %s", "before": "%s before", "after": "%s after", + "yesterday": "yesterday", + "tomorrow": "tomorrow", } lang.SetResources(resources) diff --git a/README.jp.md b/README.jp.md index 9dea2fa6..4d6b9d36 100644 --- a/README.jp.md +++ b/README.jp.md @@ -1509,6 +1509,8 @@ resources := map[string]string { "from_now": "in %s", "before": "%s before", "after": "%s after", + "yesterday": "yesterday", + "tomorrow": "tomorrow", } lang.SetResources(resources) diff --git a/README.md b/README.md index fd4ac547..2d13f97a 100644 --- a/README.md +++ b/README.md @@ -1512,6 +1512,8 @@ resources := map[string]string { "from_now": "in %s", "before": "%s before", "after": "%s after", + "yesterday": "yesterday", + "tomorrow": "tomorrow", } lang.SetResources(resources) diff --git a/difference.go b/difference.go index 84197cab..36b9d783 100755 --- a/difference.go +++ b/difference.go @@ -206,12 +206,18 @@ func (c Carbon) DiffForHumans(carbon ...Carbon) string { return translation } if c.Lt(end) && len(carbon) == 0 { + if unit == "day" && value == 1 { + return c.lang.resources["yesterday"] + } return strings.Replace(c.lang.resources["ago"], "%s", translation, 1) } if c.Lt(end) && len(carbon) > 0 { return strings.Replace(c.lang.resources["before"], "%s", translation, 1) } if c.Gt(end) && len(carbon) == 0 { + if unit == "day" && value == -1 { + return c.lang.resources["tomorrow"] + } return strings.Replace(c.lang.resources["from_now"], "%s", translation, 1) } return strings.Replace(c.lang.resources["after"], "%s", translation, 1) diff --git a/difference_unit_test.go b/difference_unit_test.go index 45065dc1..95475182 100755 --- a/difference_unit_test.go +++ b/difference_unit_test.go @@ -495,25 +495,25 @@ func TestCarbon_DiffForHumans(t *testing.T) { 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"}, + 9: {Now().AddDays(1), "tomorrow"}, + //10: {Now().SubDays(1), "yesterday"}, + //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/go.mod b/go.mod index 0a59edaa..f328a0b0 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,11 @@ -module github.com/golang-module/carbon/v2 +module github.com/kumparan/carbon/v2 -go 1.16 +go 1.20 require github.com/stretchr/testify v1.8.4 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum index 479781e6..fa4b6e68 100644 --- a/go.sum +++ b/go.sum @@ -1,17 +1,10 @@ -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/lang/en.json b/lang/en.json index cac7b695..6ac35c84 100755 --- a/lang/en.json +++ b/lang/en.json @@ -16,5 +16,7 @@ "ago": "%s ago", "from_now": "%s from now", "before": "%s before", - "after": "%s after" + "after": "%s after", + "yesterday": "yesterday", + "tomorrow": "tomorrow" } \ No newline at end of file diff --git a/lang/id.json b/lang/id.json index f495ba49..41868d43 100755 --- a/lang/id.json +++ b/lang/id.json @@ -16,5 +16,7 @@ "ago": "%s yang lalu", "from_now": "%s dari sekarang", "before": "%s sebelum", - "after": "%s sesudah" + "after": "%s sesudah", + "yesterday": "kemarin", + "tomorrow": "besok" } diff --git a/lang/jp.json b/lang/jp.json index ae872741..adc4dcd8 100755 --- a/lang/jp.json +++ b/lang/jp.json @@ -16,5 +16,7 @@ "ago": "%s前", "from_now": "%s後", "before": "%s前", - "after": "%s後" + "after": "%s後", + "yesterday": "昨日", + "tomorrow": "明日" } diff --git a/lang/kr.json b/lang/kr.json index b7ba9bdb..60d2849d 100755 --- a/lang/kr.json +++ b/lang/kr.json @@ -16,5 +16,7 @@ "ago": "%s앞", "from_now": "%s후", "before": "%s전", - "after": "%s후" + "after": "%s후", + "yesterday": "어제", + "tomorrow": "내일" } diff --git a/lang/zh-CN.json b/lang/zh-CN.json index 283c5893..55483247 100755 --- a/lang/zh-CN.json +++ b/lang/zh-CN.json @@ -16,5 +16,7 @@ "ago": "%s前", "from_now": "%s后", "before": "%s前", - "after": "%s后" + "after": "%s后", + "yesterday": "昨天", + "tomorrow": "明天" } diff --git a/lang/zh-TW.json b/lang/zh-TW.json index 87941cb8..11668b80 100755 --- a/lang/zh-TW.json +++ b/lang/zh-TW.json @@ -16,5 +16,7 @@ "ago": "%s前", "from_now": "%s後", "before": "%s前", - "after": "%s後" + "after": "%s後", + "yesterday": "昨天", + "tomorrow": "明天" } diff --git a/language_bench_test.go b/language_bench_test.go index 15f3ee5c..4ccaf1b4 100755 --- a/language_bench_test.go +++ b/language_bench_test.go @@ -18,19 +18,21 @@ func BenchmarkLanguage_SetLocale(b *testing.B) { func BenchmarkLanguage_SetResources(b *testing.B) { l := NewLanguage() resources := map[string]string{ - "seasons": "spring|summer|autumn|winter", - "year": "1 yr|%d yrs", - "month": "1 mo|%d mos", - "week": "%dw", - "day": "%dd", - "hour": "%dh", - "minute": "%dm", - "second": "%ds", - "now": "just now", - "ago": "%s ago", - "from_now": "in %s", - "before": "%s before", - "after": "%s after", + "seasons": "spring|summer|autumn|winter", + "year": "1 yr|%d yrs", + "month": "1 mo|%d mos", + "week": "%dw", + "day": "%dd", + "hour": "%dh", + "minute": "%dm", + "second": "%ds", + "now": "just now", + "ago": "%s ago", + "from_now": "in %s", + "before": "%s before", + "after": "%s after", + "yesterday": "yesterday", + "tomorrow": "tomorrow", } for n := 0; n < b.N; n++ { l.SetResources(resources) diff --git a/language_unit_test.go b/language_unit_test.go index 19544f1a..5a55c5d4 100755 --- a/language_unit_test.go +++ b/language_unit_test.go @@ -31,19 +31,21 @@ func TestLanguage_SetResources1(t *testing.T) { lang := NewLanguage() resources := map[string]string{ - "seasons": "spring|summer|autumn|winter", - "year": "1 yr|%d yrs", - "month": "1 mo|%d mos", - "week": "%dw", - "day": "%dd", - "hour": "%dh", - "minute": "%dm", - "second": "%ds", - "now": "just now", - "ago": "%s ago", - "from_now": "in %s", - "before": "%s before", - "after": "%s after", + "seasons": "spring|summer|autumn|winter", + "year": "1 yr|%d yrs", + "month": "1 mo|%d mos", + "week": "%dw", + "day": "%dd", + "hour": "%dh", + "minute": "%dm", + "second": "%ds", + "now": "just now", + "ago": "%s ago", + "from_now": "in %s", + "before": "%s before", + "after": "%s after", + "yesterday": "yst", + "tomorrow": "tmr", } lang.SetResources(resources) @@ -63,8 +65,8 @@ func TestLanguage_SetResources1(t *testing.T) { 7: {"2020-08-05 13:14:15", "2021-06-05 13:14:15", "10 mos before"}, 8: {"2020-08-05 13:14:15", "2019-10-05 13:14:15", "10 mos after"}, - 9: {"2020-08-05 13:14:15", "2020-08-06 13:14:15", "1d before"}, - 10: {"2020-08-05 13:14:15", "2020-08-04 13:14:15", "1d after"}, + 9: {"2020-08-05 13:14:15", "2020-08-06 13:14:15", "yst"}, + 10: {"2020-08-05 13:14:15", "2020-08-04 13:14:15", "tmr"}, 11: {"2020-08-05 13:14:15", "2020-08-15 13:14:15", "1w before"}, 12: {"2020-08-05 13:14:15", "2020-07-26 13:14:15", "1w after"}, From 07280f60a18f49d6013a4738763f9128c1e6bb0b Mon Sep 17 00:00:00 2001 From: Agung Hariadi Tedja Date: Wed, 3 Jan 2024 16:24:13 +0700 Subject: [PATCH 2/5] fix test --- .idea/.gitignore | 8 ++++++++ difference_unit_test.go | 38 +++++++++++++++++++------------------- 2 files changed, 27 insertions(+), 19 deletions(-) create mode 100644 .idea/.gitignore diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..73f69e09 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/difference_unit_test.go b/difference_unit_test.go index 95475182..7624d231 100755 --- a/difference_unit_test.go +++ b/difference_unit_test.go @@ -495,25 +495,25 @@ func TestCarbon_DiffForHumans(t *testing.T) { 7: {Now().AddMonthsNoOverflow(10), "10 months from now"}, 8: {Now().SubMonthsNoOverflow(10), "10 months ago"}, - 9: {Now().AddDays(1), "tomorrow"}, - //10: {Now().SubDays(1), "yesterday"}, - //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"}, + 9: {Now().AddDays(1), "tomorrow"}, + 10: {Now().SubDays(1), "yesterday"}, + 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 { From 86f736dbd919ebee91febac3b5000f8bb692aeb3 Mon Sep 17 00:00:00 2001 From: Agung Hariadi Tedja Date: Wed, 3 Jan 2024 16:34:56 +0700 Subject: [PATCH 3/5] fix test --- .idea/carbon.iml | 9 +++++++++ difference.go | 21 +++++++++++++++++++++ language_unit_test.go | 4 ++-- 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 .idea/carbon.iml diff --git a/.idea/carbon.iml b/.idea/carbon.iml new file mode 100644 index 00000000..5e764c4f --- /dev/null +++ b/.idea/carbon.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/difference.go b/difference.go index 36b9d783..46585a32 100755 --- a/difference.go +++ b/difference.go @@ -1,6 +1,7 @@ package carbon import ( + "fmt" "math" "strings" ) @@ -205,6 +206,8 @@ func (c Carbon) DiffForHumans(carbon ...Carbon) string { if unit == "now" { return translation } + fmt.Println("COK", unit, value, c.lang.resources["yesterday"]) + fmt.Println(c.Lt(end), len(carbon)) if c.Lt(end) && len(carbon) == 0 { if unit == "day" && value == 1 { return c.lang.resources["yesterday"] @@ -212,6 +215,15 @@ func (c Carbon) DiffForHumans(carbon ...Carbon) string { return strings.Replace(c.lang.resources["ago"], "%s", translation, 1) } if c.Lt(end) && len(carbon) > 0 { + if unit == "day" { + if value == 1 { + return c.lang.resources["yesterday"] + } + if value == -1 { + return c.lang.resources["tomorrow"] + } + } + return strings.Replace(c.lang.resources["before"], "%s", translation, 1) } if c.Gt(end) && len(carbon) == 0 { @@ -220,6 +232,15 @@ func (c Carbon) DiffForHumans(carbon ...Carbon) string { } return strings.Replace(c.lang.resources["from_now"], "%s", translation, 1) } + if unit == "day" { + if value == 1 { + return c.lang.resources["yesterday"] + } + if value == -1 { + return c.lang.resources["tomorrow"] + } + } + return strings.Replace(c.lang.resources["after"], "%s", translation, 1) } diff --git a/language_unit_test.go b/language_unit_test.go index 5a55c5d4..bfed3349 100755 --- a/language_unit_test.go +++ b/language_unit_test.go @@ -15,8 +15,8 @@ func TestLanguage_SetLocale(t *testing.T) { locale string expected string }{ - 0: {Now(), "en", "1 day after"}, - 1: {Tomorrow(), "zh-CN", "1 天后"}, + 0: {Now(), "en", "tomorrow"}, + 1: {Tomorrow(), "zh-CN", "明天"}, } for index, test := range tests { From 5adf9af63fb0142fb2ef1d3fc02595879759856c Mon Sep 17 00:00:00 2001 From: Agung Hariadi Tedja Date: Wed, 3 Jan 2024 16:57:12 +0700 Subject: [PATCH 4/5] cok --- .idea/modules.xml | 8 ++++++++ difference.go | 3 --- 2 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 .idea/modules.xml diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..3ac289d3 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/difference.go b/difference.go index 46585a32..6a1be877 100755 --- a/difference.go +++ b/difference.go @@ -1,7 +1,6 @@ package carbon import ( - "fmt" "math" "strings" ) @@ -206,8 +205,6 @@ func (c Carbon) DiffForHumans(carbon ...Carbon) string { if unit == "now" { return translation } - fmt.Println("COK", unit, value, c.lang.resources["yesterday"]) - fmt.Println(c.Lt(end), len(carbon)) if c.Lt(end) && len(carbon) == 0 { if unit == "day" && value == 1 { return c.lang.resources["yesterday"] From 2e13a3fba17958f5e9976a9122c5e3201769e0e7 Mon Sep 17 00:00:00 2001 From: "na.luthfi" Date: Thu, 4 Jan 2024 10:08:44 +0700 Subject: [PATCH 5/5] refactor: rename --- lang/id.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lang/id.json b/lang/id.json index 41868d43..493528c2 100755 --- a/lang/id.json +++ b/lang/id.json @@ -16,7 +16,7 @@ "ago": "%s yang lalu", "from_now": "%s dari sekarang", "before": "%s sebelum", - "after": "%s sesudah", + "after": "%s lagi", "yesterday": "kemarin", "tomorrow": "besok" }