humanize

package module
v0.0.0-...-4123e5c Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2013 License: MIT Imports: 6 Imported by: 5

README

Humane Units

Just a few functions for helping humanize times and sizes.

go get it as github.com/dustin/go-humanize, import it as "github.com/dustin/go-humanize", use it as humanize

Sizes

This lets you take numbers like 82854982 and convert them to useful strings like, 83MB or 79MiB (whichever you prefer).

Example:

fmt.Printf("That file is %s.", humanize.Bytes(82854982))

Times

This lets you take a time.Time and spit it out in relative terms. For example, 12 seconds ago or 3 days from now.

Example:

fmt.Printf("This was touched %s", humanize.Time(someTimeInstance))

Thanks to Kyle Lemons for the time implementation from an IRC conversation one day. It's pretty neat.

Ordinals

From a mailing list discussion where a user wanted to be able to label ordinals.

0 -> 0th
1 -> 1st
2 -> 2nd
3 -> 3rd
4 -> 4th
[...]

Example:

fmt.Printf("You're my %s best friend.", humanize.Ordinal(193))

Commas

Want to shove commas into numbers? Be my guest.

0 -> 0
100 -> 100
1000 -> 1,000
1000000000 -> 1,000,000,000
-100000 -> -100,000

Example:

fmt.Printf("You owe $%s.\n", humanize.Comma(6582491))

Documentation

Overview

Package humanize converts boring ugly numbers to human-friendly strings.

Durations can be turned into strings such as "3 days ago", numbers representing sizes like 82854982 into useful strings like, "83MB" or "79MiB" (whichever you prefer).

Index

Constants

View Source
const (
	Byte   = 1
	KiByte = Byte * 1024
	MiByte = KiByte * 1024
	GiByte = MiByte * 1024
	TiByte = GiByte * 1024
	PiByte = TiByte * 1024
	EiByte = PiByte * 1024
)

IEC Sizes. kibis of bits

View Source
const (
	IByte = 1
	KByte = IByte * 1000
	MByte = KByte * 1000
	GByte = MByte * 1000
	TByte = GByte * 1000
	PByte = TByte * 1000
	EByte = PByte * 1000
)

SI Sizes.

View Source
const (
	Minute = 60
	Hour   = 60 * Minute
	Day    = 24 * Hour
	Week   = 7 * Day
	Month  = 30 * Day
	Year   = 12 * Month
)

Seconds-based time units

Variables

This section is empty.

Functions

func Bytes

func Bytes(s uint64) string

Bytes produces a human readable representation of an SI size. Bytes(82854982) -> 83MB

func Comma

func Comma(v int64) string

Comma produces a string form of the given number in base 10 with commas after every three orders of magnitude. e.g. Comma(834142) -> 834,142

func IBytes

func IBytes(s uint64) string

IBytes produces a human readable representation of an IEC size. IBytes(82854982) -> 79MiB

func Ordinal

func Ordinal(x int) string

Ordinal gives you the input number in a rank/ordinal format. Ordinal(3) -> 3rd

func ParseBytes

func ParseBytes(s string) (uint64, error)

ParseBytes parses a string representation of bytes into the number of bytes it represents. ParseBytes("42MB") -> 42000000, nil ParseBytes("42mib") -> 44040192, nil

func Time

func Time(then time.Time) string

Time formats a time into a relative string. Time(someT) -> "3 weeks ago"

Types

This section is empty.

Jump to

Keyboard shortcuts

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