title: Convert Year/Month/Day to Day of Year in Python
body: <p>I'm using the <a href="https://docs.python.org/3/library/datetime.html" rel="noreferrer"><code>datetime</code></a> module, i.e.:</p>
  <pre><code>&gt;&gt;&gt; import datetime
  &gt;&gt;&gt; today = datetime.datetime.now()
  &gt;&gt;&gt; print(today)
  2009-03-06 13:24:58.857946
  </code></pre>
  <p>and I would like to compute the day of year that takes leap years into account. e.g. today (March 6, 2009) is the 65th day of 2009.</p>
  <p>I see a two options:</p>
  <ol>
  <li><p>Create a <code>number_of_days_in_month = [31, 28, ...]</code> array, decide if it's a leap year and manually sum up the days.</p>
  </li>
  <li><p>Use <code>datetime.timedelta</code> to make a guess &amp; then binary search for the correct day of the year:</p>
  <pre><code>&gt;&gt;&gt; import datetime
  &gt;&gt;&gt; YEAR = 2009
  &gt;&gt;&gt; DAY_OF_YEAR = 62
  &gt;&gt;&gt; d = datetime.date(YEAR, 1, 1) + datetime.timedelta(DAY_OF_YEAR - 1)
  </code></pre>
  </li>
  </ol>
  <p>These both feel pretty clunky &amp; I have a gut feeling that there's a more &quot;Pythonic&quot; way of calculating the day of the year. Any ideas/suggestions?</p>