Can using Chronic impair your sense of time?
Asked Answered
N

1

28

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
Neu answered 12/5, 2010 at 18:47 Comment(7)
I must admit, when I read the title of this, it made me laugh.Longwinded
ha! me too, i thought it was a trollThunell
+1 At first I was wtf. Then I laughed. Then I cried. Then I got high.Faena
When this all is said and done, this may be the best question ever asked on SO.Cistern
Where is the time_provided? method?Objectify
Good title, I was a bit confused so I checked it out.Interleave
@Faena I was going to answer the question, but then I got high..Crooked
L
3

First, I'm not really sure what are you asking about, because it looks like the code intentionally does what you describe... When there's time provided, the date fields are assigned nil. And I don't think that is Chronic is to blame because that's how your code works.

Not knowing your design (why there are separate date & time fields), the types of fields etc., I would suggest starting with a little kludge like this:

if time_provided?(self.natural_date)
  self.time = Chronic.parse(self.natural_date)
  self.date = self.time.to_date

or:

self.end_date = Chronic.parse(self.natural_date).to_date
if time_provided?(self.natural_date)
  self.time = Chronic.parse(self.natural_date)
end

Or maybe the problem is outside the code you provided: in the part that is responsible for the "because it has no date, it will not show in results" behavior? Maybe you should make the conditions more flexible?

Libertinism answered 13/5, 2010 at 7:44 Comment(1)
Great supposition. Yes, I see that the code should definately not be setting it to nil. So what I did was made this part at least non-nil .. self.date = Chronic.parse(self.natural_date) And, not entirely sure what that did, but at least it made it so that you couldn't add a time anymore as it doesn't auto suggest one. :D . The real question is, is it possible to produce a time and a date seperately. So if they add a date + time, I will get both a date, and a seperate date + time. Alternatively, if they add just a date, then I would get a date, and a date + time without the time :DNeu

© 2022 - 2024 — McMap. All rights reserved.