diff options
author | Ethan Koenig <etk39@cornell.edu> | 2016-12-22 03:58:04 -0500 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2016-12-22 16:58:04 +0800 |
commit | 4c89a9c33c4c097836f5bfa79cc7e5adc142a2f0 (patch) | |
tree | a09bfe9d886ebcd5145a17d15c945b0b14e2bc53 /modules/base/tool.go | |
parent | df7fa4e9959ab23fd94c3475d0860af810936e88 (diff) | |
download | gitea-4c89a9c33c4c097836f5bfa79cc7e5adc142a2f0.tar.gz gitea-4c89a9c33c4c097836f5bfa79cc7e5adc142a2f0.zip |
Bug fixes and tests for modules/base (#442)
Also address other TODOs
Diffstat (limited to 'modules/base/tool.go')
-rw-r--r-- | modules/base/tool.go | 45 |
1 files changed, 31 insertions, 14 deletions
diff --git a/modules/base/tool.go b/modules/base/tool.go index eb25108869..cb2eafd309 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -56,6 +56,9 @@ func DetectEncoding(content []byte) (string, error) { } result, err := chardet.NewTextDetector().DetectBest(content) + if err != nil { + return "", err + } if result.Charset != "UTF-8" && len(setting.Repository.AnsiCharset) > 0 { log.Debug("Using default AnsiCharset: %s", setting.Repository.AnsiCharset) return setting.Repository.AnsiCharset, err @@ -256,19 +259,25 @@ func computeTimeDiff(diff int64) (int64, string) { diffStr = "1 year" default: diffStr = fmt.Sprintf("%d years", diff/Year) - diff = 0 + diff -= (diff / Year) * Year } return diff, diffStr } // TimeSincePro calculates the time interval and generate full user-friendly string. func TimeSincePro(then time.Time) string { - now := time.Now() + return timeSincePro(then, time.Now()) +} + +func timeSincePro(then, now time.Time) string { diff := now.Unix() - then.Unix() if then.After(now) { return "future" } + if diff == 0 { + return "now" + } var timeStr, diffStr string for { @@ -282,9 +291,7 @@ func TimeSincePro(then time.Time) string { return strings.TrimPrefix(timeStr, ", ") } -func timeSince(then time.Time, lang string) string { - now := time.Now() - +func timeSince(then, now time.Time, lang string) string { lbl := i18n.Tr(lang, "tool.ago") diff := now.Unix() - then.Unix() if then.After(now) { @@ -295,7 +302,7 @@ func timeSince(then time.Time, lang string) string { switch { case diff <= 0: return i18n.Tr(lang, "tool.now") - case diff <= 2: + case diff <= 1: return i18n.Tr(lang, "tool.1s", lbl) case diff < 1*Minute: return i18n.Tr(lang, "tool.seconds", diff, lbl) @@ -334,12 +341,18 @@ func timeSince(then time.Time, lang string) string { // RawTimeSince retrieves i18n key of time since t func RawTimeSince(t time.Time, lang string) string { - return timeSince(t, lang) + return timeSince(t, time.Now(), lang) } // TimeSince calculates the time interval and generate user-friendly string. -func TimeSince(t time.Time, lang string) template.HTML { - return template.HTML(fmt.Sprintf(`<span class="time-since" title="%s">%s</span>`, t.Format(setting.TimeFormat), timeSince(t, lang))) +func TimeSince(then time.Time, lang string) template.HTML { + return htmlTimeSince(then, time.Now(), lang) +} + +func htmlTimeSince(then, now time.Time, lang string) template.HTML { + return template.HTML(fmt.Sprintf(`<span class="time-since" title="%s">%s</span>`, + then.Format(setting.TimeFormat), + timeSince(then, now, lang))) } // Storage space size types @@ -424,10 +437,10 @@ func Subtract(left interface{}, right interface{}) interface{} { case int64: rright = right.(int64) case float32: - fright = float64(left.(float32)) + fright = float64(right.(float32)) isInt = false case float64: - fleft = left.(float64) + fright = right.(float64) isInt = false } @@ -459,12 +472,16 @@ func TruncateString(str string, limit int) string { } // StringsToInt64s converts a slice of string to a slice of int64. -func StringsToInt64s(strs []string) []int64 { +func StringsToInt64s(strs []string) ([]int64, error) { ints := make([]int64, len(strs)) for i := range strs { - ints[i] = com.StrTo(strs[i]).MustInt64() + n, err := com.StrTo(strs[i]).Int64() + if err != nil { + return ints, err + } + ints[i] = n } - return ints + return ints, nil } // Int64sToStrings converts a slice of int64 to a slice of string. |