I am using Python to construct a proxy server as an exercise and I want to compare two different strings of time received from a server. For example,
Date: Sun, 24 Nov 2013 18:34:30 GMT
Expires: Sat, 23 Nov 2013 18:34:30 GMT
How can I compare whether the expiry time is earlier than the current time? Do I have to parse it using the strptime
method of the datetime
module or is there an easier way to do so?
Convert each of the strings to a timestamp and compare these, for example as follows:
from datetime import datetime
date1 = "Date: Sun, 24 Nov 2013 18:34:30 GMT"
date2 = "Expires: Sat, 23 Nov 2013 18:34:30 GMT"
format = "%a, %d %b %Y %H:%M:%S %Z"
if datetime.strptime(date1, "Date: " + format) >= datetime.strptime(date2, "Expires: " + format):
print "Expired"