Created
September 3, 2010 23:19
-
-
Save reinh/564706 to your computer and use it in GitHub Desktop.
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
| def timesince(d, now=Time.now) | |
| delta = (now - d).to_i | |
| return "0 minutes" if delta <= 60 | |
| chunks = [ | |
| [60 * 60 * 24 * 356 , 'year' ], | |
| [60 * 60 * 24 * 7 , 'week' ], | |
| [60 * 60 * 24 , 'day' ], | |
| [60 * 60 , 'hour' ], | |
| [60 , 'minute' ] | |
| ] | |
| pluralize = lambda {|amount, unit| "#{amount} #{unit}#{'s' if amount != 1}"} | |
| ret = "" | |
| chunks.each do |seconds, unit| | |
| amount, delta = delta.divmod(seconds) | |
| ret << pluralize[amount, unit] << ' ' if amount > 0 | |
| end | |
| ret.strip | |
| end |
Author
reinh
commented
Sep 3, 2010
Author
Month calculations are wrong ofc (not all months have 30 days)
Author
Fixed.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment