Time now handles long ago and a while from now
diff --git a/times.go b/times.go
index ba0a9b8..7720304 100644
--- a/times.go
+++ b/times.go
@@ -7,12 +7,13 @@
// Seconds-based time units
const (
- Minute = 60
- Hour = 60 * Minute
- Day = 24 * Hour
- Week = 7 * Day
- Month = 30 * Day
- Year = 12 * Month
+ Minute = 60
+ Hour = 60 * Minute
+ Day = 24 * Hour
+ Week = 7 * Day
+ Month = 30 * Day
+ Year = 12 * Month
+ LongTime = 37 * Year
)
// Time formats a time into a relative string.
@@ -22,13 +23,14 @@
lbl := "ago"
diff := now.Unix() - then.Unix()
- if then.After(now) {
+
+ after := then.After(now)
+ if after {
lbl = "from now"
diff = then.Unix() - now.Unix()
}
switch {
-
case diff <= 0:
return "now"
case diff <= 2:
@@ -63,6 +65,15 @@
case diff < 18*Month:
return fmt.Sprintf("1 year %s", lbl)
+ case diff < 2*Year:
+ return fmt.Sprintf("2 years %s", lbl)
+ case diff < LongTime:
+ return fmt.Sprintf("%d years %s", diff/Year, lbl)
}
- return then.String()
+
+ if after {
+ return "a while from now"
+ } else {
+ return "long ago"
+ }
}