TimeAgoInWords

Gem Version Build Status Dependency Status Code Climate Coverage Status

Description

Humanize elapsed time from some Time instance to Time.now, e.g. '2 hours and 1 minute ago'

Installation

$ gem install time_ago_in_words or add to your Gemfile this line: gem 'time_ago_in_words' then run bundle install

Usage

Just require 'time_ago_in_words' and then call Time#ago_in_words method:

require 'rubygems' # if ruby 1.8.7
require 'time_ago_in_words'

# Comparing from now:
(Time.now - 10).ago_in_words #=> "10 seconds ago"
(Time.now - 1).ago_in_words  #=> "1 second ago"
(Time.now - 60).ago_in_words #=> "1 minute ago"
(Time.now - 63).ago_in_words #=> "1 minute and 3 seconds ago"

# This is my current time so you can compare
Time.now #=> 2013-03-06 02:19:23 -0300

Time.local(1981,03,03,20,30,40).ago_in_words #=> "690 days and 5 hours ago"
Time.local(2013,03,03,20,30,40).ago_in_words #=> "2 days and 5 hours ago"
Time.local(2013,03,04,20,30,40).ago_in_words #=> "1 day and 5 hours ago"
Time.local(2013,03,05,20,30,40).ago_in_words #=> "5 hours and 48 minutes ago"
Time.local(2013,03,05,21,13,40).ago_in_words #=> "5 hours and 5 minutes ago"
Time.local(2013,03,06,00,30,40).ago_in_words #=> "1 hour and 48 minutes ago"
Time.local(2013,03,06,01,11,40).ago_in_words #=> "1 hour and 7 minutes ago"
Time.local(2013,03,06,01,27,40).ago_in_words #=> "51 minutes and 43 seconds ago"
Time.local(2013,03,06,02,19,20).ago_in_words #=> "3 seconds ago"

Contributing

  1. Fork it.
  2. Make your feature addition or bug fix and create your feature branch.
  3. Update the Change Log.
  4. Add specs/tests for it. This is important so I don't break it in a future version unintentionally.
  5. Commit, create a new Pull Request.
  6. Check that your pull request passes the build.

TODO

  • Add DateTime support. Currently only available for Time objects.
  • Add "N months ago"
  • Add "N years ago"
  • Add "N decades ago"
  • Add "N centuries ago"
  • Add more rspec Examples
  • Implement Time mocking for testing, instead of abusing Time.now on spec
  • Extract time_ago_in_words and implement that alternative precision syntax, e.g. "less than a minute ago"

License

Released under the MIT License. See the LICENSE file for further details.

RubyGems | Documentation | Source | Bugtracker | Build Status | Dependency Status | Code Climate