Re: Add a month

2006-02-17 Thread Gregory Piñero
Ok, 30 minutes later, here's my best solution.

http://www.answermysearches.com/index.php/how-to-add-months-to-a-date-in-python/53/

(Posted on my website to make sure tabs are kept.)

Would someone mind double-checking my logic before I put this into
production next Tuesday?

-Greg


On 2/17/06, Gregory Piñero <[EMAIL PROTECTED]> wrote:
> Actually, no wait, that's bad.  It doesn't increment the year.
>
> Does anyone have a simple way to code this?
>
> -Greg
>
>
> On 2/17/06, Gregory Piñero <[EMAIL PROTECTED]> wrote:
> > Here's how I do it:
> >
> > def monthify(anint):
> > if anint%12==0:return 12
> > else:return anint%12
> >
> > import datetime
> > d=datetime.datetime.today()
> > dplus1month=datetime.datetime(d.year,monthify(d.month+1),d.day)
> >
> > We need monthify because adding 1 to 12 is bad otherwise!
> >
> > -Greg
> >
> > On 17 Feb 2006 04:15:39 -0800, Paul Boddie <[EMAIL PROTECTED]> wrote:
> > > Fredrik Lundh wrote:
> > > >
> > > > what do you expect d_new to be after the operation ?  if the answer
> > > > is date(2006,3,17), what's date(2006,1,31) plus one month?
> > >
> > > February 31st, of course:
> > >
> > > http://sql-info.de/mysql/gotchas.html#1_14
> > >
> > > ;-)
> > >
> > > Paul
> > >
> > > --
> > > http://mail.python.org/mailman/listinfo/python-list
> > >
> >
> >
> > --
> > Gregory Piñero
> > Chief Innovation Officer
> > Blended Technologies
> > (www.blendedtechnologies.com)
> >
>
>
> --
> Gregory Piñero
> Chief Innovation Officer
> Blended Technologies
> (www.blendedtechnologies.com)
>


--
Gregory Piñero
Chief Innovation Officer
Blended Technologies
(www.blendedtechnologies.com)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Add a month

2006-02-17 Thread Jarek Zgoda
Paul Boddie napisał(a):

>>what do you expect d_new to be after the operation ?  if the answer
>>is date(2006,3,17), what's date(2006,1,31) plus one month?
> 
> February 31st, of course:
> 
> http://sql-info.de/mysql/gotchas.html#1_14

MS SQL Server documentation marks dateadd() result as non-deterministic
in Transact-SQL reference. Adding 1 month to jan 31st gives feb 28 or
29, depending on year. It's not that bad, providing you know this
behaviour. ;)

-- 
Jarek Zgoda
http://jpa.berlios.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Add a month

2006-02-17 Thread Carsten Haese
On Fri, 2006-02-17 at 16:10, Gregory Piñero wrote:
> Actually, no wait, that's bad.  It doesn't increment the year.
> 
> Does anyone have a simple way to code this?
> 
> -Greg
> 
> 
> On 2/17/06, Gregory Piñero <[EMAIL PROTECTED]> wrote:
> > Here's how I do it:
> >
> > def monthify(anint):
> > if anint%12==0:return 12
> > else:return anint%12
> >
> > import datetime
> > d=datetime.datetime.today()
> > dplus1month=datetime.datetime(d.year,monthify(d.month+1),d.day)
> >
> > We need monthify because adding 1 to 12 is bad otherwise!
> >
> > -Greg
> >
> > On 17 Feb 2006 04:15:39 -0800, Paul Boddie <[EMAIL PROTECTED]> wrote:
> > > Fredrik Lundh wrote:
> > > >
> > > > what do you expect d_new to be after the operation ?  if the answer
> > > > is date(2006,3,17), what's date(2006,1,31) plus one month?
> > >
> > > February 31st, of course:
> > >
> > > http://sql-info.de/mysql/gotchas.html#1_14
> > >
> > > ;-)
> > >
> > > Paul
> > >
> > > --
> > > http://mail.python.org/mailman/listinfo/python-list
> > >
> >
> >
> > --
> > Gregory Piñero
> > Chief Innovation Officer
> > Blended Technologies
> > (www.blendedtechnologies.com)
> >
> 
> 

I don't know if this qualifies as simple, but it seems to work:

def addmonths(thedate,months):
  "Add  months to ."
  import datetime
  y,m,d = thedate.timetuple()[:3]
  y2, m2 = divmod(m+months-1, 12)
  return datetime.date(y+y2,m2+1,d)

Note that the date constructor will raise an exception if the result
happens to be an invalid date such as February 30th or November 31st.

-Carsten


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Add a month

2006-02-17 Thread Gregory Piñero
Actually, no wait, that's bad.  It doesn't increment the year.

Does anyone have a simple way to code this?

-Greg


