title: Alter day or month of datetime.date without subclass or helper functions that use the constructor
body: <p>I receive variously formatted dates or parts of dates and after getting them into <code>datetime.date</code> format, I would like to adjust the month field or the day field without calling the constructor for a whole new <code>datetime.date</code> object.</p>
  
  <p>The data can be any of the following kinds of things:</p>
  
  <pre><code>'2012.01.01'
  '194311'
  '1865/08/30'
  '1701'
   ... etc
  </code></pre>
  
  <p>For underspecified dates, I will want to push them either to January 1, to the first of the provided month, to the end of the provided month, or to December 31. A year must be provided and won't be changed.</p>
  
  <p><code>dateutil.parser.parse</code> handles all of these nicely, except for the convention that it fills in missing months and/or days with the current month and/or day:</p>
  
  <pre><code>In [11]: dateutil.parser.parse('2012/04')
  Out[11]: datetime.datetime(2012, 4, 10, 0, 0)
  
  In [12]: dateutil.parser.parse('1865')
  Out[12]: datetime.datetime(1865, 2, 10, 0, 0)
  
  In [13]: dateutil.parser.parse('1920.03.27')
  Out[13]: datetime.datetime(1920, 3, 27, 0, 0)
  </code></pre>
  
  <p>After getting into <code>datetime.datetime</code> format, I can call the <code>datetime</code> function <code>date</code> to effectively cast as a <code>datetime.date</code>, so that part is trivial.</p>
  
  <p>What I would like to avoid is writing a helper function that must go through the pains of calling a constructor, like so:</p>
  
  <pre><code>def adjust_to_jan1(date_str):
      temp = dateutil.parser.parse(date_str).date()
      return datetime.date(temp.year, 1, 1)
  </code></pre>
  
  <p>or</p>
  
  <pre><code>def adjust_to_month_end(date_str):
      import calendar
      temp = dateutil.parser.parse(date_str).date()
      last_day = calendar.monthrange(temp.year, temp.month)
      return datetime.date(temp.year, temp.month, last_day)
  </code></pre>
  
  <p>or </p>
  
  <pre><code>def adjust_to_last_weekdat(date_str):
      import calendar
      temp = dateutil.parser.parse(date_str).date()
      last_weekday = max([elem for x in calendar.monthcalendar(temp.year, 
                                                               temp.month) 
                          for elem in x[0:5]])
      return datetime.date(temp.year, temp.month, last_weekday)
  </code></pre>
  
  <p>Since I work with this data interactively very often, it gets to be a pain to define these kinds of functions all the time. It gets more complicated: last week day that's not a holiday in some arbitrary group of countries... last weekday except it can be last Saturday if the year is before a certain year...</p>
  
  <p>What I'd like to do is just <em>declare</em> the change that I want, such as:</p>
  
  <pre><code>some_date = datetime.date(2012, 1, 20)
  some_date.day = 1
  </code></pre>
  
  <p>But this is not allowed.</p>
  
  <p>I could subclass <code>datetime</code> but then I have to worry about my subclass not doing any damage and whether it's portable for other folks using some of the scripts that come from the interactive work.</p>
  
  <p>Any other solutions would be appreciated.</p>
  
  <p>If you don't think there is a better approach than either subclassing or writing lots of these helper functions, that's fine, but I'd prefer to leave comments and answers <em>only</em> for any proposals that are different from that -- rather than getting lots of responses that say "This looks fine as it is" or something.</p>