[Python-checkins] r81988 - in python/branches/py3k/Lib: calendar.py test/test_calendar.py

alexander.belopolsky python-checkins at python.org
Mon Jun 14 19:32:04 CEST 2010


Author: alexander.belopolsky
Date: Mon Jun 14 19:32:03 2010
New Revision: 81988

Log:
Issue 6280: Tests and simpler implementation for calendar.timegm

Modified:
   python/branches/py3k/Lib/calendar.py
   python/branches/py3k/Lib/test/test_calendar.py

Modified: python/branches/py3k/Lib/calendar.py
==============================================================================
--- python/branches/py3k/Lib/calendar.py	(original)
+++ python/branches/py3k/Lib/calendar.py	Mon Jun 14 19:32:03 2010
@@ -587,17 +587,12 @@
 
 
 EPOCH = 1970
-_EPOCH_ORD = datetime.date(EPOCH, 1, 1).toordinal()
-
+_EPOCH_DATETIME = datetime.datetime(EPOCH, 1, 1)
+_SECOND = datetime.timedelta(seconds=1)
 
 def timegm(tuple):
     """Unrelated but handy function to calculate Unix timestamp from GMT."""
-    year, month, day, hour, minute, second = tuple[:6]
-    days = datetime.date(year, month, 1).toordinal() - _EPOCH_ORD + day - 1
-    hours = days*24 + hour
-    minutes = hours*60 + minute
-    seconds = minutes*60 + second
-    return seconds
+    return (datetime.datetime(*tuple[:6]) - _EPOCH_DATETIME) // _SECOND
 
 
 def main(args):

Modified: python/branches/py3k/Lib/test/test_calendar.py
==============================================================================
--- python/branches/py3k/Lib/test/test_calendar.py	(original)
+++ python/branches/py3k/Lib/test/test_calendar.py	Mon Jun 14 19:32:03 2010
@@ -2,7 +2,7 @@
 import unittest
 
 from test import support
-
+import time
 
 result_2004_text = """
                                   2004
@@ -381,13 +381,21 @@
         # A 31-day december starting on friday (2+7+7+7+7+1 days)
         self.check_weeks(1995, 12, (2, 7, 7, 7, 7, 1))
 
+class TimegmTestCase(unittest.TestCase):
+    TIMESTAMPS = [0, 10, 100, 1000, 10000, 100000, 1000000,
+                  1234567890, 1262304000, 1275785153,]
+    def test_timegm(self):
+        for secs in self.TIMESTAMPS:
+            tuple = time.gmtime(secs)
+            self.assertEqual(secs, calendar.timegm(tuple))
 
 def test_main():
     support.run_unittest(
         OutputTestCase,
         CalendarTestCase,
         MondayTestCase,
-        SundayTestCase
+        SundayTestCase,
+        TimegmTestCase,
     )
 
 


More information about the Python-checkins mailing list