Commit fd7a53d7 authored by Lennart Regebro's avatar Lennart Regebro

Remade rfc822() to utilize the _tzoffset method. Much cleaner and better code.

Remade the test code for rfc822() to provide more stable tests.
parent 4ae9e3d7
......@@ -12,7 +12,7 @@
##############################################################################
"""Encapsulation of date/time values"""
__version__='$Revision: 1.81 $'[11:-2]
__version__='$Revision: 1.82 $'[11:-2]
import re,sys, os, math, DateTimeZone
......@@ -362,6 +362,10 @@ def _calendarday(j):
return int(yr),int(mo),int(dy)
def _tzoffset(tz, t):
"""Returns the offset in seconds to GMT from a specific timezone (tz) at
a specific time (t). NB! The _tzoffset result is the same same sign as
the time zone, i.e. GMT+2 has a 7200 second offset. This is the opposite
sign of time.timezone which (confusingly) is -7200 for GMT+2."""
try:
return DateTime._tzinfo[tz].info(t)[0]
except:
......@@ -400,7 +404,10 @@ def safelocaltime(t):
return rval
def _tzoffset2rfc822zone(seconds):
return "%+03d%02d" % divmod( (-seconds/60), 60)
"""Takes an offset, such as from _tzoffset(), and returns an rfc822
compliant zone specification. Please note that the result of
_tzoffset() is the negative of what time.localzone and time.altzone is."""
return "%+03d%02d" % divmod( (seconds/60), 60)
class DateTime:
......@@ -1437,18 +1444,12 @@ class DateTime:
def rfc822(self):
"""Return the date in RFC 822 format"""
if self._tz == self._localzone0: #Use local standard time
tzoffset = _tzoffset2rfc822zone(timezone)
elif self._tz == self._localzone1: # Use local daylight saving time
tzoffset = _tzoffset2rfc822zone(altzone)
else:
tzoffset = '-0000' # unknown time zone offset
tzoffset = _tzoffset2rfc822zone(_tzoffset(self._tz, self._t))
return '%s, %2.2d %s %d %2.2d:%2.2d:%2.2d %s' % (
self._aday,self._day,self._amon,self._year,
self._hour,self._minute,self._nearsec,tzoffset)
# New formats
def fCommon(self):
"""Return a string representing the object\'s value
......
......@@ -249,22 +249,31 @@ class DateTimeTests(unittest.TestCase):
def testRFC822(self):
'''rfc822 conversion'''
isDST = time.localtime(time.time())[8]
if isDST:
offset = time.altzone
else:
offset = time.timezone
rfc822zone = "%+03d%02d" % divmod((-offset/60), 60)
wrongzone = "%+03d:%02d" % divmod((60-offset/60), 60) #one hour off, ISO format
# Create a local DateTime and test
dt = DateTime(2002, 5, 2, 8, 0, 0)
self.assertEqual(dt.rfc822(), 'Thu, 02 May 2002 08:00:00' + ' ' + rfc822zone)
# Create a non-local date time and test
dt = DateTime('2002-05-02T08:00:00Z'+wrongzone)
self.assertEqual(dt.rfc822(), 'Thu, 02 May 2002 08:00:00 -0000')
dt = DateTime('2002-05-02T08:00:00Z+00:00')
self.assertEqual(dt.rfc822(), 'Thu, 02 May 2002 08:00:00 +0000')
dt = DateTime('2002-05-02T08:00:00Z+02:00')
self.assertEqual(dt.rfc822(), 'Thu, 02 May 2002 08:00:00 +0200')
dt = DateTime('2002-05-02T08:00:00Z-02:00')
self.assertEqual(dt.rfc822(), 'Thu, 02 May 2002 08:00:00 -0200')
# Checking that conversion from local time is working.
dt = DateTime()
dts = dt.rfc822().split(' ')
times = dts[4].split(':')
_isDST = time.localtime(time.time())[8]
if _isDST: offset = time.altzone
else: offset = time.timezone
self.assertEqual(dts[0], dt.aDay() + ',')
self.assertEqual(int(dts[1]), dt.day())
self.assertEqual(dts[2], dt.aMonth())
self.assertEqual(int(dts[3]), dt.year())
self.assertEqual(int(times[0]), dt.h_24())
self.assertEqual(int(times[1]), dt.minute())
self.assertEqual(int(times[2]), int(dt.second()))
self.assertEqual(dts[5], "%+03d%02d" % divmod( (-offset/60), 60) )
def test_suite():
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment