humantime

package module
v0.0.0-...-f59ec28 Latest Latest
Warning

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

Go to latest
Published: May 14, 2020 License: MIT Imports: 2 Imported by: 4

README

HumanTime

Golang library to create human-readable US English strings from time.Duration instances.

Installation

go get github.com/BenJetson/humantime

Import

import "github.com/BenJetson/humantime"

Examples

Example using func humantime.Since(t time.Time) string
import (
    "fmt"
    "time"
    
    "github.com/BenJetson/humantime"
)

func main() {
    t := time.Now()

    time.Sleep(5 * time.Second)
    fmt.Println(humantime.Since(t)) // just now

    time.Sleep(30 * time.Second)
    fmt.Println(humantime.Since(t)) // seconds ago

    time.Sleep(25 * time.Second)
    fmt.Println(humantime.Since(t)) // a minute ago

    time.Sleep(240 * time.Second)
    fmt.Println(humantime.Since(t)) // 5 minutes ago
}
Example using func humantime.Duration(d time.Duration) string
import (
    "fmt"
    "time"
    
    "github.com/BenJetson/humantime"
)

func main() {

    var d time.Duration

    d, _ = time.ParseDuration("5s")
    fmt.Println(humantime.Since(d)) // just now

    d, _ = time.ParseDuration("25s")
    fmt.Println(humantime.Since(d)) // seconds ago

    d, _ = time.ParseDuration("60s")
    fmt.Println(humantime.Since(d)) // a minute ago

    d, _ = time.ParseDuration("5m")
    fmt.Println(humantime.Since(d)) // 5 minutes ago
}

Documentation

Index

Constants

View Source
const (
	// HOURS_IN_MONTH represents the approximate number of hours in a month.
	// Note that hours per month is approximate due to fluctuations in month length.
	HOURS_IN_MONTH = 730
	// HOURS_IN_DAY represents the number of hours in a day.
	HOURS_IN_DAY = 24
	// HOURS_IN_WEEK represents the number of hours in a week.
	HOURS_IN_WEEK = HOURS_IN_DAY * 7
	// HOURS_IN_YEAR represents the number of hours in a year.
	HOURS_IN_YEAR = HOURS_IN_DAY * 365
)

Variables

This section is empty.

Functions

func Duration

func Duration(t time.Duration) string

Duration returns a string with a plain English descrption of the length of time that the time.Duration t contains.

func Since

func Since(t time.Time) string

Since provides an easy way to call Duration without having to call time.Since on a time.Time first.

Types

This section is empty.

Jump to

Keyboard shortcuts

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