go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/chirp/pkg/controller/viewfuncs.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package controller 9 10 import ( 11 "fmt" 12 "time" 13 ) 14 15 func viewfuncAge(t time.Time) string { 16 duration := time.Now().UTC().Sub(t) 17 if duration > 24*time.Hour { 18 days := duration / (time.Duration(24) * time.Hour) 19 return fmt.Sprintf("%d days", days) 20 } 21 if duration > time.Hour { 22 hours := duration / time.Hour 23 return fmt.Sprintf("%dh", hours) 24 } 25 if duration > time.Minute { 26 minutes := duration / time.Minute 27 return fmt.Sprintf("%dm", minutes) 28 } 29 seconds := duration / time.Second 30 return fmt.Sprintf("%ds", seconds) 31 }