Comment obtenir UTC à partir du fuseau horaire? [dupliquer]
Jan 08 2021
Comment obtenir UTC à partir du fuseau horaire avec python?
Fuseau horaire: Asia/Pontianak
Du fuseau horaire (Asie / Pontianak), il en résultera +7
, +8
ou quelque chose comme ça.
Réponses
toRex Jan 08 2021 at 18:42
import pytz
from datetime import datetime, timezone
get_time = pytz.timezone('Asia/Pontianak').localize(datetime.now())
print(get_time)
Production
2021-01-08 17: 41: 34.686607 + 07: 00
Maintenant, pour obtenir le résultat du fuseau horaire
now_utc = datetime.now(timezone.utc)
print(now_utc)
TERMINATOR Jan 08 2021 at 18:42
Afin de travailler avec des fuseaux horaires en python, il est nécessaire d'utiliser la pytz
bibliothèque Python.
La première étape consiste à installer pytz
car ce n'est pas une bibliothèque standard:
pip install pytz
OU ALORS:
pip3 install pytz
Alors voici le code:
from datetime import datetime
import pytz
UTC = pytz.utc #storing the UTC property for later
time_zone = pytz.timezone('Asia/Pontianak') #get the local timzone for later
local_date_time = datetime.now(time_zone) #Formating the time to Asia/Pontianak
print(local_date_time)
#2021-01-08 18:57:02.691163+07:00
# ...and to UTC:
date_time_utc = local_date_time.astimezone(UTC)
print(date_time_utc)
#2021-01-08 18:57:02.691163+07:00