timeago

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2023 License: MIT Imports: 3 Imported by: 3

README

timea.go

Build Status Coverage Status

timea.go (did you see what I did there?) is a simple and 0-dependencies library to print given times in "time ago" manner.

Usage

Get it:

go get github.com/caarlos0/timea.go

Use it:

package main

import (
	"fmt"
	"time"

	timeago "github.com/caarlos0/timea.go"
)

func main() {
	fmt.Println(timeago.Of(time.Now().Add(-5 * time.Second)))
  // 5 seconds ago
}

You may also check the go docs for advanced usage, like custom precision and string templates.

Stargazers over time

Stargazers over time

Documentation

Overview

Package timeago provides a simple library to format a time in a "time ago" manner.

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultFormat = Format{
	ThisSecond: "now",
	LastSecond: "1 second ago",
	SecondsAgo: "%d seconds ago",

	ThisMinute: "now",
	LastMinute: "1 minute ago",
	MinutesAgo: "%d minutes ago",

	ThisHour: "this hour",
	LastHour: "last hour",
	HoursAgo: "%d hours ago",

	Today:     "today",
	Yesterday: "yesterday",
	DaysAgo:   "%d days ago",

	ThisMonth: "this month",
	LastMonth: "last month",
	MonthsAgo: "%d months ago",

	ThisYear: "this year",
	LastYear: "last year",
	YearsAgo: "%d years ago",
}

DefaultFormat is the default format of the string returned by the library.

Functions

func Of

func Of(t time.Time, options ...Options) string

Of returns the string representation of the given time with the given options.

Example
fmt.Println(Of(time.Now().Add(-10 * time.Second)))
Output:

10 seconds ago

Types

type Format

type Format struct {
	ThisSecond string
	LastSecond string
	SecondsAgo string

	ThisMinute string
	LastMinute string
	MinutesAgo string

	ThisHour string
	LastHour string
	HoursAgo string

	Today     string
	Yesterday string
	DaysAgo   string

	ThisMonth string
	LastMonth string
	MonthsAgo string

	ThisYear string
	LastYear string
	YearsAgo string
}

Format is the format of the string returned by the library.

type Options

type Options struct {
	Precision Precision
	Format    Format
}

Options define the options of the library.

type Precision

type Precision uint

Precision define the minimun amount of time to be considered.

const (
	// SecondPrecision is the second precision.
	SecondPrecision Precision = iota

	// MinutePrecision is the minute precision.
	MinutePrecision

	// HourPrecision is the hour precision.
	HourPrecision

	// DayPrecision is the day precision.
	DayPrecision

	// MonthPrecision is the month precision.
	MonthPrecision

	// YearPrecision is the year precision.
	YearPrecision
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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