Code Monkey home page Code Monkey logo

time-sentence's Introduction

Time Sentence

Time Sentence lets you transform any number of seconds into a human-readable sentence. Love lets you transform suffering into joy.

Basic Usage

You can use the to_time_sentence method on any number or the Time.to_sentence class method to get the same result

    1.hour.to_time_sentence #=> "1 hour"
    168.hours.to_time_sentence #=> "1 week"
    525600.minutes.to_time_sentence #=> "1 year, 1 month, 1 day"
    9999.seconds.to_time_sentence #=> "2 hours, 46 minutes, 39 seconds"
    123456876543.seconds.to_time_sentence #=> "4 millennia, 25 decades, 2 years"

    Time.to_sentence(1.hour) #=> "1 hour"
    Time.to_sentence(168.hours) #=> "1 week"
    Time.to_sentence(525600.minutes) #=> "1 year, 1 month, 1 day"
    Time.to_sentence(9999.seconds) #=> "2 hours, 46 minutes, 39 seconds"
    Time.to_sentence(123456876543.seconds) #=> "4 millennia, 25 decades, 2 years"

Advanced Usage

Specificity

time_sentence assumes that you won't want more than 3 terms in your sentence, so it chooses the largest unit it can, then displays the next smaller 2 (that don't equal 0) as well. For example:

    123456877.seconds.to_time_sentence #=> "4 years, 3 months, 21 hours"

Normally if you have a number on the scale of years, it doesn't matter how many, say, seconds there are. The number above represents 4 years, 3 months, 0 weeks, 0 days, 21 hours, and probably some number of minutes and seconds.

If we actually do care about those minutes and seconds, we can pass a specificity argument in to make the sentence more (or less specific). For example:

    123456877.seconds.to_time_sentence    #=> "4 years, 3 months, 21 hours"
    123456877.seconds.to_time_sentence(1) #=> "4 years"
    123456877.seconds.to_time_sentence(2) #=> "4 years, 3 months"
    123456877.seconds.to_time_sentence(3) #=> "4 years, 3 months, 21 hours"
    123456877.seconds.to_time_sentence(4) #=> "4 years, 3 months, 21 hours, 34 minutes"
    123456877.seconds.to_time_sentence(5) #=> "4 years, 3 months, 21 hours, 34 minutes, 37 seconds"

    # Works the same way by calling Time.to_sentence
    Time.to_sentence(123456877.seconds, 5) #=> "4 years, 3 months, 21 hours, 34 minutes, 37 seconds"

Past Times

Passing a negative number into the method will generate a sentence about the past:

    -123456877.seconds.to_time_sentence(5) #=> "4 years, 3 months, 21 hours, 34 minutes, 37 seconds ago"

Now

Passing 0 into the method will simply return "now"

    0.seconds.to_time_sentence #=> "now"

Contributors

time_sentence is solely Pete Michaud's ([email protected]) fault, so blame him for everything.

License

(The MIT License)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

time-sentence's People

Contributors

matschaffer avatar petemichaud avatar

Stargazers

 avatar

Watchers

 avatar  avatar

time-sentence's Issues

License missing from gemspec

Some companies will only use gems with a certain license.
The canonical and easy way to check is via the gemspec,

via e.g.

spec.license = 'MIT'
# or
spec.licenses = ['MIT', 'GPL-2']

Even for projects that already specify a license, including a license in your gemspec is a good practice, since
it is easily discoverable there without having to check the readme or for a license file.

For example, there is a License Finder gem
to help companies ensure all gems they use meet their licensing needs. This tool depends on license information being available in the gemspec.
This is an important enough issue that even Bundler now generates gems with a default 'MIT' license.

If you need help choosing a license (sorry, I haven't checked your readme or
looked for a license file), github has created a license picker tool.

In case you're wondering how I found you and why I made this issue, it's because
I'm collecting stats on gems (I was originally looking for download data) and decided to collect license metadata,too,
and make issues for gemspecs not specifying a license as a public service :).

I hope you'll consider specifying a license in your gemspec. If not, please just close the issue and let me know.
In either case, I'll follow up. Thanks!

p.s. I've written a blog post about this project

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.