Extract recognized date string passed to parse method in Chronic

391 views Asked by At

I'd like to mimic the functionality of the Mac calendar quick event or Fantastical's quick entry. Using the Chronic gem, I can pass a string like:

"Today at 3pm"
=> 2014-01-24 15:00:00 -0600 

Chronic parsing doesn't work if you pass in something like:

"Eat at Joes Today at 3pm" 
=> nil

What I've done so far is use a simple regex to split a string at a word normally used to return a date with Chronic. The initial regex is simple:

scan(/(.+)(tomorrow{1}.+|in\s.+|next\s.+|today\s.+)/)

This returns an array with the "title", if you will, and the string I want to sent to Chronic to parse for me.

Two questions:

  1. Is this really the best way to do this? I'd have to provide some mega regex to split whatever string I think my users will submit here.
  2. Would hacking at Chronic be better? It's already parsing the dates for me, but my initial thought is no because if you pass Eat at Joes Today at 3pm to Chronic.parse, it'll return nil. It seems it doesn't recognize the part of the string for formatting the date in it's present form.
1

There are 1 answers

1
jklina On BEST ANSWER

I wouldn't edit Chronic. Chronic's only function is to parse natural language date time, not other input. You might be interested in the Nickel gem here:

https://github.com/iainbeeston/nickel

This separates time from other language.

n = Nickel.parse("use the force on july 1st at 9am", Time.now)
n.message                       #=> "use the force"
n.occurrences.first.start_date  #=> "20110701"