Convertir 'H:M:S' a segundos.

En ocacaciones necesitamos convertir formato 'H:M:S' y hacerlo de una manaera mas pythonista... Lo vamos hacer bajo python3.3


>>> t = "1:23:45"
>>> print (sum(int(x) * 60 ** i for i,x in enumerate(reversed(t.split(':')))))
5025



>>> import time
>>> from datetime import datetime
>>> ahora = time.time()
>>> print (ahora)
1395079433.995347
>>> diferencia = datetime.fromtimestamp(time.time() - ahora).srtftime('%H:%M:%S')
>>> print (diferencia)
'00:01:01'
>>> print (sum(int(x) * 60 ** i for i,x in enumerate(reversed(diferencia.split(':')))))
61


Entradas más populares de este blog

Convertir disco virtualbox.ova a qemu.qcow2

Mi cambio de asterisk a FreeSWITCH

Python, Redis una combinacion estupenda.

disqus

comments powered by Disqus