Calculate the end of the previous quarter
<p>I'm looking for an elegant and pythonic way to get the date of the end of the previous quarter.</p>

<p>Something like this:</p>

<pre><code>def previous_quarter(reference_date):
   ...


&gt;&gt;&gt; previous_quarter(datetime.date(2013, 5, 31))
datetime.date(2013, 3, 31)

&gt;&gt;&gt; previous_quarter(datetime.date(2013, 2, 1))
datetime.date(2012, 12, 31)

&gt;&gt;&gt; previous_quarter(datetime.date(2013, 3, 31))
datetime.date(2012, 12, 31)

&gt;&gt;&gt; previous_quarter(datetime.date(2013, 11, 1))
datetime.date(2013, 9, 30)
</code></pre>

<p><strong>Edit: Have I tried anything?</strong></p>

<p>Yes, this seems to work:</p>

<pre><code>def previous_quarter(ref_date):
    current_date = ref_date - timedelta(days=1)
    while current_date.month % 3:
        current_date -= timedelta(days=1)
    return current_date
</code></pre>

<p>But it seems unnecessarily iterative.</p>
