This commit is contained in:
gouguoyin 2021-09-10 14:32:26 +02:00
commit dae8d3922b
5 changed files with 26 additions and 17 deletions

View File

@ -452,6 +452,15 @@ carbon.Parse("2020-08-05 13:14:15").DiffInSeconds(carbon.Parse("2020-08-05 13:14
// Difference in seconds with absolute value
carbon.Parse("2020-08-05 13:14:15").DiffInSecondsWithAbs(carbon.Parse("2020-08-05 13:14:14")) // 1
// Difference in string
carbon.Now().DiffInString() // just now
carbon.Now().AddYearsNoOverflow(1).DiffInString() // -1 year
carbon.Now().SubYearsNoOverflow(1).DiffInString() // 1 year
// Difference in string with absolute value
carbon.Now().DiffInStringWithAbs(carbon.Now()) // just now
carbon.Now().AddYearsNoOverflow(1).DiffInStringWithAbs(carbon.Now()) // 1 year
carbon.Now().SubYearsNoOverflow(1).DiffInStringWithAbs(carbon.Now()) // 1 year
// Difference in human friendly readable format
carbon.Parse("2020-08-05 13:14:15").DiffForHumans() // just now
carbon.Parse("2019-08-05 13:14:15").DiffForHumans() // 1 year ago
@ -719,7 +728,7 @@ carbon.Parse("2020-08-05 13:14:15").Microsecond() // 1596604455000000
// Get current nanosecond
carbon.Parse("2020-08-05 13:14:15").Nanosecond() // 1596604455000000000
// Get timestamp with second, Timestamp() is short for TimestampWithSecond()
// Get timestamp with second, Timestamp() is shorthand for TimestampWithSecond()
carbon.Parse("2020-08-05 13:14:15").Timestamp() // 1596604455
carbon.Parse("2020-08-05 13:14:15").TimestampWithSecond() // 1596604455
// Get timestamp with millisecond
@ -851,13 +860,13 @@ fmt.Sprintf("%s", carbon.Parse("2020-08-05 13:14:15", carbon.Tokyo)) // 2020-08-
carbon.Parse("2020-08-05 13:14:15").ToString() // 2020-08-05 13:14:15 +0800 CST
carbon.Parse("2020-08-05 13:14:15").ToString(carbon.Tokyo) // 2020-08-05 14:14:15 +0900 JST
// Output a string by layout, Layout() is short for ToLayoutString()
// Output a string by layout, Layout() is shorthand for ToLayoutString()
carbon.Parse("2020-08-05 13:14:15").Layout("20060102150405") // 20200805131415
carbon.Parse("2020-08-05 13:14:15").Layout("2006年01月02日 15时04分05秒") // 2020年08月05日 13时14分15秒
carbon.Parse("2020-08-05 13:14:15").Layout("It is 2006-01-02 15:04:05") // It is 2020-08-05 13:14:15
carbon.Parse("2020-08-05 13:14:15").Layout("2006-01-02 15:04:05", carbon.Tokyo) // 2020-08-05 14:14:15
// Output a string by format, Format() is short for ToFormatString()
// Output a string by format, Format() is shorthand for ToFormatString()
carbon.Parse("2020-08-05 13:14:15").Format("YmdHis") // 20200805131415
carbon.Parse("2020-08-05 13:14:15").Format("Y年m月d日 H时i分s秒") // 2020年08月05日 13时14分15秒
carbon.Parse("2020-08-05 13:14:15").Format("l jS \\o\\f F Y h:i:s A") // Wednesday 5th of August 2020 01:14:15 PM

View File

@ -148,7 +148,7 @@ func (l lunar) Animal() string {
return animals[l.year%MonthsPerYear]
}
// Festival get lunar festival name.
// Festival gets lunar festival name.
// 获取农历节日
func (l lunar) Festival() string {
if l.year == 0 {
@ -202,7 +202,7 @@ func (l lunar) LeapMonth() int {
return lunarTerms[l.year-minYear] & 0xf
}
// Day get lunar day.
// Day gets lunar day.
// 获取农历日
func (l lunar) Day() int {
return l.day

View File

@ -213,7 +213,7 @@ func (c Carbon) Nanosecond() int {
return c.time.In(c.loc).Nanosecond()
}
// Timestamp gets timestamp with second, it is short for TimestampWithSecond.
// Timestamp gets timestamp with second, it is shorthand for TimestampWithSecond.
// 获取秒级时间戳, 是 TimestampWithSecond 的简写
func (c Carbon) Timestamp() int64 {
if c.IsInvalid() {

View File

@ -25,7 +25,7 @@ var (
}
)
// Language define a Language struct.
// Language defines a Language struct.
// 定义 Language 结构体
type Language struct {
dir string
@ -34,7 +34,7 @@ type Language struct {
Error error
}
// NewLanguage return a new Language instance.
// NewLanguage returns a new Language instance.
// 初始化 Language 结构体
func NewLanguage() *Language {
return &Language{
@ -76,22 +76,22 @@ func (lang *Language) SetResources(resources map[string]string) {
}
}
// translate translates by unit string.
// translate returns a translated string.
// 翻译转换
func (lang *Language) translate(unit string, vaule int64) string {
func (lang *Language) translate(unit string, value int64) string {
if len(lang.resources) == 0 {
lang.SetLocale(defaultLocale)
}
slice := strings.Split(lang.resources[unit], "|")
number := getAbsValue(vaule)
number := getAbsValue(value)
if len(slice) == 1 {
return strings.Replace(slice[0], "%d", strconv.FormatInt(vaule, 10), 1)
return strings.Replace(slice[0], "%d", strconv.FormatInt(value, 10), 1)
}
if int64(len(slice)) <= number {
return strings.Replace(slice[len(slice)-1], "%d", strconv.FormatInt(vaule, 10), 1)
return strings.Replace(slice[len(slice)-1], "%d", strconv.FormatInt(value, 10), 1)
}
if !strings.Contains(slice[number-1], "%d") && vaule < 0 {
if !strings.Contains(slice[number-1], "%d") && value < 0 {
return "-" + slice[number-1]
}
return strings.Replace(slice[number-1], "%d", strconv.FormatInt(vaule, 10), 1)
return strings.Replace(slice[number-1], "%d", strconv.FormatInt(value, 10), 1)
}

View File

@ -417,7 +417,7 @@ func (c Carbon) ToLayoutString(layout string, timezone ...string) string {
return c.time.In(c.loc).Format(layout)
}
// Layout outputs a string by layout, it is short for ToLayoutString.
// Layout outputs a string by layout, it is shorthand for ToLayoutString.
// 输出指定布局的时间字符串, 是 ToLayoutString 的简写
func (c Carbon) Layout(layout string, timezone ...string) string {
if c.IsInvalid() {
@ -492,7 +492,7 @@ func (c Carbon) ToFormatString(format string, timezone ...string) string {
return buffer.String()
}
// Format outputs a string by format, it is short for ToFormatString.
// Format outputs a string by format, it is shorthand for ToFormatString.
// 输出指定格式的时间字符串, 是 ToFormatString 的简写
func (c Carbon) Format(format string, timezone ...string) string {
if c.IsInvalid() {