issue203:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue203:python [2024/04/02 07:54] – d52fr | issue203:python [2024/04/03 16:58] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 6: | Ligne 6: | ||
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.** | ||
+ | |||
+ | 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 " | **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 " | ||
Ligne 26: | Ligne 35: | ||
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' | ||
+ | |||
+ | 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' | ||
+ | | ||
+ | | ||
+ | |||
**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. | **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. | ||
Ligne 38: | Ligne 68: | ||
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' | ||
+ | |||
+ | 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' | ||
Ligne 55: | Ligne 97: | ||
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)…** | ||
+ | |||
+ | 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. | **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. | ||
Ligne 65: | Ligne 124: | ||
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.1712037244.txt.gz · Dernière modification : 2024/04/02 07:54 de d52fr