title: Validating Date of birth in C++
body: <p>I have just started learning C++.
  While going through the codes on this site I came across a code which validates date entered by the user.But that problem is that it can take even future values , thus this logic needs to be tweaked in order to work for accepting DOB.
  so I decided to take the current time using "time()" function and then comparing it with the entered date. To start with, I added two lines in the code (the ones which are commented in the below code) which are</p>
  
  <pre><code>time(&amp;tNow);
  </code></pre>
  
  <p>and</p>
  
  <pre><code>const struct tm *now = localtime(&amp;tNow);
  </code></pre>
  
  <p>here is my code :</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;sstream&gt;
  #include &lt;ctime&gt;
  
  using namespace std;
  // function expects the string in format dd/mm/yyyy:
  bool extractDate(const std::string&amp; s, int&amp; d, int&amp; m, int&amp; y){
      std::istringstream is(s);
      char delimiter;
      if (is &gt;&gt; d &gt;&gt; delimiter &gt;&gt; m &gt;&gt; delimiter &gt;&gt; y) {
          struct tm t = {0};
          t.tm_mday = d;
          t.tm_mon = m - 1;
          t.tm_year = y - 1900;
          t.tm_isdst = -1;
  
          // normalize:
          time_t when = mktime(&amp;t);
  
          time_t tNow;
  
        //   time(&amp;tNow);
  
            const struct tm *norm = localtime(&amp;when);
  
        //  const struct tm *now = localtime(&amp;tNow);       /* when I uncomment this line the code                              
        //                                                   does not accept future date   */
  
  
  
        // validate (is the normalized date still the same?):   
          return (norm-&gt;tm_mday == d    &amp;&amp;
                  norm-&gt;tm_mon  == m - 1 &amp;&amp;
                  norm-&gt;tm_year == y - 1900);
      }
      return false;
  }
  
  
  int main() {
  
      std::string s("11/01/2015");
      int d,m,y;
  
      if (extractDate(s, d, m, y))
          std::cout &lt;&lt; "date " 
                    &lt;&lt; d &lt;&lt; "/" &lt;&lt; m &lt;&lt; "/" &lt;&lt; y
                    &lt;&lt; " is valid" &lt;&lt; std::endl;
      else
          std::cout &lt;&lt; "date is invalid" &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>When I uncomment the <code>const struct tm *now = localtime(&amp;tNow);</code> the code gives correct output as "invalid date" for any future date values...but why is this happening.I am getting correct output but I want to know why.</p>