Handling Leap Years: Relativedelta

Handling Leap Years: Relativedelta

So I have been set a wonderful wonderful homework by my mentor just before going on my retreat but due to being on holiday and then a few bumps when I got back I am only just finally settling down to do it now. So let us remember; just before I went on holiday, I was learning about the timedelta object. This is an object that in Python represents a difference in a period of time between two times; this could be days, hours, months etc. and get very specific right down to microseconds (or even nanoseconds - I'm not sure - please someone tell me).

Images of code and clocks in neon pink, purple and green. Text reads: the relativedelta type. Handling more specific time difference intricacies.

But as I handled timedelta problems on the train to Lincoln with patchy internet, I noticed a problem.

When there was a series of years with leap years in it, then I had to adjust my calculations. 

But apparently there is a way in Python to handle leap years, so let's try this out. So here is my code from the last time - except that I have now made it time zone aware so that it is in British Summer Time because I am now super smart and I can now do these things!!

And here is what it outputs today:


But notice how I had to subtract 731 days as opposed to 730 because of the leap year. So I can handle this please by using the relativedelta object please? arrrrgh

This is my homework

This bit is my homework. Okay, wow. Arrrgh. What to do - first things first - look up relativedelta.

I have resisted the urge to use ChatGPT

Because I am very very tired, and I have been working very hard, but this is not how we resolve our coding problems.

Okay, I found some documentation: "The realtivedelta type is designed to be applied to an existing datetime and can replace specific components of that datetime (??!), or represents an interval of time."

My mentor has been teaching me to infer things from the code

My wonderful wonderful amazing mentor who teaches me so much has taught me that I should not read the docs in some cases where they are long and there is a lot that we can infer from the code. (This is to reduce cognitive load and is especially good for me because I have issues with reading a lot).

I have read through the code in the docs above - I highly recommend it. It is too much for me to all paste in here but it seems to explain:

Adding a month through relativedelta() will follow all kinds of logic - like if I am on the last day of a month and it is the 31st, then if I add one month, then the relativedelta method will give me the last day of the next month - whether that be the 28th, 30th, or 31st! I like this logic.

In a similar vein then it will also return a leap year to me correctly:


Notice a few things. We use a "+" for relativedelta but then we specify whether it is PLUS or MINUS after the equals '=' inside of the brackets.

Don't ask me way.

This is what they did in the example.

I followed the docs.


And so it works. 

I have completed my homework assignment. Do I know everything? No.

But I certainly know more than I did before.

Help! There is a bit that I don't understand


I am struggling. 

I have even asked ChatGPT but I still don't understand. Why do we REPLACE the original datetime as opposed to adding to it? 

Oh I see. You can specify the month of the year and the year itself OR you can do arithmetic operators.

Right? You can do both it seems, right? Right.

So you can do 

relativedelta(year=2023, month =6) to make it June 2023

or you can do

relativedelta(years=+1, days=+1) and that would make it the 16th of June 2024 relative to today.

Have I done this one right?

I am open to feedback as this has been a tricky one. Please? Thanks!!!

Just because it's been really useful here's a little summary from chat GPT. Sorry.


And a little table too, please. Sorry.



Comments

Popular Posts