[ANN] Announcing activesupport-duration-human_string 0.1.1

#1

Have you ever wanted something like distance_of_time_in_words that gave the precise amount of a time duration instead of just a round approximation like 'over 3 years'?

Introducing ActiveSupport::Duration#human_string, which converts ActiveSupport::Duration
objects into human-friendly strings like '3y 6m 4d 12h 30m 5s'.

Usage examples

    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"

Similar methods

  • 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