Outils pour utilisateurs

Outils du site


issue203:python

Ceci est une ancienne révision du document !


Fun and Games with Dates Greetings fellow Beings. I hope that your year is going well for you so far. I suppose that I really should preface this month’s article by explaining the title. “Fun and Games with Dates” really isn’t what I wanted to name this month’s article. In reality, it’s more like “Pain, Suffering and Angst with Dates”. But like the old dad joke “Why does McDonalds call them ‘Happy Meals’? Because if they called them ‘Sad Meals’, no one would buy them!”, I figured if I named it ‘My horrible experience with Dates’, either you wouldn’t want to read it OR you would assume that I was talking about an experience trying to date someone. This article started with an idea for my Micro This Micro That article series. This weekend (as I’m writing this) is the switch to Daylight Saving Time here in most of the U.S. and I was thinking about a Microcontroller program that needs to keep track of the actual time and date, making sure that, after a power failure or reboot of the device, the device goes up to the internet, gets the current time and date. Then what happens when Daylight Saving time hits? Here in the U.S. we change to Daylight Saving on the Second Sunday of March at 02:00 and change back to “Standard” time the first Sunday in November at 02:00. Never having lived in any other part of the world, and time change has not been a major conversation topic between my international friends and myself, I wondered if the rest of the world had this issue. With a little digging on the Internet, I found out.

Some areas of the world change to Daylight Saving on the Second Sunday in March at 02:00, some on the Second Sunday in March at 00:00, some on the last Sunday in March at 01:00 UTC, some on the Last Sunday in March at 02:00, some on the Friday before the last Sunday in March, some on the last Friday in April at 00:00, some on the first Sunday in October at 00:00, and others at even more difficult times to remember, unless you live there. And the switch back to “regular” time can even be more convoluted. (https://en.wikipedia.org/wiki/Daylight_saving_time_by_country#) Knowing this caused me to push things around in the “to write about” queue and sit down and start digging into research and testing code. So keeping with proper programming protocol, I needed to define the issue and lay out the steps that will need to be taken to complete the task. For those of us that identify with “human” (as opposed to silicon based computer lifeforms), the task is easy. IF this is the day before a time change, then… IF the change adds an hour, then.. Before going to bed, move the clock ahead one hour. ELSE IF the change subtracts an hour, then… Before going to bed, move the clock back one hour. ELSE IF the change adds 30 minutes, then… Before going to bed, move the clock ahead 30 minutes. ELSE IF the change subtracts 30 minutes, then… Before going to bed, move the clock back 30 minutes. ELSE Get rid of all the analogue clocks in the house and let the computers handle things!

Ok. MAYBE I over simplified the situation and the ending ELSE task MIGHT be a bit drastic, but hey. What can I say? AND YES, there is an area in the world that only changes the time by 30 minutes. Anyway, now how do we go about coding this? I won’t bore you with 10 pages of failed attempts (not that they were all failed attempts, but none that I was pleased with). I will, however, share the solution that I found that I liked. The key to the entire thing is to use a library named dateutil. It’s simple to install with pip. To walk you through the process, I’ll use PtPython. Once you have it installed, we can import it and datetime (top right). Once we have that we want to find the first Sunday of the month. Since we imported everything from dateutil.relativedelta, that includes weekday and a two-character abbreviation for the days of the week. In our case, we want “SU” for Sunday (below). While we are here, let’s get the date for the End of Daylight Saving time (in the U.S.) which is the FIRST Sunday in November 2024. This time, it’s easier, since we don’t need to add the 7 days (bottom right). I created two one-liners for all of the above steps. I simply added a year variable instead of “hardcoding” the year in the datetime.date call (below).

Now that we have our one-liners defined, we can use it to find any Daylight Saving time start and end dates. So for the US, here's a quick program that will get the start and end dates for the years from 2024 to 2030 (next page, top right). That’s all well and good, but I feel that it’s safe to assume that there are many fewer readers of FCM in the U.S. than there are in the rest of the world. The second largest group of countries (which probably includes you, dear reader) starts their time change on the last Sunday of March at 00:00 UTC and ends it on the last Sunday of October at 01:00 UTC. It’s a very simple task to modify the code to handle the last Sunday instead of the second Sunday. So we can use a bit of syntax change for our relativedelta call. dstStart = datetime.date(year, 3, 1) + relativedelta(day=31, weekday=SU(-1)) In this case, we will give it the same datetime.date value, but we’ll want to set the relativedelta to start its calculations from the 31st of the month and find the nearest Sunday with a “-1” modifier, since we are going back from the 31st. For the last Sunday of October, that’s a simple change from 3 for the month (in the datetime.date) to 10. dstEnd = datetime.date(year, 10, 1) + relativedelta(day=31, weekday=SU(-1)) So making the changes to the above program the code would be (bottom right)…

So until someone gets things changed, this will do. For those of you who don’t fall into the first two groups, you should have enough information to change things to get the dates for the time changes for your country. If you want to learn more about the dateutil package, the documentation can be found at https://dateutil.readthedocs.io/en/stable/index.html . Remember that this whole thing started because I wanted to have a Microcontroller know when the time change actually occurs. We have addressed only the date portion. The time part has yet to be finished. That will be shown in my Micro This Micro That article in this month’s Full Circle Magazine. I’ve placed the code for the two sample programs on my github repository at https://github.com/gregwa1953/FCM-203. Until next time, as always; stay safe, healthy, positive and creative!

issue203/python.1712037244.txt.gz · Dernière modification : 2024/04/02 07:54 de d52fr