Skip to content

Commit

Permalink
v1.3.1新增DiffForHumans()方法,并且支持多语言
Browse files Browse the repository at this point in the history
  • Loading branch information
gouguoyin committed Feb 8, 2021
1 parent 169f72f commit d3b4a13
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 29 deletions.
14 changes: 8 additions & 6 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,13 +390,15 @@ carbon.Parse("2020-08-05 13:14:15").DiffInSeconds(carbon.Parse("2020-08-05 13:14
carbon.Parse("2020-08-05 13:14:15").DiffInSecondsWithAbs(carbon.Parse("2020-08-05 13:14:14")) // 1

// Difference for humans in English
carbon.Parse("2019-08-05 13:14:15").DiffForHumans()) // 1 years before
carbon.Parse("2018-08-05 13:14:15").DiffForHumans()) // 2 year before
carbon.Parse("2021-08-05 13:14:15").DiffForHumans()) // 1 year after
carbon.Parse("2022-08-05 13:14:15").DiffForHumans()) // 2 years after
carbon.Now().DiffForHumans()) // just now
carbon.Now().SubYears(1).DiffForHumans()) // 1 years ago
carbon.Now().SubYears(2).DiffForHumans()) // 2 year ago
carbon.Now().AddYears(1).DiffForHumans()) // in 1 year
carbon.Now().AddYears(2).DiffForHumans()) // in 2 years
// Difference for humans in Chinese
carbon.Parse("2020-07-05 13:14:15").SetLocale("zh-CN").DiffForHumans()) // 1 月前
carbon.Parse("2020-09-05 13:14:15").SetLocale("zh-CN").DiffForHumans()) // 2 月后
carbon.Now().SetLocale("zh-CN").DiffForHumans()) // 刚刚
carbon.Now().SubMonths(1).SetLocale("zh-CN").DiffForHumans()) // 1 月前
carbon.Now().AddMonths(2).SetLocale("zh-CN").DiffForHumans()) // 2 月后
```

##### Time compare
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,14 +391,15 @@ carbon.Parse("2020-08-05 13:14:15").DiffInSecondsWithAbs(carbon.Parse("2020-08-0

// 对人类友好的可读格式时间差(默认英文)
carbon.Now().DiffForHumans()) // just now
carbon.Parse("2019-08-05 13:14:15").DiffForHumans()) // 1 years before
carbon.Parse("2018-08-05 13:14:15").DiffForHumans()) // 2 year before
carbon.Parse("2021-08-05 13:14:15").DiffForHumans()) // 1 year after
carbon.Parse("2022-08-05 13:14:15").DiffForHumans()) // 2 years after
carbon.Now().SubYears(1).DiffForHumans()) // 1 years ago
carbon.Now().SubYears(2).DiffForHumans()) // 2 year ago
carbon.Now().AddYears(1).DiffForHumans()) // in 1 year
carbon.Now().AddYears(2).DiffForHumans()) // in 2 years
// 对人类友好的可读格式时间差(指定语言)
carbon.Now().SetLocale("zh-CN").DiffForHumans()) // 刚刚
carbon.Parse("2020-07-05 13:14:15").SetLocale("zh-CN").DiffForHumans()) // 1 月前
carbon.Parse("2020-09-05 13:14:15").SetLocale("zh-CN").DiffForHumans()) // 2 月后
carbon.Now().SubMonths(1).SetLocale("zh-CN").DiffForHumans()) // 1 月前
carbon.Now().AddMonths(2).SetLocale("zh-CN").DiffForHumans()) // 2 月后

```

##### 时间比较
Expand Down
4 changes: 2 additions & 2 deletions final.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,10 @@ func (c Carbon) DiffForHumans() string {
c = c.SetLocale(locale)
translation := c.Lang.translate(unit, count)
if c.IsFuture() {
return strings.Replace(c.Lang.resources["after"], "{time}", translation, 1)
return strings.Replace(c.Lang.resources["future"], "{time}", translation, 1)
}
if c.IsPast() {
return strings.Replace(c.Lang.resources["before"], "{time}", translation, 1)
return strings.Replace(c.Lang.resources["past"], "{time}", translation, 1)
}
return c.Lang.resources["now"]
}
Expand Down
18 changes: 9 additions & 9 deletions final_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -976,15 +976,15 @@ func TestCarbon_DiffForHumans1(t *testing.T) {
output string // 期望输出值
}{
{Now().ToDateTimeString(), "just now"},
{Now().AddYears(1).ToDateTimeString(), "1 year after"},
{Now().SubYears(1).ToDateTimeString(), "1 year before"},
{Now().AddYears(10).ToDateTimeString(), "10 years after"},
{Now().SubYears(10).ToDateTimeString(), "10 years before"},

{Now().AddMonths(1).ToDateTimeString(), "1 month after"},
{Now().SubMonths(1).ToDateTimeString(), "1 month before"},
{Now().AddMonths(10).ToDateTimeString(), "10 months after"},
{Now().SubMonths(10).ToDateTimeString(), "10 months before"},
{Now().AddYears(1).ToDateTimeString(), "in 1 year"},
{Now().SubYears(1).ToDateTimeString(), "1 year ago"},
{Now().AddYears(10).ToDateTimeString(), "in 10 years"},
{Now().SubYears(10).ToDateTimeString(), "10 years ago"},

{Now().AddMonths(1).ToDateTimeString(), "in 1 month"},
{Now().SubMonths(1).ToDateTimeString(), "1 month ago"},
{Now().AddMonths(10).ToDateTimeString(), "in 10 months"},
{Now().SubMonths(10).ToDateTimeString(), "10 months ago"},
}

for _, v := range Tests {
Expand Down
4 changes: 2 additions & 2 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"minute":"1 minute|{count} minutes",
"second":"1 second|{count} seconds",
"now": "just now",
"after":"{time} after",
"before":"{time} before"
"past":"{time} ago",
"future":"in {time}"
}
4 changes: 2 additions & 2 deletions lang/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"minute":"{count} 分钟",
"second":"{count} 秒",
"now": "刚刚",
"after":"{time}",
"before":"{time}"
"past":"{time}",
"future":"{time}"
}
4 changes: 2 additions & 2 deletions lang/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"minute":"{count} 分鐘",
"second":"{count} 秒",
"now": "剛剛",
"after":"{time}",
"before":"{time}"
"past":"{time}",
"future":"{time}"
}

0 comments on commit d3b4a13

Please sign in to comment.