Last active
June 19, 2022 23:02
-
-
Save emad-elsaid/cc026f69d1d73cdb9d87eb3e8931c7d1 to your computer and use it in GitHub Desktop.
Revisions
-
emad-elsaid revised this gist
Jun 19, 2022 . 1 changed file with 3 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,24 +1,15 @@ func ago(t time.Duration) (o string) { const day = time.Hour * 24 const week = day * 7 const month = day * 30 const year = day * 365 const maxPrecision = 2 if t.Seconds() < 1 { return "seconds ago" } for precision := 0; t.Seconds() > 0 && precision < maxPrecision; precision++ { switch { case t >= year: years := t / year -
emad-elsaid revised this gist
Jun 19, 2022 . No changes.There are no files selected for viewing
-
emad-elsaid revised this gist
Jun 19, 2022 . No changes.There are no files selected for viewing
-
emad-elsaid revised this gist
Jun 19, 2022 . No changes.There are no files selected for viewing
-
emad-elsaid created this gist
Jun 19, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,55 @@ func ago(t time.Duration) string { o := "" t = t.Round(time.Second) const day = time.Hour * 24 const week = day * 7 const month = day * 30 const year = day * 365 const maxPrecision = 2 precision := 0 if t.Seconds() == 0 { return "seconds ago" } for t.Seconds() > 0 { if precision >= maxPrecision { break } precision++ switch { case t >= year: years := t / year t -= years * year o += fmt.Sprintf("%d years ", years) case t >= month: months := t / month t -= months * month o += fmt.Sprintf("%d months ", months) case t >= week: weeks := t / week t -= weeks * week o += fmt.Sprintf("%d weeks ", weeks) case t >= day: days := t / day t -= days * day o += fmt.Sprintf("%d days ", days) case t >= time.Hour: hours := t / time.Hour t -= hours * time.Hour o += fmt.Sprintf("%d hours ", hours) case t >= time.Minute: minutes := t / time.Minute t -= minutes * time.Minute o += fmt.Sprintf("%d minutes ", minutes) case t >= time.Second: seconds := t / time.Second t -= seconds * time.Second o += fmt.Sprintf("%d seconds ", seconds) } } return o + "ago" }