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 31 for a 30 days month it raises a
ValuError exception. Then I subtract 1 to day and repeat.
The code seems too naive and very very complicated!
What is the best way to achieve this? Any existent module?
At the very end, what I want is to subtract nx where x can be y, m,
w, d
for respectively years, months, weeks or days.
I feel I am missing something here ...
Thanks.
Paulo
I implemented a part of a script to subtract n months from datetime.
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 31 for a 30 days month it raises
a ValuError exception. Then I subtract 1 to day and repeat.
The code seems too naive and very very complicated!
What is the best way to achieve this? Any existent module?
At the very end, what I want is to subtract nx where x can be y, m, w,
d for respectively years, months, weeks or days.
I feel I am missing something here ...
Thanks.
Paulo
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, calendar.monthrange(year, month)[1])
return date(year, month, day)
Here's how my code does it:
import calendarI have a datetime, not a date.
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, calendar.monthrange(year, month)[1])
return date(year, month, day)
Paul
On Tue, 2022-06-21 at 05:29 +0100, 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 31 for a 30 days month it raises a
ValuError exception. Then I subtract 1 to day and repeat.
The code seems too naive and very very complicated!
What is the best way to achieve this? Any existent module?
At the very end, what I want is to subtract nx where x can be y, m,
w, d
for respectively years, months, weeks or days.
I feel I am missing something here ...
Thanks.
Paulo
Any comments are welcome.
I have a datetime, not a date.
Anyway, the use of calendar.monthrange simplifies the task a lot.
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?
À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 minds, however, we immediately think 90 days=3 months.
For example, one may want to delete some files older than 9 months. It's
far more intuitive than 270 days.
When we talk about years it is still going. For example I need to keep
my receipts for 5 years because IRS audits.
Accepting this, it's intuitive, for example, that 3 months before July,
31 is April, 30.
The same happens for the years. 5 years before February, 29 is February, 28.
Again, this is my opinion and that's the way I like it :-)
On 22 Jun 2022, at 17:59, Paulo da Silva <p_d_a_s_i_l_v_a_ns@nonetnoaddress.pt> 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. This seems to come from computers era. In our minds, however, we immediately think 90 days=3 months.
For example, one may want to delete some files older than 9 months. It's far more intuitive than 270 days.
When we talk about years it is still going. For example I need to keep my receipts for 5 years because IRS audits.
Accepting this, it's intuitive, for example, that 3 months before July, 31 is April, 30.
The same happens for the years. 5 years before February, 29 is February, 28.
Again, this is my opinion and that's the way I like it :-)
Regards
Paulo
--
https://mail.python.org/mailman/listinfo/python-list
On 22 Jun 2022, at 17:59, Paulo da Silva <p_d_a_s_i_l_v_a_ns@nonetnoaddress.pt> 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. This seems to come from computers era. In our minds, however, we immediately think 90 days=3 months.
For example, one may want to delete some files older than 9 months. It's far more intuitive than 270 days.
When we talk about years it is still going. For example I need to keep my receipts for 5 years because IRS audits.
Accepting this, it's intuitive, for example, that 3 months before July, 31 is April, 30.
The same happens for the years. 5 years before February, 29 is February, 28.
The advantage of 30 days, 90 days etc is that a contract or law does not need to tell you
how to deal with the problems of calendar months.
As you say in peoples thoughts that 1 month or 3 months etc. But an accounts department
will know how to to the number of days till they have to pay up.
On 22 Jun 2022, at 17:59, Paulo da Silva <p_d_a_s_i_l_v_a_ns@nonetnoaddress.pt> 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. This seems to come from computers era. In our minds, however, we immediately think 90 days=3 months.
For example, one may want to delete some files older than 9 months. It's far more intuitive than 270 days.
When we talk about years it is still going. For example I need to keep my receipts for 5 years because IRS audits.
Accepting this, it's intuitive, for example, that 3 months before July, 31 is April, 30.
The same happens for the years. 5 years before February, 29 is February, 28.
The advantage of 30 days, 90 days etc is that a contract or law does not need to tell you
how to deal with the problems of calendar months.
As you say in peoples thoughts that 1 month or 3 months etc. But an accounts department
will know how to to the number of days till they have to pay up.
The package arrow has a simple shift method for months, weeks etc
https://arrow.readthedocs.io/en/latest/#replace-shift
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 31 for a 30 days month it raises a ValuError exception. Then I subtract 1 to day and repeat.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 296 |
Nodes: | 16 (2 / 14) |
Uptime: | 32:26:39 |
Calls: | 6,648 |
Calls today: | 3 |
Files: | 12,193 |
Messages: | 5,328,599 |