Handle timestamps that don't fit in the platform's time_t.
authorAdam Sampson <ats@offog.org>
Sun, 12 Jul 2015 17:03:27 +0000 (18:03 +0100)
committerAdam Sampson <ats@offog.org>
Sun, 12 Jul 2015 17:03:27 +0000 (18:03 +0100)
On a 32-bit platform, time.localtime can only deal with 32-bit inputs.
This adds a more thorough set of tests for weird article dates, and
exercises DayWriter as well as article dates.

rawdoglib/rawdog.py
test-rawdog

index c75bd65513f5d9fddf1bea707cb126bb7e2df678..684a0f9554ee016b8872208399f5bac0aade2ccf 100644 (file)
@@ -81,7 +81,10 @@ def safe_ftime(format, t):
 
 def format_time(secs, config):
        """Format a time and date nicely."""
-       t = time.localtime(secs)
+       try:
+               t = time.localtime(secs)
+       except ValueError, e:
+               return u"(bad time %s; %s)" % (repr(secs), str(e))
        format = config["datetimeformat"]
        if format is None:
                format = config["timeformat"] + ", " + config["dayformat"]
@@ -775,7 +778,11 @@ class DayWriter:
                self.counter += 1
 
        def time(self, s):
-               tm = time.localtime(s)
+               try:
+                       tm = time.localtime(s)
+               except ValueError:
+                       # e.g. "timestamp out of range for platform time_t"
+                       return
                if tm[:3] != self.lasttime[:3] and self.config["daysections"]:
                        self.close(0)
                        self.start_day(tm)
index 7b3f77ec144430efcd39b961436a8a6bf89f2fb7..7d3a953e98cb57299178bf830ea1f35d46eb9d43 100755 (executable)
@@ -1298,10 +1298,15 @@ if [ -n "$ja_LC_ALL" ]; then
        contains $statedir/output.html "HEADING-Tuesday" "ITEMDATE-Tuesday"
 fi
 
-begin "two-digit year with timezone"
-# As of 2015-07, feedparser treats the time zone as the year number in the date
-# below -- seen in the real world in <http://zeptobars.ru/en/rss>.
-# But time.strftime doesn't know how to format dates in the year 300 AD!
+begin "strange dates in feeds"
+# Python's time.strftime can't handle all possible dates, and the range of
+# dates that Python can work with in time_t format varies between platforms.
+# rawdog won't be able to display dates that Python can't handle, but it
+# should at least not crash if feedparser decides to present them
+# (for example, if feedparser misparses a timezone as a feed).
+echo "__date__" >$statedir/item
+add "itemtemplate item"
+add "sortbyfeeddate true"
 cat >$httpdir/feed.rss <<EOF
 <rss version="2.0">
   <channel>
@@ -1309,8 +1314,23 @@ cat >$httpdir/feed.rss <<EOF
     <link>http://example.org/</link>
     <description>example-feed-description</description>
     <item>
-      <title>example-item-title</title>
+      <title>Date in 300 outside 32-bit time_t range</title>
+      <link>http://example.org/item</link>
+      <pubDate>Mon, 1 Jan 0300 01:23:45 +0000</pubDate>
+    </item>
+    <item>
+      <title>Date in 1750 using Julian calendar</title>
+      <link>http://example.org/item</link>
+      <pubDate>Mon, 1 Jan 1750 01:23:45 +0000</pubDate>
+    </item>
+    <item>
+      <title>Date in 1969 with negative time_t</title>
       <link>http://example.org/item</link>
+      <pubDate>Wed, 1 Jan 1969 01:23:45 +0000</pubDate>
+    </item>
+    <item>
+      <title>Date in 2015 that feedparser 5.2.0 misparses as 300</title>
+      <link>http://zeptobars.ru/en/rss</link>
       <pubDate>Fri, 20 Mar 15 17:32:14 +0300</pubDate>
     </item>
   </channel>
@@ -1319,18 +1339,6 @@ EOF
 add "feed 0 $httpurl/feed.rss"
 runs -uw
 
-begin "year before 1000"
-make_atom10 $httpdir/feed.atom
-add "feed 0 $httpurl/feed.atom"
-cat >$statedir/plugins/mangle.py <<EOF
-import rawdoglib.plugins
-def seen(rawdog, config, article, ignore):
-    # 25th July 385
-    article.date = -50000000000
-rawdoglib.plugins.attach_hook("article_seen", seen)
-EOF
-runs -uw
-
 begin "item authors"
 cat >$httpdir/feed.atom <<EOF
 <?xml version="1.0" encoding="utf-8"?>