v1.3.1新增DiffForHumans()方法,并且支持多语言

This commit is contained in:
gouguoyin 2021-02-08 10:12:55 +08:00
parent 8cc716d1bd
commit a15dc8a5b8
7 changed files with 31 additions and 28 deletions

View File

@ -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

View File

@ -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 月后
```
##### 时间比较

View File

@ -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"]
}

View File

@ -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().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(), "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().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 {

View File

@ -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}"
}

View File

@ -7,6 +7,6 @@
"minute":"{count} 分钟",
"second":"{count} 秒",
"now": "刚刚",
"after":"{time}后",
"before":"{time}前"
"past":"{time}前",
"future":"{time}后"
}

View File

@ -7,6 +7,6 @@
"minute":"{count} 分鐘",
"second":"{count} 秒",
"now": "剛剛",
"after":"{time}後",
"before":"{time}前"
"past":"{time}前",
"future":"{time}後"
}