0.0
No commit activity in last 3 years
No release in over 3 years
Extend Numeric with method to provide duration as a human readable string
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 1.0
~> 12.3
~> 3.3
 Project Readme

duration-human

Gem Version

Ruby module to entend Numeric class with a method to convert a number of seconds to a human readable time duration.

Requires Ruby 2.4.0 or newer. Tested with Ruby 2.4, 2.5 & 2.6

For example:

80.duration      ==> "80 seconds"
200.duration     ==> "3 minutes and 20 seconds"
4800.duration    ==> "80 minutes and 0 seconds"
9000.duration    ==> "2 hours and 30 minutes"
93660.duration   ==> "26 hours and 1 minutes"
349200.duration  ==> "4 days and 1 hours"

Setting concise to true in the method arguments will use initials s, m, h, d for the units, and omit the word and.

For example:

80.duration(concise: true)      ==> "80s"
200.duration(concise: true)     ==> "3m 20s"
93660.duration(concise: true)   ==> "26h 1m"
349200.duration (concise: true) ==> "4d 1h"

The thresholds for changing between seconds, minutes, hours and days is "2". For example, 80 is less than 2 minutes, so it is "80 seconds". 200 is more than 2 minutes, so it is "3 minutes and 20 seconds" instead of "200 seconds".