Skip to content

Instantly share code, notes, and snippets.

@pgeraghty
Created February 24, 2015 01:42
Show Gist options
  • Save pgeraghty/1230eee7638da0853ffc to your computer and use it in GitHub Desktop.
Save pgeraghty/1230eee7638da0853ffc to your computer and use it in GitHub Desktop.
format compact duration as days, hours, minutes, seconds e.g. 3d23h10s
# seconds = duration in seconds as integer i.e. time.to_i
[
seconds / (60*60*24), # days
seconds % (60*60*24) / (60*60), # hours
seconds % (60*60) / 60, # minutes
seconds % 60 # seconds
].zip(%w(d h m s)).map { |u| u.join unless u.first.zero? }.compact.tap { |a| a << '<1s' if a.none? }.join
# Uses Object#tap rather than Rails Object enhancements like Object#presence
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment