timeago

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2021 License: MIT Imports: 6 Imported by: 1

README

timeago

Basically it's golang port of https://github.com/hustcc/timeago.js

timeago is a go library used to format date with *** time ago statement.

  • i18n supported.
  • Time ago and time in supported.

such as

just now
12 seconds ago
2 hours ago
3 days ago
3 weeks ago
2 years ago

in 12 seconds
in 3 minutes
in 24 days
in 6 months

Supports all i18n of timeago.js

  • Install
go get github.com/sersh88/timeago
  • Usage
t := time.Date(2021, 1, 1, 20, 34, 58, 651387237, time.UTC)
log.Println(timeago.New(t).Format())
log.Println(timeago.New(t).WithLocale("ru").Format())
log.Println(timeago.New(t).WithLocale("ru").WithRelativeTime(t.Add(-time.Hour*24)).Format())

The default locale is en. If passed locale not exists, default will be used. You can also define your own locale with RegisterLocale() function

  // Example:
  func myOwnDeLocale(_ float64, index int) (ago string, in string) {
    var res = [][]string{
      {"gerade eben", "vor einer Weile"},
      {"vor %d Sekunden", "in %d Sekunden"},
      {"vor 1 Minute", "in 1 Minute"},
      {"vor %d Minuten", "in %d Minuten"},
      {"vor 1 Stunde", "in 1 Stunde"},
      {"vor %d Stunden", "in %d Stunden"},
      {"vor 1 Tag", "in 1 Tag"},
      {"vor %d Tagen", "in %d Tagen"},
      {"vor 1 Woche", "in 1 Woche"},
      {"vor %d Wochen", "in %d Wochen"},
      {"vor 1 Monat", "in 1 Monat"},
      {"vor %d Monaten", "in %d Monaten"},
      {"vor 1 Jahr", "in 1 Jahr"},
      {"vor %d Jahren", "in %d Jahren"},
    }[index]
    return res[0], res[1]
  }
  timeago.RegisterLocale("de", myOwnDeLocale)

passed arguments to locale function have the same meaning as in timeago.js

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EN = []string{"second", "minute", "hour", "day", "week", "month", "year"}

Functions

func RegisterLocale

func RegisterLocale(localeName string, f LocaleFunction)

Types

type LocaleFunction

type LocaleFunction func(number float64, index int) (ago string, in string)

type TimeAgo

type TimeAgo struct {
	Time       time.Time
	RelativeTo time.Time
	Locale     string
}

func New

func New(t time.Time) *TimeAgo

func (TimeAgo) Format

func (t TimeAgo) Format() string

func (*TimeAgo) WithLocale

func (t *TimeAgo) WithLocale(locale string) *TimeAgo

func (*TimeAgo) WithRelativeTime

func (t *TimeAgo) WithRelativeTime(tt time.Time) *TimeAgo

Jump to

Keyboard shortcuts

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