Skip to content

Commit

Permalink
Merge pull request #3 from kumparan/my-branch
Browse files Browse the repository at this point in the history
feature: support yesterday and tomorrow on en, id, jp, kr, and zh
  • Loading branch information
naluthfi authored Jan 4, 2024
2 parents f4ff556 + 2e13a3f commit b4776e2
Show file tree
Hide file tree
Showing 18 changed files with 117 additions and 47 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/carbon.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions README.cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions README.jp.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,8 @@ resources := map[string]string {
"from_now": "in %s",
"before": "%s before",
"after": "%s after",
"yesterday": "yesterday",
"tomorrow": "tomorrow",
}
lang.SetResources(resources)

Expand Down
24 changes: 24 additions & 0 deletions difference.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,38 @@ 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 {
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 {
if unit == "day" && value == -1 {
return c.lang.resources["tomorrow"]
}
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)
}

Expand Down
4 changes: 2 additions & 2 deletions difference_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,8 @@ 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"},
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"},

Expand Down
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
7 changes: 0 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
4 changes: 3 additions & 1 deletion lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
4 changes: 3 additions & 1 deletion lang/id.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@
"ago": "%s yang lalu",
"from_now": "%s dari sekarang",
"before": "%s sebelum",
"after": "%s sesudah"
"after": "%s lagi",
"yesterday": "kemarin",
"tomorrow": "besok"
}
4 changes: 3 additions & 1 deletion lang/jp.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@
"ago": "%s前",
"from_now": "%s後",
"before": "%s前",
"after": "%s後"
"after": "%s後",
"yesterday": "昨日",
"tomorrow": "明日"
}
4 changes: 3 additions & 1 deletion lang/kr.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@
"ago": "%s앞",
"from_now": "%s후",
"before": "%s전",
"after": "%s후"
"after": "%s후",
"yesterday": "어제",
"tomorrow": "내일"
}
4 changes: 3 additions & 1 deletion lang/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@
"ago": "%s前",
"from_now": "%s后",
"before": "%s前",
"after": "%s后"
"after": "%s后",
"yesterday": "昨天",
"tomorrow": "明天"
}
4 changes: 3 additions & 1 deletion lang/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@
"ago": "%s前",
"from_now": "%s後",
"before": "%s前",
"after": "%s後"
"after": "%s後",
"yesterday": "昨天",
"tomorrow": "明天"
}
28 changes: 15 additions & 13 deletions language_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 19 additions & 17 deletions language_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)

Expand All @@ -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"},

Expand Down

0 comments on commit b4776e2

Please sign in to comment.