[Python-checkins] python/nondist/sandbox/datetime datetime.py,1.94,1.95 test_both.py,1.65,1.66

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Wed, 11 Dec 2002 19:24:57 -0800


Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory sc8-pr-cvs1:/tmp/cvs-serv15091

Modified Files:
	datetime.py test_both.py 
Log Message:
Test that a utcoffset out of bounds is caught when it gets formatted.
Change the Python implementation to catch it (the C implementation already
did).


Index: datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.py,v
retrieving revision 1.94
retrieving revision 1.95
diff -C2 -d -r1.94 -r1.95
*** datetime.py	12 Dec 2002 02:58:42 -0000	1.94
--- datetime.py	12 Dec 2002 03:24:55 -0000	1.95
***************
*** 970,973 ****
--- 970,975 ----
                      sign = "+"
                  hh, mm = divmod(off, 60)
+                 if hh >= 24:
+                     raise ValueError("utcoffset must be in -1439 .. 1439")
                  return "%s%02d%s%02d" % (sign, hh, sep, mm)
  

Index: test_both.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_both.py,v
retrieving revision 1.65
retrieving revision 1.66
diff -C2 -d -r1.65 -r1.66
*** test_both.py	12 Dec 2002 02:58:43 -0000	1.65
--- test_both.py	12 Dec 2002 03:24:55 -0000	1.66
***************
*** 1549,1552 ****
--- 1549,1571 ----
  #        self.assertEqual(t3.strftime("%H:%M:%S %Z %z"), "13:47:00 MET +0100")
  
+     def test_utc_offset_out_of_bounds(self):
+         class Edgy(tzinfo):
+             def __init__(self, offset):
+                 self.offset = offset
+             def utcoffset(self, dt):
+                 return self.offset
+ 
+         for offset, legit in ((-1440, False),
+                               (-1439, True),
+                               (1439, True),
+                               (1440, False)):
+             t = timetz(1, 2, 3, tzinfo=Edgy(offset))
+             if legit:
+                 aofs = abs(offset)
+                 h, m = divmod(aofs, 60)
+                 tag = "%c%02d:%02d" % (offset < 0 and '-' or '+', h, m)
+                 self.assertEqual(str(t), "01:02:03" + tag)
+             else:
+                 self.assertRaises(ValueError, str, t)
  
  def test_suite():