CheatSheet: Python date, time, timezone
Quelques instructions Python pour gérer l’affichage, la conversion des dates et heures.
from datetime import datetime, timezone, timedelta import pytz from calendar import timegm from time import gmtime, strftime, strptime datetime.now(timezone.utc).astimezone(pytz.timezone('Europe/Paris')).strftime("%d/%m/%Y %H:%M:%S") # # '24/10/2020 14:44:04' # Conversion d'un timestamp UTC en epoch heure locale # metadata_video_begin = '2020-10-22 06:59:59' first_pos_epoch = datetime.fromtimestamp(timegm((strptime(metadata_video_begin, '%Y-%m-%d %H:%M:%S'))), pytz.timezone('Europe/Paris')) ### >>> first_pos_epoch ### datetime.datetime(2020, 10, 22, 8, 59, 59, tzinfo=<DstTzInfo 'Europe/Paris' CEST+2:00:00 DST>) # Calcul d'un delta et formatage # (first_pos_epoch + timedelta(seconds = 120)).strftime("%d/%m/%Y %H:%M:%S") # '22/10/2020 09:01:59'