Re: Subtract n months from datetime

2022-06-27 Thread Peter J. Holzer
On 2022-06-21 05:29:52 +0100, Paulo da Silva wrote: > I implemented a part of a script to subtract n months from datetime. > Basically I subtracted n%12 from year and n//12 from the month adding 12 > months when it goes<=0. Then used try when converting to datetime again. So, > if the day is for

Aw: Re: Subtract n months from datetime [Why?]

2022-06-23 Thread Karsten Hilbert
> What makes sense depends on where you're looking from. > > It's 28 February, you need to keep it for 5 years, therefore you could > reason that you can dispose of it on 28 February, 5 years hence. > > However, that happens to be a leap year. > > Should you still have it on 29 February? Nope

Re: Subtract n months from datetime

2022-06-22 Thread Paulo da Silva
Às 19:47 de 22/06/22, Marco Sulla escreveu: The package arrow has a simple shift method for months, weeks etc https://arrow.readthedocs.io/en/latest/#replace-shift At first look it seems pretty good! I didn't know it. Thank you Marco. Paulo --

Re: Subtract n months from datetime [Why?]

2022-06-22 Thread Paulo da Silva
Às 20:25 de 22/06/22, Barry Scott escreveu: On 22 Jun 2022, at 17:59, Paulo da Silva wrote: Às 05:29 de 21/06/22, Paulo da Silva escreveu: As a general response to some comments ... Suppose we need to delete records from a database older than ... Today, it's usual to specify days. For

Re: Subtract n months from datetime [Why?]

2022-06-22 Thread MRAB
On 2022-06-22 20:25, Barry Scott wrote: On 22 Jun 2022, at 17:59, Paulo da Silva wrote: Às 05:29 de 21/06/22, Paulo da Silva escreveu: As a general response to some comments ... Suppose we need to delete records from a database older than ... Today, it's usual to specify days. For

Re: Subtract n months from datetime [Why?]

2022-06-22 Thread Barry Scott
> On 22 Jun 2022, at 17:59, Paulo da Silva > wrote: > > Às 05:29 de 21/06/22, Paulo da Silva escreveu: > > As a general response to some comments ... > > Suppose we need to delete records from a database older than ... > Today, it's usual to specify days. For example you have to keep some

Re: Subtract n months from datetime

2022-06-22 Thread Marco Sulla
The package arrow has a simple shift method for months, weeks etc https://arrow.readthedocs.io/en/latest/#replace-shift -- https://mail.python.org/mailman/listinfo/python-list

Re: Subtract n months from datetime [Why?]

2022-06-22 Thread MRAB
On 2022-06-22 17:59, Paulo da Silva wrote: Às 05:29 de 21/06/22, Paulo da Silva escreveu: As a general response to some comments ... Suppose we need to delete records from a database older than ... Today, it's usual to specify days. For example you have to keep some gov papers for 90 days.

Re: Subtract n months from datetime [Why?]

2022-06-22 Thread Paulo da Silva
Às 05:29 de 21/06/22, Paulo da Silva escreveu: As a general response to some comments ... Suppose we need to delete records from a database older than ... Today, it's usual to specify days. For example you have to keep some gov papers for 90 days. This seems to come from computers era. In our

Re: Subtract n months from datetime

2022-06-21 Thread Cameron Simpson
On 21Jun2022 17:02, Paulo da Silva wrote: >I have a datetime, not a date. Then you need a date. I would break the datetime into a date and a time, then do the months stuff to the date, then compose a new datetime from the result. >Anyway, the use of calendar.monthrange simplifies the task a

Re: Subtract n months from datetime

2022-06-21 Thread Paulo da Silva
Às 05:44 de 21/06/22, Paul Bryan escreveu: Here's how my code does it: import calendar def add_months(value: date, n: int):   """Return a date value with n months added (or subtracted if negative)."""   year = value.year + (value.month - 1 + n) // 12   month = (value.month - 1 + n) % 12 +

Re: Subtract n months from datetime

2022-06-21 Thread Dan Stromberg
On Mon, Jun 20, 2022 at 9:45 PM Paul Bryan wrote: > Here's how my code does it: > > > import calendar > > def add_months(value: date, n: int): > """Return a date value with n months added (or subtracted if > negative).""" > year = value.year + (value.month - 1 + n) // 12 > month =

Re: Subtract n months from datetime

2022-06-21 Thread Richard Damon
On 6/21/22 12:29 AM, Paulo da Silva wrote: Hi! I implemented a part of a script to subtract n months from datetime. Basically I subtracted n%12 from year and n//12 from the month adding 12 months when it goes<=0. Then used try when converting to datetime again. So, if the day is for example

Re: Subtract n months from datetime

2022-06-20 Thread Paul Bryan
Here's how my code does it: import calendar def add_months(value: date, n: int):   """Return a date value with n months added (or subtracted if negative)."""   year = value.year + (value.month - 1 + n) // 12   month = (value.month - 1 + n) % 12 + 1   day = min(value.day,