<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>I.NFECTIO.US: Validate Dates in Your Models</title>
    <link>http://i.nfectio.us/articles/2006/09/07/validate-dates-in-your-models</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>"Nothing in life is worth doing if you have no life while doing it"</description>
    <item>
      <title>Validate Dates in Your Models</title>
      <description>mojombo in #caboose, just released his first version of &lt;a href="http://chronic.rubyforge.org/"&gt;Chronic&lt;/a&gt;, a new Ruby Gem for natural language processing of Dates and Times.

&lt;pre&gt;
gem install chronic

irb:&gt;
Chronic.parse('tomorrow')
    #=&gt; Mon Aug 28 12:00:00 PDT 2006

Chronic.parse('monday', :context =&gt; :past)
    #=&gt; Mon Aug 21 12:00:00 PDT 2006

Chronic.parse('this tuesday 5:00')
    #=&gt; Tue Aug 29 17:00:00 PDT 2006

Chronic.parse('this tuesday 5:00', :ambiguous_time_range =&gt; :none)
    #=&gt; Tue Aug 29 05:00:00 PDT 2006

Chronic.parse('may 27th', :now =&gt; Time.local(2000, 1, 1))
    #=&gt; Sat May 27 12:00:00 PDT 2000

Chronic.parse('may 27th', :guess =&gt; false)
    #=&gt; Sun May 27 00:00:00 PDT 2007..Mon May 28 00:00:00 PDT 2007

&lt;/pre&gt;

The thing I find really nice is that if you pass it a malformed date, it returns nil. This means, in your model validations, you can add:

&lt;pre&gt;
require 'chronic'

class Meeting &lt; ActiveRecord::Base

  def validation
    errors.add :meeting_date, 'is not a valid date' if Chronic.parse(meeting_date.to_s).nil?
  end

end
&lt;/pre&gt;</description>
      <pubDate>Thu, 07 Sep 2006 23:11:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:8014225f-73e7-4d6b-aa0b-35091ad56078</guid>
      <author>Bob Silva</author>
      <link>http://i.nfectio.us/articles/2006/09/07/validate-dates-in-your-models</link>
      <category>Ruby on Rails</category>
      <category>rails</category>
      <category>date</category>
      <category>validation</category>
      <category>activerecord</category>
    </item>
    <item>
      <title>"Validate Dates in Your Models" by Adam T.</title>
      <description>I'm not entirely sure I'm going to get much of a response here, it seems good to bring up.

Something I'm running into is that at the point where you've hit your validation (possibly by overriding the validate() method), your target attribute (meeting_date, in this case) has already been typecast to its database type. Assuming that type is "datetime," this becomes a problem because Chronic.parse WILL return nil on a string like, "Thu Dec 14 16:34:00 -0800 2006."</description>
      <pubDate>Thu, 14 Dec 2006 16:31:46 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:90156647-b335-41c3-bf6b-f3a3167a0f7e</guid>
      <link>http://i.nfectio.us/articles/2006/09/07/validate-dates-in-your-models#comment-237</link>
    </item>
    <item>
      <title>"Validate Dates in Your Models" by Kevin</title>
      <description>What about the other way? ;)

9/9/2006 is tomorrow, one day from now, next Saturday, etc. etc. etc.

(Seriously though, great work... it even knows fortnights!)</description>
      <pubDate>Fri, 08 Sep 2006 14:17:10 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:7e1eba41-f82f-4a4f-9492-e29c6cb751e4</guid>
      <link>http://i.nfectio.us/articles/2006/09/07/validate-dates-in-your-models#comment-130</link>
    </item>
    <item>
      <title>"Validate Dates in Your Models" by d. Taylor Singletary</title>
      <description>This is terrific. Thanks. Ruby is the single easiest and most pleasant language I've ever dealt with for manipulating Time.</description>
      <pubDate>Fri, 08 Sep 2006 07:27:36 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:1a743e0f-ddae-43c5-9966-b972a711b070</guid>
      <link>http://i.nfectio.us/articles/2006/09/07/validate-dates-in-your-models#comment-128</link>
    </item>
  </channel>
</rss>
