title: Parsing a time string in portable c++
body: <p>Can anyone suggest a nicer way of writing the following piece of code?</p>
  
  <p>I'd like to avoid using <code>boost::time</code> as that raises linking issues, and can't use <code>strptime()</code> because I'm on msvc.</p>
  
  <pre><code>#include &lt;ctime&gt;
  
  int strtomonth(string s)
  {
      const char *months[12] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
      for (int i=0;i&lt;12;i++)
          if (strcmp(months[i],s.c_str())==0)
              return i;
      return -1;
  }
  
  time_t get_compilation_time()
  {
      stringstream s;
      s &lt;&lt; __TIMESTAMP__; // formatted like "Tue Dec  6 13:42:54 2011"
      string weekday,month;
      int day,h,m,sec,year;
      char colon;
      s &gt;&gt; weekday &gt;&gt; month &gt;&gt; day &gt;&gt; h &gt;&gt; colon &gt;&gt; m &gt;&gt; colon &gt;&gt; sec &gt;&gt; year;
  
      tm compile_time;
      compile_time.tm_sec = 0;
      compile_time.tm_min = 0;
      compile_time.tm_hour = 0;
      compile_time.tm_mday = day;
      compile_time.tm_mon = strtomonth(month);
      compile_time.tm_year = year-1900;
      compile_time.tm_isdst = 0;
      compile_time.tm_wday = 0;
      compile_time.tm_yday = 0;
  
      time_t cmptime = mktime(&amp;compile_time);
      return cmptime;
  }
  </code></pre>
  
  <p>(I appreciate in this case I could make a pre-build step in python or similar to create a "buildtime.h" and save me the runtime computation, but as this now works and overheads aren't an issue I'll stick with it).</p>
  
  <p>But... C-style struct initialization with undefined behaviour if I get it wrong, references in what should be a purely functional piece of code, declaring variables which get discarded, parsing month strings <em>myself</em> ... yuck!  Is there a better way?</p>