Created
February 24, 2015 01:42
-
-
Save pgeraghty/1230eee7638da0853ffc to your computer and use it in GitHub Desktop.
format compact duration as days, hours, minutes, seconds e.g. 3d23h10s
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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