printer

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2019 License: MIT Imports: 3 Imported by: 1

README

DateTime Printer

This is a printer for Go's time.Time that prints the Date and Time portions of a timestamp differently depending on how long ago the timestamp occurred.

  • When it has been less than a day, only the time portion is printed: "5 minutes ago".
  • When it has been more than a day, only the date portion is printed: "2019-10-03".

Example

package main

import (
	"time"
	
	printer "github.com/carolynvs/datetime-printer"
)

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

	p := printer.DateTimePrinter{}

	println(p.Format(now))
	// "now"

	time.Sleep(2*time.Second)
	println(p.Format(now))
	// "2 seconds ago"

	yearAgo := now.AddDate(-1, 0, 0)
	println(p.Format(yearAgo))
	// "2018-05-10"

	p.DateFormat = "2006/01/02"
	println(p.Format(yearAgo))
	// "2018/05/10"
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DateTimePrinter added in v0.2.0

type DateTimePrinter struct {
	// DateFormat to use for printing timestamps beyond a day old.
	DateFormat string

	// Now allows you use a fixed point in time when printing.
	// This is useful when printing tables of data, so all the relative times
	// are in relation to the same "now", and when unit testing.
	Now func() time.Time
}

DateTimePrinter uses go-humanize to print just the time portion of recent timestamps (within the last day) and for anything past a day, only prints the date portion of the timestamp.

func (DateTimePrinter) DateFormatOrDefault added in v0.2.0

func (t DateTimePrinter) DateFormatOrDefault() string

DateFormatOrDefault gets the format to apply to dates.

func (DateTimePrinter) Format added in v0.2.0

func (t DateTimePrinter) Format(value time.Time) string

Format the specified timestamp relative to now.

func (DateTimePrinter) NowOrDefault added in v0.2.0

func (t DateTimePrinter) NowOrDefault() time.Time

NowOrDefault gets the current time, using the overridden Now(), or time.Now().

Jump to

Keyboard shortcuts

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