[Tutor] Formatting questions regarding datetime.isoformat()

Peter Otten __peter__ at web.de
Thu Sep 6 09:39:19 CEST 2012


staticsafe wrote:

> Hello,
> 
> I am running Python 2.6.6 on a Debian Squeeze system. I am using two
> modules in this bit of code - datetime and python-tvrage (available on
> pypy here: http://pypi.python.org/pypi/python-tvrage/).
> 
> My goal is to find the time remaining until a certain show airs. Here is
> some code I have written so far:
> 
> #!/usr/bin/env python
> from tvrage import quickinfo
> from datetime import tzinfo, timedelta, datetime
> 
> showinfo = quickinfo.fetch("Warehouse 13")
> nextairdate = showinfo['RFC3369']
> now = datetime.now().isoformat()
> 
> Now for my question/problem.
> 
> In [68]: showinfo['RFC3339']
> Out[68]: '2012-09-10T21:00:00-4:00'
> 
> In [72]: datetime.now().isoformat()
> Out[72]: '2012-09-05T22:47:46.061688'
> 
> isoformat() is in a different format than the value returned by the API.
> My question is how would one format both values so that I can perform a
> timedelta on them as is the purpose of my code?
> 
> I hope I have provided the right amount of detail for my question.

After some trial and error:

import dateutil.parser # python-dateutil
import pytz # python-tz
import datetime

show_start = dateutil.parser.parse("2012-09-10T21:00:00-0400")
now = datetime.datetime.now(pytz.timezone("EST"))

print "show starts: ", show_start
print "current time:", now
print "days to go:  ", show_start - now

(Caveat: I'm using Python 2.7)



More information about the Tutor mailing list