human_duration

package module
v3.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2024 License: MIT Imports: 5 Imported by: 0

README

Human Duration Build Status GoDoc

A little Go util to print duration strings in a human-friendly format

Docs

The String function takes a Duration and the precision that's important to the user.

The allowed precisions are year, week, day, hour, minute and second

Usage

import "github.com/davidbanham/human_duration"

example := time.Hour * 24 + time.Minute * 4 + time.Second * 8

fmt.Println(human_duration.String(example, "second")) // 1 day 4 minutes 8 seconds
fmt.Println(human_duration.String(example, "minute")) // 1 day 4 minutes
fmt.Println(human_duration.String(example, "day"))    // 1 day
fmt.Println(human_duration.String(example, "year"))   // less than a year

day := time.Hour * 24
year := day * 365

longExample := year * 4 + day * 2

fmt.Println(human_duration.String(longExample, "second")) // 4 years 2 days

There are more examples in the tests.

Credit

Adapted and extended from (this gist)[https://gist.github.com/harshavardhana/327e0577c4fed9211f65]

Documentation

Overview

Package human_duration provides human readable output of time.Duration

Index

Examples

Constants

View Source
const (
	Second = "second"
	Minute = "minute"
	Hour   = "hour"
	Day    = "day"
	Week   = "week"
	Year   = "year"
)

Available precisions

Variables

This section is empty.

Functions

func Ago added in v3.3.0

func Ago(t time.Time) string

func ShortString

func ShortString(duration time.Duration, precision string) string

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

func String(duration time.Duration, precision string) 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 4 weeks 5 days 1 hour 1 minute 1 second

func StringCeiling

func StringCeiling(duration time.Duration, precision, ceiling string) string
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

func StringCeilingPadded(duration time.Duration, precision, ceiling string, padded bool) string

func Timestamp

func Timestamp(interval time.Duration, precision string) string

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL