duration

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2022 License: BSD-3-Clause Imports: 5 Imported by: 4

Documentation

Overview

Package duration implements functions to turn durations into a human-readable format.

For consistency reasons, months are always treated as 30 days, and years are always treated as 365 days.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Days

func Days(d time.Duration) (days int, left time.Duration)

Days returns the number of days in d, and the total time not in a month.

func Format

func Format(d time.Duration) string

Format returns d as a human-readable string.

If d is <1 month, the formats used are day, hour, minute, and second. If d is <1 year, the formats used are month, day, hour, and minute. In all other cases, the formats used are year, month, day, and hour.

func FormatCustom

func FormatCustom(d time.Duration, p Parts, join Joiner) string

FormatCustom is like Format, but allows you to specify your own Parts and Joiner, for localization.

func FormatTime

func FormatTime(t time.Time) string

FormatTime formats t according to format. If t is in the past, returns "<time> ago". If t is in the future, returns "until <time> from now"

func Hours

func Hours(d time.Duration) (hours int, left time.Duration)

Hours returns the number of hours in d, and the total time not in an hour.

func Minutes

func Minutes(d time.Duration) (minutes int, left time.Duration)

Minutes returns the number of minutes in d, and the total time not in an minute.

func Months

func Months(d time.Duration) (months int, left time.Duration)

Months returns the number of months in d, and the total time not in a month.

func Years

func Years(d time.Duration) (years int, left time.Duration)

Years returns the number of years in d, and the total time not in a year.

Types

type Joiner

type Joiner func([]string) string

Joiner is used to join the duration parts into the final string.

type Parts

type Parts struct {
	Years   func(int) string
	Months  func(int) string
	Days    func(int) string
	Hours   func(int) string
	Minutes func(int) string
	Seconds func(int) string
}

Parts are functions to transform a number into a human-readable string.

func EnglishParts

func EnglishParts() Parts

EnglishParts returns a copy of the default Parts for English.

Jump to

Keyboard shortcuts

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