]> git.dolda2000.com Git - ashd.git/commitdiff
python: Added HTTP date formatting functions to ashd.wsgiutil.
authorFredrik Tolf <fredrik@dolda2000.com>
Mon, 16 Jul 2012 04:58:36 +0000 (06:58 +0200)
committerFredrik Tolf <fredrik@dolda2000.com>
Mon, 16 Jul 2012 04:58:36 +0000 (06:58 +0200)
python/ashd/wsgiutil.py
python3/ashd/wsgiutil.py

index b947407a675c0b8b252416b1004285b2acaf77c2..711435f525f1d701936e3a2e5990d086a0874239 100644 (file)
@@ -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):
 </html>""" % (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
index 5fe7535140eafc6ae08246126d0cb761656e995c..896af6177e487b7c31641f0c01c639827ddea248 100644 (file)
@@ -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