timeago

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2022 License: MIT Imports: 6 Imported by: 4

README

Code Coverage Build Status Build Status Scrutinizer Code Quality Go Report Card GitHub

100% test covered, fast and lightweight datetime converter that converts given datetime into "n time ago" format from [YEAR-MONTH-DAY HOURS:MINUTES:SECONDS]. Supports Russian and English languages.

Example usage on repl.it

Configurations

Language

Default language is English. Optionally you can set the language in your application by calling timeago.Set() function and pass the flag "ru" or "en" in your init function.

func init() {
    timeago.Set("language", "ru")
}
Location

Default location is Europe/Kiev. Optionally you can set the location in your application by calling timeago.Set() function and pass the location you need in your init function.

func init() {
    timeago.Set("location", "America/New_York")
}

Please make sure that timezone configuration is correct for your location. It is very important for displaying the correct datetime.

Usage

For outputting post publishing date or something else you can just pass the date to method timeago.Take(). It will count the interval between now and given date and returns converted format.

timeago.Take("2019-10-23 10:46:00") // after 10 seconds outputs: 10 seconds ago

If you want to show last user login like if user is online or not, you can optionally add |online to the datetime string. All it does is just displaying Online if date interval withing 60 seconds.

timeago.Take("2019-10-23 10:46:00|online")

Quick Start

go get -u github.com/SerhiiCho/timeago

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultTimeFormat = "2006-01-02 15:04:05"
View Source
var ErrUnsupported = errors.New(`Unsupported`)

Functions

func RegisterRules added in v1.2.0

func RegisterRules(lang string, rule Rule)

func RegisterTranslations

func RegisterTranslations(lang string, trans map[string]string, rule ...Rule)

func Set

func Set(paramName string, value string) error

Set sets configurations parameters to given value paramName is the name of the parameter, can be `language` or `location`. Seconds parameter is value of the configuration. For parameter `language` can be `ru` or `en`

func Take

func Take(datetime string, langs ...string) string

Take coverts given datetime into `x time ago` format. For displaying `Online` word if date interval within 60 seconds, add `|online` flag to the datetime string. or add `|online|2006-01-02 15:04:05` flag to the datetime string. Format must be [year-month-day hours:minutes:seconds}

func Timestamp

func Timestamp(timestamp int64, langAndOptions ...string) string

Types

type Detector added in v1.2.0

type Detector func(number int64, lastDigit int) bool

type Rule added in v1.2.0

type Rule struct {
	Single  Detector
	Plural  Detector
	Special Detector
}

func (Rule) String added in v1.2.0

func (r Rule) String(number int64, lastDigit int) string

type Translations

type Translations struct {
	// contains filtered or unexported fields
}

func NewTranslations

func NewTranslations(source map[string]string) *Translations

func (*Translations) T

func (t *Translations) T(key string) string

Jump to

Keyboard shortcuts

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