Python : Convertir une date du format timestamp vers une date

Publié le par Chtof

Principalement dans des fichiers de log, pour des raisons de performance, les dates sont parfois enregistrés au format timestamp. Difficile alors de savoir au premier coup d'oeil à quel date correspond le "1206527190" ! La fonction qui suit pourra vous aider dans vos développements de scripts en langage Python. Dans la partie "MAIN", vous trouverez un exemple de son utilisation (timestamp et format souhaité passés en paramètres) :


#!/usr/bin/python

import datetime

from time import gmtime, strftime

 

# FUNCTION:     timestampToDate

# DESCRIPTION:  Convert a timestamp according a given pattern

# ARGS:         - timestamp:    The timestamp to be converted

#               - datePattern:  The wished date format

def timestampToDate(timestamp, datePattern):

  return strftime("%b %d %Y %H:%M", gmtime(float(timestamp)))

 

# MAIN #############################################

date=timestampToDate("1206527190", "%b %d %Y %H:%M")

print date



Publié dans Programmation

Pour être informé des derniers articles, inscrivez vous :
Commenter cet article