Documentation ¶
Overview ¶
Package human_duration provides human readable output of time.Duration
Index ¶
- Constants
- func ShortString(duration time.Duration, precision string) string
- func String(duration time.Duration, precision string) string
- func StringCeiling(duration time.Duration, precision, ceiling string) string
- func StringCeilingPadded(duration time.Duration, precision, ceiling string, padded bool) string
- func Timestamp(interval time.Duration, precision string) string
Examples ¶
Constants ¶
View Source
const ( Second = "second" Minute = "minute" Hour = "hour" Day = "day" Year = "year" )
Available precisions
Variables ¶
This section is empty.
Functions ¶
func ShortString ¶ added in v1.3.0
ShortString converts duration to a shortened human readable format, according to precision.
Example ¶
day := time.Hour * 24 year := day * 365 duration := 2*year + 2*day + 2*time.Minute + 2*time.Second fmt.Println(ShortString(duration, Second))
Output: 2y2d2m2s
func String ¶
String converts duration to human readable format, according to precision.
Example ¶
duration := time.Hour*24*365 + time.Hour*8 + time.Minute*33 + time.Second*24 fmt.Println(String(duration, Second)) start, _ := time.Parse(time.RFC3339, "2012-11-01T22:08:41+00:00") stop, _ := time.Parse(time.RFC3339, "2013-12-04T23:09:42+00:00") fmt.Println(String(stop.Sub(start), Second))
Output: 1 year 8 hours 33 minutes 24 seconds 1 year 33 days 1 hour 1 minute 1 second
func StringCeiling ¶ added in v1.4.0
Example ¶
duration := time.Hour*24 + time.Hour*2 + time.Minute*33 + time.Second*24 fmt.Println(StringCeiling(duration, Second, Hour))
Output: 26 hours 33 minutes 24 seconds
func StringCeilingPadded ¶ added in v1.4.0
func Timestamp ¶ added in v1.4.0
Timestamp converts duration to a common timestamp format, often used for videos.
Example ¶
duration := (25 * time.Hour) + (20 * time.Minute) + (14 * time.Second) fmt.Println(Timestamp(duration, "second")) fmt.Println(Timestamp(duration, "minute"))
Output: 25:20:14 25:20
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.