OSDN Git Service

Re implement misc.httpdate_to_secs. use datetime.
authorAiwota Programmer <aiwotaprog@tetteke.tk>
Thu, 21 Sep 2006 19:49:12 +0000 (04:49 +0900)
committerAiwota Programmer <aiwotaprog@tetteke.tk>
Thu, 21 Sep 2006 19:49:12 +0000 (04:49 +0900)
src/FukuiNoNamari/misc.py

index 25e5b62..7477455 100644 (file)
 
 import os.path
 import re
-import time
 import threading
+from datetime import tzinfo, timedelta, datetime
 
 import config
 from BbsType import bbs_type_exception
 
-REG_EXPR_HTTPDATE = re.compile("((?:Mon)|(?:Tue)|(?:Wed)|(?:Thu)|(?:Fri)|(?:Sat)|(?:Sun)), (\d{2}) ((?:Jan)|(?:Feb)|(?:Mar)|(?:Apr)|(?:May)|(?:Jun)|(?:Jul)|(?:Aug)|(?:Sep)|(?:Oct)|(?:Nov)|(?:Dec)) (\d{4}) (\d{2}):(\d{2}):(\d{2}) GMT")
-WDAY_DICT = {"Mon":0, "Tue":1, "Wed":2, "Thu":3, "Fri":4, "Sat":5, "Sun":6}
+REG_EXPR_HTTPDATE = re.compile("[^ ,]{3}, (?P<day>\d{2}) (?P<month>(?:Jan)|(?:Feb)|(?:Mar)|(?:Apr)|(?:May)|(?:Jun)|(?:Jul)|(?:Aug)|(?:Sep)|(?:Oct)|(?:Nov)|(?:Dec)) (?P<year>\d{4}) (?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2}) GMT")
 MON_DICT = {"Jan":1, "Feb":2, "Mar":3, "Apr":4, "May":5, "Jun":6, "Jul":7,
             "Aug":8, "Sep":9, "Oct":10, "Nov":11, "Dec":12}
 
@@ -102,25 +101,42 @@ def get_board_cache_path(bbs_type):
 
     return os.path.join(get_thread_idx_dir_path(bbs_type), ".cache")
 
+ZERO = timedelta(0)
+HOUR = timedelta(hours=1)
+class UTC(tzinfo):
+    """UTC"""
+
+    def utcoffset(self, dt):
+        return ZERO
+
+    def tzname(self, dt):
+        return "UTC"
+
+    def dst(self, dt):
+        return ZERO
+
+utc = UTC()
+
+epoch = datetime(1970, 1, 1, 0, 0, 0, 0, utc)
+
 def httpdate_to_secs(httpdate):
     """Returns the seconds since the epoch"""
 
     if not httpdate:
-        raise ValueError
+        return 0
 
     m = REG_EXPR_HTTPDATE.match(httpdate)
     if m:
-        tm_wday = WDAY_DICT[m.group(1)]
-        tm_mday = int(m.group(2))
-        tm_mon = MON_DICT[m.group(3)]
-        tm_year = int(m.group(4))
-        tm_hour = int(m.group(5))
-        tm_min = int(m.group(6))
-        tm_sec = int(m.group(7))
-
-        return int(time.mktime(
-            (tm_year,tm_mon,tm_mday,tm_hour,tm_min,tm_sec,tm_wday,0,-1)) \
-            - time.timezone)
+        tm_day = int(m.group("day"))
+        tm_mon = MON_DICT[m.group("month")]
+        tm_year = int(m.group("year"))
+        tm_hour = int(m.group("hour"))
+        tm_min = int(m.group("minute"))
+        tm_sec = int(m.group("second"))
+        
+        d = datetime(tm_year, tm_mon, tm_day, tm_hour, tm_min, tm_sec, 0, utc)
+        delta = d - epoch
+        return delta.days*24*60*60 + delta.seconds
     else:
         raise ValueError