mirror of
https://gitee.com/dromara/carbon.git
synced 2024-11-29 18:57:37 +08:00
v1.3.1新增DiffForHumans()方法,并且支持多语言
This commit is contained in:
parent
8cc716d1bd
commit
a15dc8a5b8
14
README.en.md
14
README.en.md
@ -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
|
||||
|
13
README.md
13
README.md
@ -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 月后
|
||||
|
||||
```
|
||||
|
||||
##### 时间比较
|
||||
|
4
final.go
4
final.go
@ -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"]
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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}"
|
||||
}
|
@ -7,6 +7,6 @@
|
||||
"minute":"{count} 分钟",
|
||||
"second":"{count} 秒",
|
||||
"now": "刚刚",
|
||||
"after":"{time}后",
|
||||
"before":"{time}前"
|
||||
"past":"{time}前",
|
||||
"future":"{time}后"
|
||||
}
|
||||
|
@ -7,6 +7,6 @@
|
||||
"minute":"{count} 分鐘",
|
||||
"second":"{count} 秒",
|
||||
"now": "剛剛",
|
||||
"after":"{time}後",
|
||||
"before":"{time}前"
|
||||
"past":"{time}前",
|
||||
"future":"{time}後"
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user