diff options
author | Casey Link <unnamedrambler@gmail.com> | 2008-06-30 19:44:05 -0400 |
---|---|---|
committer | Casey Link <unnamedrambler@gmail.com> | 2008-07-03 11:54:54 -0400 |
commit | f8a309583294eb069e698243c2456f06c81a63ac (patch) | |
tree | 025562e65153ea5f6cf87395b83758f092add103 | |
parent | 499d0966b64336036d9e9e78413e0b1cd78f4aa4 (diff) |
utils.rb: added timeago and distance_of_time_in_words functions
-rw-r--r-- | lib/rbot/core/utils/utils.rb | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/rbot/core/utils/utils.rb b/lib/rbot/core/utils/utils.rb index b180a62a..084f92a5 100644 --- a/lib/rbot/core/utils/utils.rb +++ b/lib/rbot/core/utils/utils.rb @@ -236,6 +236,49 @@ module ::Irc end end + # Returns human readable time. + # Like: 5 days ago + # about one hour ago + # options + # :start_date, sets the time to measure against, defaults to now + # :date_format, used with <tt>to_formatted_s<tt>, default to :default + def Utils.timeago(time, options = {}) + start_date = options.delete(:start_date) || Time.new + date_format = options.delete(:date_format) || :default + delta_minutes = (start_date.to_i - time.to_i).floor / 60 + if delta_minutes.abs <= (8724*60) # eight weeks? I'm lazy to count days for longer than that + distance = Utils.distance_of_time_in_words(delta_minutes); + if delta_minutes < 0 + _("%{d} from now") % {:d => distance} + else + _("%{d} ago") % {:d => distance} + end + else + return _("on %{date}") % {:date => system_date.to_formatted_s(date_format)} + end + end + # Translates a number of minutes into verbal distances. + # e.g. 0.5 => less than a minute + # 70 => about one hour + def Utils.distance_of_time_in_words(minutes) + case + when minutes < 1 + _("less than a minute") + when minutes < 50 + _("%{m} minutes") % {:m => minutes} + when minutes < 90 + _("about one hour") + when minutes < 1080 + _("%{m} hours") % {:m => (minutes / 60).round} + when minutes < 1440 + _("one day") + when minutes < 2880 + _("about one day") + else + _("%{m} days") % {:m => (minutes / 1440).round} + end + end + # Execute an external program, returning a String obtained by redirecting # the program's standards errors and output |