elapsed

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2018 License: MIT Imports: 5 Imported by: 12

README

Elapsed time

GoDoc Build Status Code Coverage Go Report Card

Golang package to return the elapsed time since a given time in a human readable format.

Installation
$ go get -u github.com/rvflash/elapsed
Usage
t := time.Now().Add(-time.Hour)
fmt.Println(elapsed.Time(t))
// Output: 1 hour ago

t = time.Now().Add(-time.Hour * 24 * 3)
fmt.Println(elapsed.Time(t))
// Output:  3 days ago

t, _ = time.Parse("2006-02-01", "2049-08-19")
fmt.Println(elapsed.Time(t))
// Output: not yet

t = time.Now().Add(-time.Hour * 24 * 3)
fmt.Println(elapsed.LocalTime(t, "fr"))
// Output: il y a 3 jours

Documentation

Overview

Package elapsed return the elapsed time since a given time in a human readable format.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrExists     = errors.New("already exists")
	ErrIncomplete = errors.New("missing translation")
	ErrISOCode    = errors.New("invalid language code")
)

Common errors

Functions

func AddTranslation added in v0.1.0

func AddTranslation(lang string, tr Terms) error

AddTranslation adds the terms for the given language code. It fails to do it if the language code already exists or if it misses some translation IDs.

func LocalTime added in v0.1.0

func LocalTime(t time.Time, lang string) string

LocalTime returns in a human readable format the elapsed time since the given datetime using the given ISO 639-1 language code.

func Time

func Time(t time.Time) string

Time returns in a human readable format the elapsed time since the given datetime in english. This methods keeps the interface of the first version of the package.

Example
package main

import (
	"fmt"
	"time"

	"github.com/rvflash/elapsed"
)

func main() {
	t := time.Now().Add(-time.Hour)
	fmt.Println(elapsed.Time(t))

	t = time.Now().Add(-time.Hour * 24 * 3)
	fmt.Println(elapsed.Time(t))

	t, _ = time.Parse("2006-02-01", "2049-08-19")
	fmt.Println(elapsed.Time(t))

	t = time.Now().Add(-time.Hour * 24 * 3)
	fmt.Println(elapsed.LocalTime(t, "fr"))
}
Output:

1 hour ago
3 days ago
not yet
il y a 3 jours

Types

type Terms added in v0.1.0

type Terms map[TrID]string

Lists all translations by identifier.

type TrID added in v0.1.0

type TrID int

TrID is the ID of a translation.

const (
	// NotYet is the translation ID for the "not yet" text.
	NotYet TrID = iota
	// JustNow is the translation ID for the "just now" text.
	JustNow
	// LastMinute is the translation ID for the "1 minute ago" text.
	LastMinute
	// Minutes is the translation ID for the "%d minutes ago" text.
	Minutes
	// LastHour is the translation ID for the "1 hour ago" text.
	LastHour
	// Hours is the translation ID for the "%d hours ago" text.
	Hours
	// Yesterday is the translation ID for the "yesterday" text.
	Yesterday
	// Days is the translation ID for the "%d days ago" text.
	Days
	// Weeks is the translation ID for the "`%d weeks ago" text.
	Weeks
	// Months is the translation ID for the "%d months ago" text.
	Months
	// Years is the translation ID for the "%d years ago" text.
	Years
)

type Translation added in v0.1.0

type Translation map[string]Terms

Lists all translations by language code.

Jump to

Keyboard shortcuts

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