On 2/17/06, Gregory Piñero <[EMAIL PROTECTED]> wrote:
> Here's how I do it:
>
> def monthify(anint):
> if anint%12==0:return 12
> else:return anint%12
>
> import datetime
> d=datetime.datetime.today()
> dplus1month=datetime.datetime(d.year,monthify(d.month+1),d.day)
>
> We need monthify because adding 1 to 12 is bad otherwise!
>
> -Greg
>
> On 17 Feb 2006 04:15:39 -0800, Paul Boddie <[EMAIL PROTECTED]> wrote:
> > Fredrik Lundh wrote:
> > >
> > > what do you expect d_new to be after the operation ?  if the answer
> > > is date(2006,3,17), what's date(2006,1,31) plus one month?
> >
> > February 31st, of course:
> >
> > http://sql-info.de/mysql/gotchas.html#1_14
> >
> > ;-)
> >
> > Paul
> >
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
>
>
> --
> Gregory Piñero
> Chief Innovation Officer
> Blended Technologies
> (www.blendedtechnologies.com)
>


--
Gregory Piñero
Chief Innovation Officer
Blended Technologies
(www.blendedtechnologies.com)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Add a month

2006-02-17 Thread Gregory Piñero
Here's how I do it:

def monthify(anint):
if anint%12==0:return 12
else:return anint%12

import datetime
d=datetime.datetime.today()
dplus1month=datetime.datetime(d.year,monthify(d.month+1),d.day)

We need monthify because adding 1 to 12 is bad otherwise!

-Greg

On 17 Feb 2006 04:15:39 -0800, Paul Boddie <[EMAIL PROTECTED]> wrote:
> Fredrik Lundh wrote:
> >
> > what do you expect d_new to be after the operation ?  if the answer
> > is date(2006,3,17), what's date(2006,1,31) plus one month?
>
> February 31st, of course:
>
> http://sql-info.de/mysql/gotchas.html#1_14
>
> ;-)
>
> Paul
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>


--
Gregory Piñero
Chief Innovation Officer
Blended Technologies
(www.blendedtechnologies.com)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Add a month

2006-02-17 Thread Paul Boddie
Fredrik Lundh wrote:
>
> what do you expect d_new to be after the operation ?  if the answer
> is date(2006,3,17), what's date(2006,1,31) plus one month?

February 31st, of course:

http://sql-info.de/mysql/gotchas.html#1_14

;-)

Paul

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Add a month

2006-02-17 Thread Daniel Dittmar
Fredrik Lundh wrote:
> [EMAIL PROTECTED] wrote:
> 
>> Hi, this is probably a really simple question but...
>> How do you add a month to a datetime date in python? It would be nice
>> if  you could do something like:
>>
>> d = datetime.date(2006,2,17)
>> dm = datetime.timedelta(months=1)
>> d_new = d + dm
>>
>> but timedelta doesn't have a 'months' setting. Am I missing some easy
>> method or do I have to code a work around myself??
> 
> what do you expect d_new to be after the operation ?  if the answer
> is date(2006,3,17), what's date(2006,1,31) plus one month?

Traceback (most recent call last):
   File "", line 1, in ?
ValueError: date domain error

in analogy to math.sqrt (-2)

Daniel
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Add a month

2006-02-17 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote:

> Hi, this is probably a really simple question but...
> How do you add a month to a datetime date in python? It would be nice
> if  you could do something like:
>
> d = datetime.date(2006,2,17)
> dm = datetime.timedelta(months=1)
> d_new = d + dm
>
> but timedelta doesn't have a 'months' setting. Am I missing some easy
> method or do I have to code a work around myself??

what do you expect d_new to be after the operation ?  if the answer
is date(2006,3,17), what's date(2006,1,31) plus one month?





-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Add a month

2006-02-17 Thread Raymond Hettinger
[EMAIL PROTECTED]
> Hi, this is probably a really simple question but...
> How do you add a month to a datetime date in python? It would be nice
> if  you could do something like:
>
> d = datetime.date(2006,2,17)
> dm = datetime.timedelta(months=1)
> d_new = d + dm
>
> but timedelta doesn't have a 'months' setting. Am I missing some easy
> method or do I have to code a work around myself??

You need to code you own version.

It was not included with datetime.timedelta() because the definition is
ambiguous (i.e. what date is one month after Jan 30th?).


Raymond

-- 
http://mail.python.org/mailman/listinfo/python-list


Add a month

2006-02-17 Thread novin01
Hi, this is probably a really simple question but...
How do you add a month to a datetime date in python? It would be nice
if  you could do something like:

d = datetime.date(2006,2,17)
dm = datetime.timedelta(months=1)
d_new = d + dm

but timedelta doesn't have a 'months' setting. Am I missing some easy
method or do I have to code a work around myself??

Thanks in advance for the help!

-- 
http://mail.python.org/mailman/listinfo/python-list