I'm new to python and am trying to do something that seems trivial: I want to add an if-not-modified header to an http request passing the time, now minus 60 seconds. I'm running into a bunch of difficulty with the now() - 60 seconds part.
I've looked at this How do I convert local time to UTC in Python?, this How can I convert a datetime object to milliseconds since epoch (unix time) in Python?, and many other questions but there has to be a more straightforward way than this sort of approach:
time.strftime("%Y-%m-%d %H:%M:%S",
time.gmtime(time.mktime(time.strptime("2008-09-17 14:04:00",
"%Y-%m-%d %H:%M:%S"))))
to get the correct time and pass it as an arg to addheaders.
Here's my code so far:
interval = 60 #changes other places
timestamp = datetime.datetime.now() - datetime.timedelta(seconds=interval)
opener.addheaders("if-modified-since", time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime(time.mktime(time.strptime(timestamp, "%Y-%m-%d %H:%M:%S")))))
which throws a TypeError: expected string or buffer
but all in all, it seems like utter insanity to go through this much munging around to get something as simple as now() + 60 seconds in a UTC string. Can someone with more python ninja-foo please help me see the error of my ways?