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

Skip to content

Commit b39aff8

Browse files
committed
Add unrelated but handy function: timegm(), to calculate Unix
timestamp from GMT tuple.
1 parent 145a5f7 commit b39aff8

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

Lib/calendar.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,20 @@ def prcal(year):
151151
prweek(cal[i], 2)
152152
print _spacing,
153153
print
154+
155+
# Unrelated but handy function to calculate Unix timestamp from GMT
156+
EPOCH = 1970
157+
def timegm(tuple):
158+
year, month, day, hour, minute, second = tuple[:6]
159+
assert year >= EPOCH
160+
assert 1 <= month <= 12
161+
days = 365*(year-EPOCH) + leapdays(EPOCH, year)
162+
for i in range(1, month):
163+
days = days + mdays[i]
164+
if month > 2 and isleap(year):
165+
days = days + 1
166+
days = days + day - 1
167+
hours = days*24 + hour
168+
minutes = hours*60 + minute
169+
seconds = minutes*60 + second
170+
return seconds

0 commit comments

Comments
 (0)