Parcourir la Source

Fix relative times

They now use the proper singular form, and the larger differences have
been fixed as well.
Lucas Stadler 8 ans auparavant
Parent
commit
f46d6120bf
1 fichiers modifiés avec 26 ajouts et 7 suppressions
  1. 26 7
      quit.go

+ 26 - 7
quit.go

@ -381,23 +381,42 @@ type FancyTime struct {
381 381
}
382 382
383 383
func (fc FancyTime) PrettyString() string {
384
	makeForm := func(c int, singular, plural string) string {
385
		form := plural
386
		if c == 1 {
387
			form = singular
388
		}
389
		return fmt.Sprintf("%d %s ago", c, form)
390
	}
384 391
	now := time.Now()
385 392
	diff := time.Since(fc.Time)
386 393
	switch {
387 394
	case diff < time.Minute:
388
		return fmt.Sprintf("%d seconds ago", diff/time.Second)
395
		return makeForm(int(diff/time.Second), "second", "seconds")
389 396
	case diff < time.Hour:
390
		return fmt.Sprintf("%d minutes ago", diff/time.Minute)
397
		return makeForm(int(diff/time.Minute), "minute", "minutes")
391 398
	case now.AddDate(0, 0, -1).Before(fc.Time):
392
		return fmt.Sprintf("%d hours ago", diff/time.Hour)
399
		return makeForm(int(diff/time.Hour), "hour", "hours")
393 400
	case now.AddDate(0, 0, -7).Before(fc.Time):
394
		return fmt.Sprintf("%d days ago", diff/(24*time.Hour))
401
		d := now.Day() - fc.Time.Day()
402
		if d <= 0 {
403
			d += 30
404
		}
405
		return makeForm(d, "day", "days")
395 406
	case now.AddDate(0, -1, 0).Before(fc.Time):
396
		return fmt.Sprintf("%d weeks ago", diff/(7*24*time.Hour))
407
		d := now.Day() - fc.Time.Day()
408
		if d <= 0 {
409
			d += 30
410
		}
411
		return makeForm(d/7, "week", "weeks")
397 412
	case now.AddDate(-1, 0, 0).Before(fc.Time):
398
		return fmt.Sprintf("%d months ago", diff/(30*7*24*time.Hour))
413
		m := now.Month() - fc.Time.Month()
414
		if m <= 0 {
415
			m += 12
416
		}
417
		return makeForm(int(m), "month", "months")
399 418
	default:
400
		return fmt.Sprintf("%d years ago", diff/(365*7*24*time.Hour))
419
		return makeForm(now.Year()-fc.Time.Year(), "year", "years")
401 420
	}
402 421
}
403 422