issue203:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue203:python [2024/03/30 10:12] – créée auntiee | issue203:python [2024/04/03 16:58] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Fun and Games with Dates | + | **Fun and Games with Dates |
Greetings fellow Beings. I hope that your year is going well for you so far. | Greetings fellow Beings. I hope that your year is going well for you so far. | ||
Ligne 5: | Ligne 5: | ||
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. | 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. | + | 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 " | + | Jeux et amusements avec les dates |
+ | |||
+ | Salutations, | ||
+ | |||
+ | Je suppose que je devrais vraiment commencer l' | ||
+ | |||
+ | Cet article est parti d'une idée pour ma série d' | ||
+ | |||
+ | |||
+ | **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 " | ||
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. | 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. | ||
Ligne 25: | Ligne 34: | ||
| | ||
ELSE | ELSE | ||
- | Get rid of all the analogue clocks in the house and let the computers handle things! | + | Get rid of all the analogue clocks in the house and let the computers handle things!** |
+ | |||
+ | Certaines régions du monde passent à l' | ||
- | 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. | + | Sachant cela, j'ai réorganisé la file d' |
+ | |||
+ | Pour respecter le protocole de programmation, | ||
+ | |||
+ | Pour ceux d' | ||
+ | |||
+ | SI nous sommes la veille d'un changement d' | ||
+ | SI le changement ajoute une heure, alors... | ||
+ | Avant d' | ||
+ | OU SI le changement soustrait une heure, alors... | ||
+ | Avant d' | ||
+ | OU SI le changement ajoute 30 minutes, alors... | ||
+ | Avant de vous coucher, avancez l' | ||
+ | OU SI le changement entraîne une soustraction de 30 minutes, alors... | ||
+ | Avant d' | ||
+ | OU | ||
+ | | ||
+ | |||
+ | |||
+ | **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. | 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. | ||
Ligne 37: | Ligne 67: | ||
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). | 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). | + | 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).** |
+ | D' | ||
- | 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). | + | Quoi qu'il en soit, comment allons-nous coder cela ? Je ne vais pas vous ennuyer avec 10 pages de tentatives ratées (non pas qu' |
+ | |||
+ | La clé de tout cela est l' | ||
+ | |||
+ | Une fois que nous avons cela, nous voulons trouver le premier dimanche du mois. Puisque nous avons tout importé de dateutil.relativedelta, | ||
+ | |||
+ | Pendant que nous y sommes, obtenons la date de la fin de l' | ||
+ | |||
+ | J'ai créé deux lignes simples pour toutes les étapes ci-dessus. J'ai simplement ajouté une variable year au lieu de coder en dur l' | ||
+ | |||
+ | |||
+ | **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. | 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. | ||
Ligne 54: | Ligne 96: | ||
dstEnd = datetime.date(year, | dstEnd = datetime.date(year, | ||
- | So making the changes to the above program the code would be (bottom right)… | + | 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. | + | Maintenant que nous avons défini nos deux lignes simples, nous pouvons les utiliser pour trouver les dates de début et de fin de l' |
+ | |||
+ | C'est bien beau, mais je pense que l'on peut supposer qu'il y a beaucoup moins de lecteurs du FCM aux États-Unis qu'il n'y en a dans le reste du monde. Le deuxième groupe de pays le plus important (dont vous faites probablement partie, cher lecteur) commence son changement d' | ||
+ | |||
+ | Il est très simple de modifier le code pour gérer le dernier dimanche au lieu du deuxième dimanche. | ||
+ | |||
+ | Nous pouvons donc utiliser un changement de syntaxe pour notre appel relativedelta. | ||
+ | |||
+ | dstStart = datetime.date(year, | ||
+ | |||
+ | Dans ce cas, nous lui donnerons la même valeur datetime.date, | ||
+ | |||
+ | dstEnd = datetime.date(year, | ||
+ | |||
+ | En modifiant le programme ci-dessus, le code serait donc (en bas à droite). | ||
+ | |||
+ | |||
+ | **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:// | If you want to learn more about the dateutil package, the documentation can be found at https:// | ||
Ligne 64: | Ligne 123: | ||
I’ve placed the code for the two sample programs on my github repository at https:// | I’ve placed the code for the two sample programs on my github repository at https:// | ||
- | Until next time, as always; stay safe, healthy, positive and creative! | + | Until next time, as always; stay safe, healthy, positive and creative!** |
+ | |||
+ | Jusqu' | ||
+ | |||
+ | Si vous voulez en savoir plus sur le paquetage dateutil, la documentation se trouve à l' | ||
+ | |||
+ | Rappelez-vous que tout ceci a commencé parce que je voulais qu'un microcontrôleur sache quand le changement d' | ||
+ | |||
+ | J'ai placé le code des deux exemples de programmes sur mon dépôt github à l' | ||
+ | |||
+ | Jusqu' | ||
+ | |||
+ | //lignes noires de l' | ||
+ | **Next, we need to get a start date, which in our case will be March 1, 2024, and it needs to be a type of ..** | ||
+ | |||
+ | Ensuite, nous devons obtenir la date de début, qui dans notre cas est le 1er mars 2024 et elle doit être du type datetime.date | ||
+ | |||
+ | //lignes noires de l' | ||
+ | **So the first Sunday of March 2024 is the third. Now that we know that, we just need to add 7 days.** | ||
+ | |||
+ | Ainsi, le premier dimanche de mars 2024 est le 3. Maintenant que nous le savons, il suffit d' | ||
+ | |||
+ | |||
+ | **So, the beginning (in the U.S.) of Daylight Saving time in 2024 is March 10. We could also have used the following line instead of “(days=+7)”.** | ||
+ | Ainsi, en 2024, l' | ||
+ | |||
+ | |||
+ | |||
+ | **Which would return the same date. Just two ways of doing the same | ||
+ | thing.** | ||
+ | |||
+ | Qui retourne la même date. Simplement, deux façons de faire la même chose. | ||
+ | |||
+ | |||
+ | //lignes noires des encadrés p 32, en haut à droite// | ||
+ | **And the output would be…** | ||
+ | |||
+ | Et la sortie serait : | ||
+ | |||
+ | **And the output is…** | ||
+ | Et la sortie est : | ||
issue203/python.1711789976.txt.gz · Dernière modification : 2024/03/30 10:12 de auntiee