title: Parsing date which may or may not contain milliseconds
body: <p>So this question is more of best way to handle this sort of input in python. Here is an example of input date <code>2018-12-31 23:59:59.999999</code>. The millisecond part may or may not be part of input. </p>
  
  <p>I am currently using this code to convert this to datetime</p>
  
  <pre><code>input_ts = datetime.datetime.strptime(input_str, '%Y-%m-%dT%H:%M:%S.%f')
  </code></pre>
  
  <p>But the problem in this case is that it will throw an exception if input string doesn't contain milliseconds part i.e., <code>2018-12-31 23:59:59</code></p>
  
  <p>In Java, I could have approached this problem in two ways. (its a pseudo explanation, without taking into account of small boundary checks)</p>
  
  <ol>
  <li><p>(preferred approach). Check the input string length. if its less than <strong>19</strong> then it is missing milliseconds. Append <code>.000000</code> to it. </p></li>
  <li><p>(not preferred). Let the main code parse the string, if it throws an exception, then parse it with new time format i.e., <code>%Y-%m-%dT%H:%M:%S</code></p></li>
  <li><p>The third approach could be just strip off milliseconds. </p></li>
  </ol>
  
  <p>I am not sure if python has anything built-in to handle these kind of situations. Any suggestions?</p>