From d8509ee5370742b5c32e0f7fdb1ae42d7e6cc2fc Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Mon, 16 Jul 2012 06:58:36 +0200 Subject: [PATCH] python: Added HTTP date formatting functions to ashd.wsgiutil. --- python/ashd/wsgiutil.py | 14 ++++++++++++++ python3/ashd/wsgiutil.py | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/python/ashd/wsgiutil.py b/python/ashd/wsgiutil.py index b947407..711435f 100644 --- a/python/ashd/wsgiutil.py +++ b/python/ashd/wsgiutil.py @@ -1,3 +1,5 @@ +import time + def htmlquote(text): ret = "" for c in text: @@ -27,3 +29,15 @@ def simpleerror(env, startreq, code, title, msg): """ % (title, title, htmlquote(msg)) startreq("%i %s" % (code, title), [("Content-Type", "text/html"), ("Content-Length", str(len(buf)))]) return [buf] + +def httpdate(ts): + return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(ts)) + +def phttpdate(dstr): + tz = dstr[-6:] + dstr = dstr[:-6] + if tz[0] != " " or (tz[1] != "+" and tz[1] != "-") or not tz[2:].isdigit(): + return None + tz = int(tz[1:]) + tz = (((tz / 100) * 60) + (tz % 100)) * 60 + return time.mktime(time.strptime(dstr, "%a, %d %b %Y %H:%M:%S")) - tz - time.altzone diff --git a/python3/ashd/wsgiutil.py b/python3/ashd/wsgiutil.py index 5fe7535..896af61 100644 --- a/python3/ashd/wsgiutil.py +++ b/python3/ashd/wsgiutil.py @@ -1,3 +1,5 @@ +import time + def htmlquote(text): ret = "" for c in text: @@ -28,3 +30,15 @@ def simpleerror(env, startreq, code, title, msg): buf = buf.encode("ascii") startreq("%i %s" % (code, title), [("Content-Type", "text/html"), ("Content-Length", str(len(buf)))]) return [buf] + +def httpdate(ts): + return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(ts)) + +def phttpdate(dstr): + tz = dstr[-6:] + dstr = dstr[:-6] + if tz[0] != " " or (tz[1] != "+" and tz[1] != "-") or not tz[2:].isdigit(): + return None + tz = int(tz[1:]) + tz = (((tz / 100) * 60) + (tz % 100)) * 60 + return time.mktime(time.strptime(dstr, "%a, %d %b %Y %H:%M:%S")) - tz - time.altzone -- 2.11.0