[Tutor] time question
Lloyd Hugh Allen
lha2@columbia.edu
Sat, 25 Aug 2001 10:40:05 -0400
Here's something strange:
----code follows----
>>> quitTime = (2001, 8, 22, 9, 45, -1, -1, -1, -1)
>>> import time
>>> nowTime = time.localtime(time.time())
>>> nowTime
(2001, 8, 25, 10, 0, 54, 5, 237, 1)
>>> theDiff = []
>>> for foo in range(len(nowTime)):
theDiff.append(nowTime[foo]-quitTime[foo])
>>> theDiff
[0, 0, 3, 1, -45, 55, 6, 238, 2]
>>> theDiff = tuple(theDiff)
>>> theDiff
(0, 0, 3, 1, -45, 55, 6, 238, 2)
>>> time.localtime(time.mktime(theDiff))
(1999, 12, 2, 23, 15, 55, 3, 336, 0)
----end code-------
I had been hoping that time.mktime and time.localtime would
intelligently handle the -45 minutes. Seems not to be the case. I trust
that with well-formed times they are in fact inverse functions?
Here's my go at a real solution:
-----code starts----
>>> def convertTime(theDiff):
"""Returns a time tuple with strictly positive elements.
Note that this function does not intelligently handle months,
but assumes that all months are 28 days long."""
offSet = [0, 12, 28, 24, 60]
theDiff = list(theDiff)
for foo in range(len(theDiff)-1, 0, -1):
if theDiff[foo]<0:
theDiff[foo] = theDiff[foo] + offSet[foo]
theDiff[foo-1] = theDiff[foo-1] - 1
theDiff = tuple(theDiff)
return theDiff
----end code--------
-LHA