Closes #14267: Corrected computation of rollover filename.

This commit is contained in:
Vinay Sajip 2012-03-13 12:06:35 +00:00
parent b968b36ad0
commit 9790e083a5
1 changed files with 15 additions and 6 deletions

View File

@ -272,9 +272,10 @@ class TimedRotatingFileHandler(BaseRotatingHandler):
dstAtRollover = time.localtime(newRolloverAt)[-1] dstAtRollover = time.localtime(newRolloverAt)[-1]
if dstNow != dstAtRollover: if dstNow != dstAtRollover:
if not dstNow: # DST kicks in before next rollover, so we need to deduct an hour if not dstNow: # DST kicks in before next rollover, so we need to deduct an hour
newRolloverAt = newRolloverAt - 3600 addend = -3600
else: # DST bows out before next rollover, so we need to add an hour else: # DST bows out before next rollover, so we need to add an hour
newRolloverAt = newRolloverAt + 3600 addend = 3600
newRolloverAt += addend
result = newRolloverAt result = newRolloverAt
return result return result
@ -326,11 +327,20 @@ class TimedRotatingFileHandler(BaseRotatingHandler):
self.stream.close() self.stream.close()
self.stream = None self.stream = None
# get the time that this sequence started at and make it a TimeTuple # get the time that this sequence started at and make it a TimeTuple
currentTime = int(time.time())
dstNow = time.localtime(currentTime)[-1]
t = self.rolloverAt - self.interval t = self.rolloverAt - self.interval
if self.utc: if self.utc:
timeTuple = time.gmtime(t) timeTuple = time.gmtime(t)
else: else:
timeTuple = time.localtime(t) timeTuple = time.localtime(t)
dstThen = timeTuple[-1]
if dstNow != dstThen:
if dstNow:
addend = 3600
else:
addend = -3600
timeTuple = time.localtime(t + addend)
dfn = self.baseFilename + "." + time.strftime(self.suffix, timeTuple) dfn = self.baseFilename + "." + time.strftime(self.suffix, timeTuple)
if os.path.exists(dfn): if os.path.exists(dfn):
os.remove(dfn) os.remove(dfn)
@ -346,19 +356,18 @@ class TimedRotatingFileHandler(BaseRotatingHandler):
#print "%s -> %s" % (self.baseFilename, dfn) #print "%s -> %s" % (self.baseFilename, dfn)
self.mode = 'w' self.mode = 'w'
self.stream = self._open() self.stream = self._open()
currentTime = int(time.time())
newRolloverAt = self.computeRollover(currentTime) newRolloverAt = self.computeRollover(currentTime)
while newRolloverAt <= currentTime: while newRolloverAt <= currentTime:
newRolloverAt = newRolloverAt + self.interval newRolloverAt = newRolloverAt + self.interval
#If DST changes and midnight or weekly rollover, adjust for this. #If DST changes and midnight or weekly rollover, adjust for this.
if (self.when == 'MIDNIGHT' or self.when.startswith('W')) and not self.utc: if (self.when == 'MIDNIGHT' or self.when.startswith('W')) and not self.utc:
dstNow = time.localtime(currentTime)[-1]
dstAtRollover = time.localtime(newRolloverAt)[-1] dstAtRollover = time.localtime(newRolloverAt)[-1]
if dstNow != dstAtRollover: if dstNow != dstAtRollover:
if not dstNow: # DST kicks in before next rollover, so we need to deduct an hour if not dstNow: # DST kicks in before next rollover, so we need to deduct an hour
newRolloverAt = newRolloverAt - 3600 addend = -3600
else: # DST bows out before next rollover, so we need to add an hour else: # DST bows out before next rollover, so we need to add an hour
newRolloverAt = newRolloverAt + 3600 addend = 3600
newRolloverAt += addend
self.rolloverAt = newRolloverAt self.rolloverAt = newRolloverAt
class WatchedFileHandler(logging.FileHandler): class WatchedFileHandler(logging.FileHandler):