En ella se comenta que a pesar de que python tiene su Zen (import this) hay modulos de la librería estándar que o bien no son simples o se podrían mejorar.
Un claro ejemplo es datetime. ¿Porque no se incluye la timezone por defecto?
En el momento que quieres hacer conversiones entre timezones, necesitas de modulos externos. Y de nuevo nos encontramos con otro de los problemas de python. La multitud de opciones... ¿Cual usar pytz o dateutil?
Os dejo con un programilla que convierte de utc a localtime usando los dos.
from datetime import datetime
import pytz
from dateutil import tz
# The problem with now and utcnow is that they don't set set tzinfo to None
now = datetime.now()
utcnow = datetime.utcnow()
# Wit this one, we are setting the tzinfo (using dateutil)
summertime = datetime(2012,7,1,23,30, tzinfo=tz.gettz('UTC'))
#--- Converson using dateutil
# We are using naive timezones with the exception of summertime
# Instead of using local datetime with tz.tzlocal() we are hardcoding it
print "*** Conversions using dateutil"
print 'now:', now.strftime('%Y-%m-%d %H:%M:%S')
print 'utc:', utcnow.strftime('%Y-%m-%d %H:%M:%S')
print 'summertime:', summertime.strftime('%Y-%m-%d %H:%M:%S')
UTC = tz.gettz('UTC')
HERE = tz.gettz('Europe/Madrid')
utcnow2 = utcnow.replace(tzinfo=UTC)
print 'utc localized:', utcnow2.astimezone(HERE).strftime('%Y-%m-%d %H:%M:%S')
print 'summertime localized:', summertime.astimezone(HERE).strftime('%Y-%m-%d %H:%M:%S')
#--- Converson using pytz
# Pay attention to how we can create a utc datetime with timezone using now()
# Instead of using pytz.utz we could also use pytz.timezone('UTC')
madrid = pytz.timezone('Europe/Madrid')
utc_with_timezone = datetime.now(tz=pytz.utc)
utc_withouth_timezone = datetime.utcnow()
print "*** Conversions using pytz"
print 'with:', utc_with_timezone.strftime('%Y-%m-%d %H:%M:%S')
print 'withouth:', utc_withouth_timezone.strftime('%Y-%m-%d %H:%M:%S')
print 'with localized:', utc_with_timezone.astimezone(madrid).strftime('%Y-%m-%d %H:%M:%S')
# astimezone cannot be applied to a naive time zone so we need to do like with dateutil
# and firtsly we need to localize the datetime
utc_withouth_timezone = pytz.utc.localize(utc_withouth_timezone)
print 'without localized:', utc_withouth_timezone.astimezone(madrid).strftime('%Y-%m-%d %H:%M:%S')
print 'summertime localized:', summertime.astimezone(madrid).strftime('%Y-%m-%d %H:%M:%S')
Salida:
*** Conversions using dateutil
now: 2012-11-13 12:17:01
utc: 2012-11-13 11:17:01
summertime: 2012-07-01 23:30:00
utc localized: 2012-11-13 12:17:01
summertime localized: 2012-07-02 01:30:00
*** Conversions using pytz
with: 2012-11-13 11:17:01
withouth: 2012-11-13 11:17:01
with localized: 2012-11-13 12:17:01
without localized: 2012-11-13 12:17:01
summertime localized: 2012-07-02 01:30:00
Vamos que da un poco igual usar uno que otro...
En el caso de pytz en lugar de usar localize() podríamos haber hecho uso de replace() como si se hace con dateutil.
Si teneis que usar offets, en lugar de zonas, dateutil los soporta. Mi balanza a priori se inclina para dateutil por ese motivo. Ejemplo:
from datetime import datetime
from dateutil import tz
HERE = tz.tzlocal()
OFFSET = tz.tzoffset(None, 3600)
UTC = tz.gettz('UTC')
now = datetime.now(UTC)
here = now.replace(tzinfo=HERE)
offset = now.replace(tzinfo=OFFSET)
print 'here:', here.astimezone(HERE).strftime('%Y-%m-%d %H:%M:%S')
print 'offset:', offset.astimezone(OFFSET).strftime('%Y-%m-%d %H:%M:%S')
No hay comentarios:
Publicar un comentario