Coder Social home page Coder Social logo

sloppy-duration's Introduction

Golang SloppyDuration

A Golang library for manipulating durations with coarser granularity and less precision.

Why?

The time.Duration type supports a time granularity of up to an "hour" unit, and it does so to ensure duration precision. Obviously, for example, a month is a variable duration, which is why it cannot be manipulated precisely.

SloppyDuration expands upon time.Duration's parsing and Stringer ability with support for:

  • Days (d): 24h
  • Weeks (w): 24h * 7
  • Months (M): 365d / 12
  • Years (y): 1d * 365

Additionally, String() provides a simpler, less precise output, such as "2m" rather than "2m3s". The string value can also be customized via a template.Template.

SloppyDuration does not support multi-unit duration strings, such as "1h45m0s", nor signed durations.

An example use case for this library could be to render a publication date for a blog post of, say, "2 months ago".

Usage

Install:

go get github.com/troykinsella/sloppy-duration

Use:

year, err := sloppy_duration.Parse("1y")
if err != nil {
	panic(err)
}
fmt.Printf("%s days in a year\n", year.Days())
// Prints "365 days in a year"

soon, err := sloppy_duration.Parse("48h")
if err != nil {
	panic(err)
}
fmt.Printf("Production will fail in: %s\n", soon)
// Prints "Production will fail in: 2d"

See examples/ for more details.

Testing

go get github.com/onsi/ginkgo/ginkgo
ginkgo

License

MIT © Troy Kinsella

sloppy-duration's People

Contributors

troykinsella avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.