ago

package
v0.4.11 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Day   time.Duration = time.Hour * 24
	Month time.Duration = Day * 30
	Year  time.Duration = Day * 365
)

Variables

View Source
var Calculate = Ago{
	PastPrefix:   "",
	PastSuffix:   " ago",
	FuturePrefix: "in ",
	FutureSuffix: "",

	Periods: []FormatPeriod{
		{time.Second, "about a second", "%d seconds"},
		{time.Minute, "about a minute", "%d minutes"},
		{time.Hour, "about an hour", "%d hours"},
		{Day, "one day", "%d days"},
		{Month, "one month", "%d months"},
		{Year, "one year", "%d years"},
	},

	Zero: "about a second",

	Max:           73 * time.Hour,
	DefaultLayout: "2006-01-02",
}

Predefined english configuration

Functions

This section is empty.

Types

type Ago

type Ago struct {
	PastPrefix   string
	PastSuffix   string
	FuturePrefix string
	FutureSuffix string

	Periods []FormatPeriod

	Zero string
	Max  time.Duration // Maximum duration for using the special formatting.
	//DefaultLayout is used if delta is greater than the minimum of last period
	//in Periods and Max. It is the desired representation of the date 2nd of
	// January 2006.
	DefaultLayout string
}

Config allows the customization of timeago. You may configure string items (language, plurals, ...) and maximum allowed duration value for fuzzy formatting.

func NoMax

func NoMax(a Ago) Ago

NoMax creates an new config without a maximum

func WithMax

func WithMax(a Ago, max time.Duration, defaultLayout string) Ago

WithMax creates an new config with special formatting limited to durations less than max. Values greater than max will be formatted by the standard time package using the defaultLayout.

func (Ago) Format

func (a Ago) Format(t time.Time) string

Format returns a textual representation of the time value formatted according to the layout defined in the Config. The time is compared to time.Now() and is then formatted as a fuzzy timestamp (eg. "4 days ago")

func (Ago) FormatReference

func (a Ago) FormatReference(t time.Time, reference time.Time) string

FormatReference is the same as Format, but the reference has to be defined by the caller

func (Ago) FormatRelativeDuration

func (a Ago) FormatRelativeDuration(d time.Duration) string

FormatRelativeDuration is the same as Format, but for time.Duration. Config.Max is not used in this function, as there is no other alternative.

type FormatPeriod

type FormatPeriod struct {
	D    time.Duration
	One  string
	Many string
}

Jump to

Keyboard shortcuts

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