timeago

package module
v1.2.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: 1

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
	fmt.Println(timeago.Of(time.Now().Add(5 * time.Second))) // 5 seconds from now
}

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{
	SecondsFromNow: "%d seconds from now",
	NextSecond:     "1 second from now",
	ThisSecond:     "now",
	LastSecond:     "1 second ago",
	SecondsAgo:     "%d seconds ago",

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

	HoursFromNow: "%d hours from now",
	NextHour:     "next hour",
	ThisHour:     "this hour",
	LastHour:     "last hour",
	HoursAgo:     "%d hours ago",

	DaysFromNow: "%d days from now",
	Tomorrow:    "tomorrow",
	Today:       "today",
	Yesterday:   "yesterday",
	DaysAgo:     "%d days ago",

	MonthsFromNow: "%d months from now",
	NextMonth:     "next month",
	ThisMonth:     "this month",
	LastMonth:     "last month",
	MonthsAgo:     "%d months ago",

	YearsFromNow: "%d years from now",
	NextYear:     "next year",
	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 {
	SecondsFromNow string
	NextSecond     string
	ThisSecond     string
	SecondsAgo     string
	LastSecond     string

	MinutesFromNow string
	NextMinute     string
	ThisMinute     string
	MinutesAgo     string
	LastMinute     string

	HoursFromNow string
	NextHour     string
	ThisHour     string
	HoursAgo     string
	LastHour     string

	DaysFromNow string
	Tomorrow    string
	Today       string
	Yesterday   string
	DaysAgo     string

	MonthsFromNow string
	NextMonth     string
	ThisMonth     string
	LastMonth     string
	MonthsAgo     string

	YearsFromNow string
	NextYear     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 minimum 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