From f8a309583294eb069e698243c2456f06c81a63ac Mon Sep 17 00:00:00 2001 From: Casey Link Date: Mon, 30 Jun 2008 19:44:05 -0400 Subject: utils.rb: added timeago and distance_of_time_in_words functions --- lib/rbot/core/utils/utils.rb | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'lib/rbot') 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 to_formatted_s, 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 -- cgit v1.2.3