Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 43ca710

Browse files
committed
Issue 6280: Tests and simpler implementation for calendar.timegm
1 parent 4e749a1 commit 43ca710

2 files changed

Lines changed: 13 additions & 10 deletions

File tree

Lib/calendar.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -587,17 +587,12 @@ def formatstring(cols, colwidth=_colwidth, spacing=_spacing):
587587

588588

589589
EPOCH = 1970
590-
_EPOCH_ORD = datetime.date(EPOCH, 1, 1).toordinal()
591-
590+
_EPOCH_DATETIME = datetime.datetime(EPOCH, 1, 1)
591+
_SECOND = datetime.timedelta(seconds=1)
592592

593593
def timegm(tuple):
594594
"""Unrelated but handy function to calculate Unix timestamp from GMT."""
595-
year, month, day, hour, minute, second = tuple[:6]
596-
days = datetime.date(year, month, 1).toordinal() - _EPOCH_ORD + day - 1
597-
hours = days*24 + hour
598-
minutes = hours*60 + minute
599-
seconds = minutes*60 + second
600-
return seconds
595+
return (datetime.datetime(*tuple[:6]) - _EPOCH_DATETIME) // _SECOND
601596

602597

603598
def main(args):

Lib/test/test_calendar.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import unittest
33

44
from test import support
5-
5+
import time
66

77
result_2004_text = """
88
2004
@@ -381,13 +381,21 @@ def test_december(self):
381381
# A 31-day december starting on friday (2+7+7+7+7+1 days)
382382
self.check_weeks(1995, 12, (2, 7, 7, 7, 7, 1))
383383

384+
class TimegmTestCase(unittest.TestCase):
385+
TIMESTAMPS = [0, 10, 100, 1000, 10000, 100000, 1000000,
386+
1234567890, 1262304000, 1275785153,]
387+
def test_timegm(self):
388+
for secs in self.TIMESTAMPS:
389+
tuple = time.gmtime(secs)
390+
self.assertEqual(secs, calendar.timegm(tuple))
384391

385392
def test_main():
386393
support.run_unittest(
387394
OutputTestCase,
388395
CalendarTestCase,
389396
MondayTestCase,
390-
SundayTestCase
397+
SundayTestCase,
398+
TimegmTestCase,
391399
)
392400

393401

0 commit comments

Comments
 (0)