views:

59

answers:

2

Yes, I've read and done teh Google many times but I still can't get this working... maybe I'm an idiot :)

I have a system using tickets. Start date is "created_at" in the timestamps. Each ticket closes 7 days after "created_at". In the model, I'm using:

def closes
  (self.created_at + 7.days)
end

I'm trying to create another method that will take "closes" and return it as how many days, hours, minutes, and seconds are left before the ticket closes. Anyone want to help and/or admonish my skills? ;)

EDIT:

Ideally, I'd like the final output to be something like "6d14h22s" or something like that.

+1  A: 

Use distance_of_time_in_words helper method:

class Ticket < ActiveRecord::Base
  include ActionView::Helpers::DateHelper

  def closes_in_words
    distance_of_time_in_words(Time.now, self.closes)
  end
end
KandadaBoggu
Thank you very much, that goes a long way. The one thing it's missing is it only uses one metric "6 days left" where I need something like "6d12h14m". I do appreciate your help though!
Kevin
You have to write custom code to do what you need. I like the way Rails does it. In my view, hour/min/sec granularity becomes relevant when within a day of the deadline.
KandadaBoggu
I agree, although I could see some uses to be a bit more specific. Thanks for the help Kandada
Kevin
+1  A: 

I don't know of any good way to arbitrarily format durations with rails, but you can do it yourself like this:

def closes_in
  minutes = (Time.now - closes).to_i / 60
  days = minutes / (24*60)
  minutes -= days * 24*60
  hours = minutes / 60
  minutes -= hours * 60
  "#{days}d#{hours}h#{minutes}m"
end
mckeed
Awesome. I appreciate the help mckeed :)
Kevin