[DEPRECATED] Go implementation of plcbundle
at rust-test 43 lines 897 B view raw
1package bundle 2 3import ( 4 "fmt" 5 "time" 6) 7 8// formatTimeDistance formats a duration as "X ago" or "live" 9func formatTimeDistance(d time.Duration) string { 10 if d < 10*time.Second { 11 return "live" 12 } 13 if d < time.Minute { 14 return fmt.Sprintf("%ds ago", int(d.Seconds())) 15 } 16 if d < time.Hour { 17 return fmt.Sprintf("%dm ago", int(d.Minutes())) 18 } 19 if d < 24*time.Hour { 20 hours := int(d.Hours()) 21 mins := int(d.Minutes()) % 60 22 if mins > 0 { 23 return fmt.Sprintf("%dh%dm ago", hours, mins) 24 } 25 return fmt.Sprintf("%dh ago", hours) 26 } 27 days := int(d.Hours() / 24) 28 if days == 1 { 29 return "1 day ago" 30 } 31 if days < 7 { 32 return fmt.Sprintf("%d days ago", days) 33 } 34 weeks := days / 7 35 if weeks < 4 { 36 return fmt.Sprintf("%d weeks ago", weeks) 37 } 38 months := days / 30 39 if months < 12 { 40 return fmt.Sprintf("%d months ago", months) 41 } 42 return fmt.Sprintf("%.1f years ago", float64(days)/365) 43}