Skip to content

Commit

Permalink
2.1.8 unified private function comment format
Browse files Browse the repository at this point in the history
  • Loading branch information
kuafuRace committed May 21, 2022
1 parent cf73766 commit 4bb54d1
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 26 deletions.
2 changes: 1 addition & 1 deletion difference.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (c Carbon) DiffForHumans(carbon ...Carbon) string {
return strings.Replace(c.lang.resources["after"], "%s", translation, 1)
}

// diff gets the difference for unit and value.
// gets the difference for unit and value.
// 获取相差单位和差值
func (c Carbon) diff(end Carbon) (unit string, value int64) {
switch true {
Expand Down
12 changes: 6 additions & 6 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@ import (
"fmt"
)

// invalidTimezoneError returns an invalid timezone error.
// returns an invalid timezone error.
// 无效的时区错误
var invalidTimezoneError = func(timezone string) error {
return fmt.Errorf("invalid timezone %q, please see the file %q for all valid timezones", timezone, "$GOROOT/lib/time/zoneinfo.zip")
}

// invalidDurationError returns an invalid duration error.
// 无效的持续时长错误
// returns an invalid duration error.
// 无效的时长错误
var invalidDurationError = func(duration string) error {
return fmt.Errorf("invalid duration %q, please make sure the duration is valid", duration)
}

// invalidValueError returns an invalid value error.
// returns an invalid value error.
// 无效的时间字符串错误
var invalidValueError = func(value string) error {
return fmt.Errorf("cannot parse %q as carbon, please make sure the value is valid", value)
}

// invalidLayoutError returns an invalid layout error.
// returns an invalid layout error.
// 无效的布局模板错误
var invalidLayoutError = func(value, layout string) error {
return fmt.Errorf("cannot parse %q as carbon by layout %q, please make sure the value and layout match", value, layout)
}

// invalidFormatError returns an invalid format error.
// returns an invalid format error.
// 无效的格式模板错误
var invalidFormatError = func(value, format string) error {
return fmt.Errorf("cannot parse %q as carbon by format %q, please make sure the value and format match", value, format)
Expand Down
14 changes: 7 additions & 7 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"
)

// formats common formatting symbols
// common formatting symbols
// 常规格式化符号
var formats = map[byte]string{
'd': "02", // Day: Day of the month, 2 digits with leading zeros. Eg: 01 to 31.
Expand All @@ -32,7 +32,7 @@ var formats = map[byte]string{
'r': "Thu, 21 Dec 2000 16:01:07 +0200", // Format: RFC 2822 formatted date. Eg: Thu, 21 Dec 2000 16:01:07 +0200.
}

// format2layout converts format to layout.
// converts format to layout.
// format 转 layout
func format2layout(format string) string {
buffer := bytes.NewBuffer(nil)
Expand All @@ -41,7 +41,7 @@ func format2layout(format string) string {
buffer.WriteString(layout)
} else {
switch format[i] {
case '\\': // 原样输出,不解析
case '\\': // raw output, no parse
buffer.WriteByte(format[i+1])
i++
continue
Expand All @@ -53,7 +53,7 @@ func format2layout(format string) string {
return buffer.String()
}

// getLocationByTimezone gets a Location instance by a timezone string.
// gets a Location instance by a timezone string.
// 通过时区获取 Location 实例
func getLocationByTimezone(timezone string) (*time.Location, error) {
loc, err := time.LoadLocation(timezone)
Expand All @@ -63,8 +63,8 @@ func getLocationByTimezone(timezone string) (*time.Location, error) {
return loc, err
}

// parseByDuration parses as a Duration instance by a duration string.
// 通过持续时长解析
// parses as a Duration instance by a duration string.
// 通过时长解析
func parseByDuration(duration string) (time.Duration, error) {
td, err := time.ParseDuration(duration)
if err != nil {
Expand All @@ -73,7 +73,7 @@ func parseByDuration(duration string) (time.Duration, error) {
return td, err
}

// getAbsValue gets absolute value.
// gets absolute value.
// 获取绝对值
func getAbsValue(value int64) int64 {
return (value ^ value>>31) - value>>31
Expand Down
8 changes: 4 additions & 4 deletions language.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (
var fs embed.FS

var (
// defaultDir default directory
// default directory
defaultDir = "lang"

// defaultLocale default locale
// default locale
defaultLocale = "en"

// invalidLocaleError returns an invalid locale error.
// returns an invalid locale error.
invalidLocaleError = func(locale string) error {
return fmt.Errorf("invalid locale file %q, please make sure the json file exists and is valid", locale)
}
Expand Down Expand Up @@ -76,7 +76,7 @@ func (lang *Language) SetResources(resources map[string]string) {
}
}

// translate returns a translated string.
// returns a translated string.
// 翻译转换
func (lang *Language) translate(unit string, value int64) string {
if len(lang.resources) == 0 {
Expand Down
8 changes: 4 additions & 4 deletions language_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ func TestLanguage_SetLocale(t *testing.T) {
assert := assert.New(t)

tests := []struct {
input1 Carbon // 输入值
input2 string // 输入值
input Carbon // 输入值
locale string // 输入值
expected string // 期望值
}{
{Now(), "en", "1 day after"},
Expand All @@ -21,8 +21,8 @@ func TestLanguage_SetLocale(t *testing.T) {

for index, test := range tests {
lang := NewLanguage()
lang.SetLocale(test.input2)
assert.Equal(test.expected, (test.input1).AddDays(1).SetLanguage(lang).DiffForHumans(test.input1), "Current test index is "+strconv.Itoa(index))
lang.SetLocale(test.locale)
assert.Equal(test.expected, (test.input).AddDays(1).SetLanguage(lang).DiffForHumans(test.input), "Current test index is "+strconv.Itoa(index))
}
}

Expand Down
7 changes: 3 additions & 4 deletions traveler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ import (
)

// AddDuration adds one duration.
// 按照持续时长字符串增加时间,支持整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合
// 按照时长增加时间,支持整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合
func (c Carbon) AddDuration(duration string) Carbon {
if c.IsInvalid() {
return c
}
td, err := parseByDuration(duration)
c.time = c.Carbon2Time().Add(td)
c.Error = err
c.time, c.Error = c.Carbon2Time().Add(td), err
return c
}

// SubDuration subtracts one duration.
// 按照持续时长字符串减少时间,支持整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合
// 按照时长减少时间,支持整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合
func (c Carbon) SubDuration(duration string) Carbon {
return c.AddDuration("-" + duration)
}
Expand Down

0 comments on commit 4bb54d1

Please sign in to comment.