FuzzyIn returns a human readable description of how far b lies in the future relative to a, such as "in about 7 hours". Each unit is rounded to the nearest whole value rather than truncated, so a duration a hair under a round boundary (for example a 6 month lifetime a second after issuance) is not r
(a, b time.Time)
| 52 | // boundary (for example a 6 month lifetime a second after issuance) is not reported one unit short. A value that |
| 53 | // rounds up to the next unit's threshold is reported in that larger unit instead, so it never reads "60 minutes". |
| 54 | func FuzzyIn(a, b time.Time) string { |
| 55 | d := b.Sub(a) |
| 56 | if d < time.Minute { |
| 57 | return "in less than a minute" |
| 58 | } |
| 59 | if minutes := int(math.Round(d.Minutes())); minutes < 60 { |
| 60 | return fmt.Sprintf("in about %s", Pluralize(minutes, "minute")) |
| 61 | } |
| 62 | if hours := int(math.Round(d.Hours())); hours < 24 { |
| 63 | return fmt.Sprintf("in about %s", Pluralize(hours, "hour")) |
| 64 | } |
| 65 | if days := int(math.Round(d.Hours() / 24)); days < 30 { |
| 66 | return fmt.Sprintf("in about %s", Pluralize(days, "day")) |
| 67 | } |
| 68 | if months := int(math.Round(d.Hours() / 24 / 30)); months < 12 { |
| 69 | return fmt.Sprintf("in about %s", Pluralize(months, "month")) |
| 70 | } |
| 71 | years := max(int(math.Round(d.Hours()/24/365)), 1) |
| 72 | return fmt.Sprintf("in about %s", Pluralize(years, "year")) |
| 73 | } |
| 74 | |
| 75 | // FuzzyAgoAbbr is an abbreviated version of FuzzyAgo. It returns a human readable string of the |
| 76 | // time duration between a and b that is estimated to the nearest unit of time. |