Skip to content

Convert Duration objects to human-friendly strings like '2h 30m 17s'

License

Notifications You must be signed in to change notification settings

VuframeLab/activesupport-duration-human_string

 
 

Repository files navigation

ActiveSupport::Duration#human_string Gem Version

Convert ActiveSupport::Duration objects to human-friendly strings like '2h 30m 17s' or '3y 6m 4d 12h 30m 5s'.

  • Like distance_of_time_in_words helper but exact rather than approximate
    • distance_of_time_in_words is somewhat configurable but only gives an approximation ('over 3 years'), with no option to print the exact number of each units.
  • Like #inspect but more concise and configurable.
    • #inspect is long ('3 years, 6 months, 4 days, 12 hours, 30 minutes, and 5 seconds') and not at all configurable.
  • Like #iso8601 but more human readable rather than machine readable.
    • #iso8601 is concise and machine-readable, but not very human-readable ('P3Y6M4DT12H30M5S')!
  • Unlike #to_s, which is very concise ('110839937') but not at all human-readable for large durations

Installation

Add this line to your application's Gemfile:

gem 'activesupport-duration-human_string'

Usage

    duration = 3500.seconds
    duration.human_str                 # =>  '58m 20s'
    duration.human_str(delimiter: '')  # =>  '58m20s'
    duration.human_str(separator: ' ') # =>  '58 m 20 s'
    duration.human_str(delimiter: ', ', separator: ' ') # =>  '58 m, 20 s'

    duration = ActiveSupport::Duration.parse "P3Y6M4DT12H30M5S"
    # => 3 years, 6 months, 4 days, 12 hours, 30 minutes, and 5 seconds

    duration.human_str                  # => "3y 6m 4d 12h 30m 5s"
    (duration - 4.days).human_str       # => "3y 6m 12h 30m 5s"
    duration.human_str(delimiter: ', ') # => "3y, 6m, 4d, 12h, 30m, 5s"

Also aliased as human_string, to_human_s.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/TylerRick/activesupport-duration-human_string.

Not really related

About

Convert Duration objects to human-friendly strings like '2h 30m 17s'

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 98.5%
  • Shell 1.5%