Pages

2012-08-26

Using PyRSS2Gen in time zones other than GMT

PyRSS2Gen

--- PyRSS2Gen.py.orig 2012-08-25 10:22:14.292968887 +0000
+++ PyRSS2Gen.py 2012-08-25 13:38:58.838836110 +0000
@@ -57,12 +57,13 @@
     # Isn't there a standard way to do this for Python?  The
     # rfc822 and email.Utils modules assume a timestamp.  The
     # following is based on the rfc822 module.
-    return "%s, %02d %s %04d %02d:%02d:%02d GMT" % (
+    return ("%s, %02d %s %04d %02d:%02d:%02d %s" % (
             ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][dt.weekday()],
             dt.day,
             ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
              "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][dt.month-1],
-            dt.year, dt.hour, dt.minute, dt.second)
+            dt.year, dt.hour, dt.minute, dt.second,
+            dt.strftime('%z'))).strip()
 
         
 ##

Update, August 28, 2:07 p.m. JST: Output timezone in any case ('GMT' by default).
--- PyRSS2Gen.py.orig 2012-08-25 10:22:14.292968887 +0000
+++ PyRSS2Gen.py 2012-08-28 04:31:25.535324750 +0000
@@ -57,12 +57,15 @@
     # Isn't there a standard way to do this for Python?  The
     # rfc822 and email.Utils modules assume a timestamp.  The
     # following is based on the rfc822 module.
-    return "%s, %02d %s %04d %02d:%02d:%02d GMT" % (
+    tz = 'GMT' # default timezone
+    if dt.tzinfo:
+        tz = dt.strftime('%z')
+    return "%s, %02d %s %04d %02d:%02d:%02d %s" % (
             ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][dt.weekday()],
             dt.day,
             ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
              "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][dt.month-1],
-            dt.year, dt.hour, dt.minute, dt.second)
+            dt.year, dt.hour, dt.minute, dt.second, tz)
 
         
 ##

Update, September 1, 5:06 p.m. JST: Simplified the patch.
--- PyRSS2Gen.py.orig 2012-08-25 10:22:14.292968887 +0000
+++ PyRSS2Gen.py 2012-09-01 07:16:36.291449545 +0000
@@ -57,12 +57,13 @@
     # Isn't there a standard way to do this for Python?  The
     # rfc822 and email.Utils modules assume a timestamp.  The
     # following is based on the rfc822 module.
-    return "%s, %02d %s %04d %02d:%02d:%02d GMT" % (
+    return "%s, %02d %s %04d %02d:%02d:%02d %s" % (
             ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][dt.weekday()],
             dt.day,
             ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
              "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][dt.month-1],
-            dt.year, dt.hour, dt.minute, dt.second)
+            dt.year, dt.hour, dt.minute, dt.second,
+            dt.strftime('%z') if dt.tzinfo else 'GMT') # default timezone is 'GMT'
 
         
 ##

No comments: