title: input date validation check calling to_date
body: <p>I'm using postgreSQL-9.1.6 on CENTOS.</p>
  <p>I have issue on <strong>to_date</strong> function like...</p>
  <pre><code>postgres=# select to_date('20130229','yyyymmdd');
    to_date   
  ------------
   2013-03-01
  (1 row)
  </code></pre>
  <p>so I'm trying to add validation check for out of range against incoming date.</p>
  <blockquote>
  <p>postgres=# select to_date('20130229','yyyymmdd');</p>
  <p>ERROR:  timestamp out of range</p>
  </blockquote>
  <p>I found a hint from <a href="http://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=d4c78c1811d14b14f47f3611bf2c2c1a35817988" rel="nofollow noreferrer">here</a> but it didn't work and I asked <a href="https://stackoverflow.com/questions/17519851/raise-error-when-date-is-not-valid">here</a>.
  unfortunately, I couldn't get answer.</p>
  <p>Finally, I reached another conclustion. Below is My <strong>formatting.c</strong> to which 9 lines marked <code>+</code> added.</p>
  <pre><code>Datum
  to_date(PG_FUNCTION_ARGS)
  {
          text       *date_txt = PG_GETARG_TEXT_P(0);
          text       *fmt = PG_GETARG_TEXT_P(1);
          DateADT         result;
          struct pg_tm tm;
          fsec_t          fsec;
  
      +   int ndays[]={-1,31,28,31,30,31,30,31,31,30,31,30,31};
  
      +   int last_day_of_month;
  
          do_to_timestamp(date_txt, fmt, &amp;tm, &amp;fsec);
  
      +   last_day_of_month = ndays[tm.tm_mon];
      +   if (((tm.tm_year &amp; 3) == 0 &amp;&amp; ((tm.tm_year % 25) != 0 || (tm.tm_year &amp; 15) == 0)) &amp;&amp; tm.tm_mon == 2 )
      +           last_day_of_month = ndays[tm.tm_mon] + 1;
  
      +   if( tm.tm_mon &gt; 12 || tm.tm_mon &lt; 1 || tm.tm_mday &gt; last_day_of_month || tm.tm_mday &lt; 1
  )
      +           ereport(ERROR,
      +                           (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
      +                            errmsg(&quot;timestamp out of range&quot;)));
  
          result = date2j(tm.tm_year, tm.tm_mon, tm.tm_mday) - POSTGRES_EPOCH_JDATE;
  
          PG_RETURN_DATEADT(result);
  }
  </code></pre>
  <p>although my own <strong>formatting.c</strong> works well, I'm not sure this works perfect. I'm worried about unexpected results like throwing error against the valid date.</p>
  <p>Any advice would be very appreciated.</p>