def timegm(year, month, day, hour, minute, second):
"""
Convert time tuple in GMT to seconds since epoch, GMT
"""
EPOCH = 1970
if year < EPOCH:
raise ValueError("Years prior to %d not supported" % (EPOCH,))
assert 1 <= month <= 12
days = 365*(year-EPOCH) + calendar.leapdays(EPOCH, year)
for i in range(1, month):
days = days + calendar.mdays[i]
if month > 2 and calendar.isleap(year):
days = days + 1
days = days + day - 1
hours = days*24 + hour
minutes = hours*60 + minute
seconds = minutes*60 + second
return seconds
python类leapdays()的实例源码
def timegm(year, month, day, hour, minute, second):
"""Convert time tuple in GMT to seconds since epoch, GMT"""
EPOCH = 1970
assert year >= EPOCH
assert 1 <= month <= 12
days = 365*(year-EPOCH) + calendar.leapdays(EPOCH, year)
for i in range(1, month):
days = days + calendar.mdays[i]
if month > 2 and calendar.isleap(year):
days = days + 1
days = days + day - 1
hours = days*24 + hour
minutes = hours*60 + minute
seconds = minutes*60 + second
return seconds
def test_no_range(self):
# test when no range i.e. two identical years as args
self.assertEqual(calendar.leapdays(2010,2010), 0)
def test_no_leapdays(self):
# test when no leap years in range
self.assertEqual(calendar.leapdays(2010,2011), 0)
def test_no_leapdays_upper_boundary(self):
# test no leap years in range, when upper boundary is a leap year
self.assertEqual(calendar.leapdays(2010,2012), 0)
def test_one_leapday_lower_boundary(self):
# test when one leap year in range, lower boundary is leap year
self.assertEqual(calendar.leapdays(2012,2013), 1)
def test_several_leapyears_in_range(self):
self.assertEqual(calendar.leapdays(1997,2020), 5)
def test_no_range(self):
# test when no range i.e. two identical years as args
self.assertEqual(calendar.leapdays(2010,2010), 0)
def test_no_leapdays(self):
# test when no leap years in range
self.assertEqual(calendar.leapdays(2010,2011), 0)
def test_no_leapdays_upper_boundary(self):
# test no leap years in range, when upper boundary is a leap year
self.assertEqual(calendar.leapdays(2010,2012), 0)
def test_one_leapday_lower_boundary(self):
# test when one leap year in range, lower boundary is leap year
self.assertEqual(calendar.leapdays(2012,2013), 1)
def test_several_leapyears_in_range(self):
self.assertEqual(calendar.leapdays(1997,2020), 5)
def test_no_range(self):
# test when no range i.e. two identical years as args
self.assertEqual(calendar.leapdays(2010,2010), 0)
def test_no_leapdays(self):
# test when no leap years in range
self.assertEqual(calendar.leapdays(2010,2011), 0)
def test_no_leapdays_upper_boundary(self):
# test no leap years in range, when upper boundary is a leap year
self.assertEqual(calendar.leapdays(2010,2012), 0)
def test_one_leapday_lower_boundary(self):
# test when one leap year in range, lower boundary is leap year
self.assertEqual(calendar.leapdays(2012,2013), 1)
def test_several_leapyears_in_range(self):
self.assertEqual(calendar.leapdays(1997,2020), 5)
def timegm(year, month, day, hour, minute, second):
"""Convert time tuple in GMT to seconds since epoch, GMT"""
EPOCH = 1970
assert year >= EPOCH
assert 1 <= month <= 12
days = 365*(year-EPOCH) + calendar.leapdays(EPOCH, year)
for i in range(1, month):
days = days + calendar.mdays[i]
if month > 2 and calendar.isleap(year):
days = days + 1
days = days + day - 1
hours = days*24 + hour
minutes = hours*60 + minute
seconds = minutes*60 + second
return seconds
def test_no_range(self):
# test when no range i.e. two identical years as args
self.assertEqual(calendar.leapdays(2010,2010), 0)
def test_no_leapdays(self):
# test when no leap years in range
self.assertEqual(calendar.leapdays(2010,2011), 0)
def test_no_leapdays_upper_boundary(self):
# test no leap years in range, when upper boundary is a leap year
self.assertEqual(calendar.leapdays(2010,2012), 0)
def test_one_leapday_lower_boundary(self):
# test when one leap year in range, lower boundary is leap year
self.assertEqual(calendar.leapdays(2012,2013), 1)
def test_several_leapyears_in_range(self):
self.assertEqual(calendar.leapdays(1997,2020), 5)
def test_no_range(self):
# test when no range i.e. two identical years as args
self.assertEqual(calendar.leapdays(2010,2010), 0)
def test_no_leapdays(self):
# test when no leap years in range
self.assertEqual(calendar.leapdays(2010,2011), 0)
def test_no_leapdays_upper_boundary(self):
# test no leap years in range, when upper boundary is a leap year
self.assertEqual(calendar.leapdays(2010,2012), 0)
def test_one_leapday_lower_boundary(self):
# test when one leap year in range, lower boundary is leap year
self.assertEqual(calendar.leapdays(2012,2013), 1)
def test_several_leapyears_in_range(self):
self.assertEqual(calendar.leapdays(1997,2020), 5)
def test_no_range(self):
# test when no range i.e. two identical years as args
self.assertEqual(calendar.leapdays(2010,2010), 0)
def test_no_leapdays(self):
# test when no leap years in range
self.assertEqual(calendar.leapdays(2010,2011), 0)