MCPcopy Create free account
hub / github.com/cli/cli / FuzzyIn

Function FuzzyIn

internal/text/text.go:54–73  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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".
54func 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.

Callers 5

relativeToNowFunction · 0.92
viewRunFunction · 0.92
listRunFunction · 0.92
createRunFunction · 0.92
TestFuzzyInFunction · 0.85

Calls 3

PluralizeFunction · 0.85
SubMethod · 0.80
MinutesMethod · 0.80

Tested by 1

TestFuzzyInFunction · 0.68