@@ -56,6 +56,9 @@ func DetectEncoding(content []byte) (string, error) {
56
56
}
57
57
58
58
result , err := chardet .NewTextDetector ().DetectBest (content )
59
+ if err != nil {
60
+ return "" , err
61
+ }
59
62
if result .Charset != "UTF-8" && len (setting .Repository .AnsiCharset ) > 0 {
60
63
log .Debug ("Using default AnsiCharset: %s" , setting .Repository .AnsiCharset )
61
64
return setting .Repository .AnsiCharset , err
@@ -256,19 +259,25 @@ func computeTimeDiff(diff int64) (int64, string) {
256
259
diffStr = "1 year"
257
260
default :
258
261
diffStr = fmt .Sprintf ("%d years" , diff / Year )
259
- diff = 0
262
+ diff -= ( diff / Year ) * Year
260
263
}
261
264
return diff , diffStr
262
265
}
263
266
264
267
// TimeSincePro calculates the time interval and generate full user-friendly string.
265
268
func TimeSincePro (then time.Time ) string {
266
- now := time .Now ()
269
+ return timeSincePro (then , time .Now ())
270
+ }
271
+
272
+ func timeSincePro (then , now time.Time ) string {
267
273
diff := now .Unix () - then .Unix ()
268
274
269
275
if then .After (now ) {
270
276
return "future"
271
277
}
278
+ if diff == 0 {
279
+ return "now"
280
+ }
272
281
273
282
var timeStr , diffStr string
274
283
for {
@@ -282,9 +291,7 @@ func TimeSincePro(then time.Time) string {
282
291
return strings .TrimPrefix (timeStr , ", " )
283
292
}
284
293
285
- func timeSince (then time.Time , lang string ) string {
286
- now := time .Now ()
287
-
294
+ func timeSince (then , now time.Time , lang string ) string {
288
295
lbl := i18n .Tr (lang , "tool.ago" )
289
296
diff := now .Unix () - then .Unix ()
290
297
if then .After (now ) {
@@ -295,7 +302,7 @@ func timeSince(then time.Time, lang string) string {
295
302
switch {
296
303
case diff <= 0 :
297
304
return i18n .Tr (lang , "tool.now" )
298
- case diff <= 2 :
305
+ case diff <= 1 :
299
306
return i18n .Tr (lang , "tool.1s" , lbl )
300
307
case diff < 1 * Minute :
301
308
return i18n .Tr (lang , "tool.seconds" , diff , lbl )
@@ -334,12 +341,18 @@ func timeSince(then time.Time, lang string) string {
334
341
335
342
// RawTimeSince retrieves i18n key of time since t
336
343
func RawTimeSince (t time.Time , lang string ) string {
337
- return timeSince (t , lang )
344
+ return timeSince (t , time . Now (), lang )
338
345
}
339
346
340
347
// TimeSince calculates the time interval and generate user-friendly string.
341
- func TimeSince (t time.Time , lang string ) template.HTML {
342
- return template .HTML (fmt .Sprintf (`<span class="time-since" title="%s">%s</span>` , t .Format (setting .TimeFormat ), timeSince (t , lang )))
348
+ func TimeSince (then time.Time , lang string ) template.HTML {
349
+ return htmlTimeSince (then , time .Now (), lang )
350
+ }
351
+
352
+ func htmlTimeSince (then , now time.Time , lang string ) template.HTML {
353
+ return template .HTML (fmt .Sprintf (`<span class="time-since" title="%s">%s</span>` ,
354
+ then .Format (setting .TimeFormat ),
355
+ timeSince (then , now , lang )))
343
356
}
344
357
345
358
// Storage space size types
@@ -424,10 +437,10 @@ func Subtract(left interface{}, right interface{}) interface{} {
424
437
case int64 :
425
438
rright = right .(int64 )
426
439
case float32 :
427
- fright = float64 (left .(float32 ))
440
+ fright = float64 (right .(float32 ))
428
441
isInt = false
429
442
case float64 :
430
- fleft = left .(float64 )
443
+ fright = right .(float64 )
431
444
isInt = false
432
445
}
433
446
@@ -459,12 +472,16 @@ func TruncateString(str string, limit int) string {
459
472
}
460
473
461
474
// StringsToInt64s converts a slice of string to a slice of int64.
462
- func StringsToInt64s (strs []string ) []int64 {
475
+ func StringsToInt64s (strs []string ) ( []int64 , error ) {
463
476
ints := make ([]int64 , len (strs ))
464
477
for i := range strs {
465
- ints [i ] = com .StrTo (strs [i ]).MustInt64 ()
478
+ n , err := com .StrTo (strs [i ]).Int64 ()
479
+ if err != nil {
480
+ return ints , err
481
+ }
482
+ ints [i ] = n
466
483
}
467
- return ints
484
+ return ints , nil
468
485
}
469
486
470
487
// Int64sToStrings converts a slice of int64 to a slice of string.
0 commit comments