Can using Chronic impair your sense of time?

Posted by Trip on Stack Overflow See other posts from Stack Overflow or by Trip
Published on 2010-05-12T18:47:55Z Indexed on 2010/05/13 13:44 UTC
Read the original article Hit count: 174

Filed under:
|

Haha..

I'm using Chronic to parse the time users add in the Calendar. Where the code works and implements the right time, the end result is that, IF a user adds a time, then it has no date, and because it has no date, it will not show in results. Any ideas?

def set_dates
  unless self.natural_date.blank? || Chronic.parse(self.natural_date).blank?
    # check if we are dealing with a date or a date + time
    if time_provided?(self.natural_date)
      self.date = nil
      self.time = Chronic.parse(self.natural_date)
    else
      self.date = Chronic.parse(self.natural_date).to_date
      self.time = nil
    end
  end

  unless self.natural_end_date.blank? || Chronic.parse(self.natural_end_date).blank?
    # check if we are dealing with a date or a date + time
    if time_provided?(self.natural_end_date)
      self.end_date = nil
      self.end_time = Chronic.parse(self.natural_end_date)
    else
      self.end_date = Chronic.parse(self.natural_end_date).to_date
      self.end_time = nil
    end
  end
end

Edit:

Here is the time_provided? method:

def time_provided?(natural_date_string)
  date_span = Chronic.parse(natural_date_string, :guess => false)
  (date_span.last - date_span.first).to_i == 1
end

© Stack Overflow or respective owner

Related posts about chronic

Related posts about ruby-on-rails