On Mar 13, 2008, at 12:11 PM, Stefano Bortolotti wrote:

> Thanks guys! I have done this:
>
>  def remaining_time
>    if expiration != nil
>      diff = expiration - Time.now
>
>      days = (diff / 1.day).to_i
>      hours = ((diff / 1.hour) % 24).to_i
>      mins = ((diff / 1.minute) 60).to_i
>
>      if diff < 7.days
>        return days.to_s + "gg, " + hours.to_s + ":" + mins.to_s
>      else
>        return "more than 7 days"
>      end
>    end
>  end
>
> I'll try to improve it! If you have some advices, they are wellcomes..


Since you have 1.day, 1.hour, 1.min, I assume that you have  
ActiveSupport and likely have Rails.  In any case, you can use  
distance_of_time_in_words

http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#M001006

If you want to clean up your own method:

days, dayfrac = diff.divmod(1.day)
hours, hrfrac = dayfrac.divmod(1.hour)
mins, secs = hrfrac.divmod(1.min)

if days >= 7
   return "more than 7 days"
else
   result = []
   result << "%sgg,"%days unless days.zero?
   result << "%d:%0d"%[hours,mins]
   return result.join(' ')
end

-Rob

Rob Biedenharn		http://agileconsultingllc.com
Rob / AgileConsultingLLC.com