]> granicus.if.org Git - python/commitdiff
Issue 6280: Tests and simpler implementation for calendar.timegm
authorAlexander Belopolsky <alexander.belopolsky@gmail.com>
Mon, 14 Jun 2010 17:32:03 +0000 (17:32 +0000)
committerAlexander Belopolsky <alexander.belopolsky@gmail.com>
Mon, 14 Jun 2010 17:32:03 +0000 (17:32 +0000)
Lib/calendar.py
Lib/test/test_calendar.py

index 0c1fdad56749d95a3f463f6a9b282082dcb03eba..a82b21a70aa7ffb6e12a8ef642255f85389ff714 100644 (file)
@@ -587,17 +587,12 @@ def formatstring(cols, colwidth=_colwidth, spacing=_spacing):
 
 
 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):
index e8b2ca5482c280ad62f3ffd8f80bb0e0f59cdbad..b7ce4750e0ed2e536527fdf791dd4839f58bfaee 100644 (file)
@@ -2,7 +2,7 @@ import calendar
 import unittest
 
 from test import support
-
+import time
 
 result_2004_text = """
                                   2004
@@ -381,13 +381,21 @@ class SundayTestCase(MonthCalendarTestCase):
         # 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,
     )