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

Skip to content

Commit affbd87

Browse files
committed
Issue #5262: Fixed bug in next roll over time computation in TimedRotatingFileHandler.
1 parent e653a7d commit affbd87

2 files changed

Lines changed: 22 additions & 16 deletions

File tree

Lib/logging/handlers.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None,
172172
#
173173
# Case of the 'when' specifier is not important; lower or upper case
174174
# will work.
175-
currentTime = int(time.time())
176175
if self.when == 'S':
177176
self.interval = 1 # one second
178177
self.suffix = "%Y-%m-%d_%H-%M-%S"
@@ -203,8 +202,13 @@ def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None,
203202

204203
self.extMatch = re.compile(self.extMatch, re.ASCII)
205204
self.interval = self.interval * interval # multiply by units requested
206-
self.rolloverAt = currentTime + self.interval
205+
self.rolloverAt = self.computeRollover(int(time.time()))
207206

207+
def computeRollover(self, currentTime):
208+
"""
209+
Work out the rollover time based on the specified time.
210+
"""
211+
result = currentTime + self.interval
208212
# If we are rolling over at midnight or weekly, then the interval is already known.
209213
# What we need to figure out is WHEN the next interval is. In other words,
210214
# if you are rolling over at midnight, then your base interval is 1 day,
@@ -214,7 +218,7 @@ def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None,
214218
# the rest. Note that this code doesn't care about leap seconds. :)
215219
if self.when == 'MIDNIGHT' or self.when.startswith('W'):
216220
# This could be done with less code, but I wanted it to be clear
217-
if utc:
221+
if self.utc:
218222
t = time.gmtime(currentTime)
219223
else:
220224
t = time.localtime(currentTime)
@@ -224,7 +228,7 @@ def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None,
224228
# r is the number of seconds left between now and midnight
225229
r = _MIDNIGHT - ((currentHour * 60 + currentMinute) * 60 +
226230
currentSecond)
227-
self.rolloverAt = currentTime + r
231+
result = currentTime + r
228232
# If we are rolling over on a certain day, add in the number of days until
229233
# the next rollover, but offset by 1 since we just calculated the time
230234
# until the next day starts. There are three cases:
@@ -240,25 +244,24 @@ def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None,
240244
# The calculations described in 2) and 3) above need to have a day added.
241245
# This is because the above time calculation takes us to midnight on this
242246
# day, i.e. the start of the next day.
243-
if when.startswith('W'):
247+
if self.when.startswith('W'):
244248
day = t[6] # 0 is Monday
245249
if day != self.dayOfWeek:
246250
if day < self.dayOfWeek:
247251
daysToWait = self.dayOfWeek - day
248252
else:
249253
daysToWait = 6 - day + self.dayOfWeek + 1
250-
newRolloverAt = self.rolloverAt + (daysToWait * (60 * 60 * 24))
251-
if not utc:
254+
newRolloverAt = result + (daysToWait * (60 * 60 * 24))
255+
if not self.utc:
252256
dstNow = t[-1]
253257
dstAtRollover = time.localtime(newRolloverAt)[-1]
254258
if dstNow != dstAtRollover:
255259
if not dstNow: # DST kicks in before next rollover, so we need to deduct an hour
256260
newRolloverAt = newRolloverAt - 3600
257261
else: # DST bows out before next rollover, so we need to add an hour
258262
newRolloverAt = newRolloverAt + 3600
259-
self.rolloverAt = newRolloverAt
260-
261-
#print "Will rollover at %d, %d seconds from now" % (self.rolloverAt, self.rolloverAt - currentTime)
263+
result = newRolloverAt
264+
return result
262265

263266
def shouldRollover(self, record):
264267
"""
@@ -327,8 +330,8 @@ def doRollover(self):
327330
#print "%s -> %s" % (self.baseFilename, dfn)
328331
self.mode = 'w'
329332
self.stream = self._open()
330-
newRolloverAt = self.rolloverAt + self.interval
331333
currentTime = int(time.time())
334+
newRolloverAt = self.computeRollover(currentTime)
332335
while newRolloverAt <= currentTime:
333336
newRolloverAt = newRolloverAt + self.interval
334337
#If DST changes and midnight or weekly rollover, adjust for this.

Misc/NEWS

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Core and Builtins
2424
Library
2525
-------
2626

27+
- Issue #5262: Fixed bug in next rollover time computation in
28+
TimedRotatingFileHandler.
29+
2730
- Issue #6217: The C implementation of io.TextIOWrapper didn't include the
2831
errors property. Additionally, the errors and encoding properties of StringIO
2932
are always None now.
@@ -347,7 +350,7 @@ Library
347350
- Issue #5150: IDLE's format menu now has an option to strip trailing
348351
whitespace.
349352

350-
- Issue #5940: distutils.command.build_clib.check_library_list was not doing
353+
- Issue #5940: distutils.command.build_clib.check_library_list was not doing
351354
the right type checkings anymore.
352355

353356
- Issue #4875: On win32, ctypes.util.find_library does no longer
@@ -783,15 +786,15 @@ Library
783786

784787
- Issue #6048: Now Distutils uses the tarfile module in archive_util.
785788

786-
- Issue #6062: In distutils, fixed the package option of build_ext. Feedback
789+
- Issue #6062: In distutils, fixed the package option of build_ext. Feedback
787790
and tests on pywin32 by Tim Golden.
788791

789792
- Issue #6053: Fixed distutils tests on win32. patch by Hirokazu Yamamoto.
790793

791794
- Issue #6046: Fixed the library extension when distutils build_ext is used
792795
inplace. Initial patch by Roumen Petrov.
793796

794-
- Issue #6041: Now distutils `sdist` and `register` commands use `check` as a
797+
- Issue #6041: Now distutils `sdist` and `register` commands use `check` as a
795798
subcommand.
796799

797800
- Issue #6022: a test file was created in the current working directory by
@@ -812,7 +815,7 @@ Library
812815

813816
- Issue #2245: aifc now skips chunk types it doesn't recognize, per spec.
814817

815-
- Issue #5874: distutils.tests.test_config_cmd is not locale-sensitive
818+
- Issue #5874: distutils.tests.test_config_cmd is not locale-sensitive
816819
anymore.
817820

818821
- Issue #5810: Fixed Distutils test_build_scripts so it uses
@@ -1221,7 +1224,7 @@ Build
12211224
linker, rather than always exit successfully. Patch by Floris Bruynooghe.
12221225

12231226
- Issue #4587: Add configure option --with-dbmliborder=db1:db2:... to specify
1224-
the order that backends for the dbm extension are checked.
1227+
the order that backends for the dbm extension are checked.
12251228

12261229
- Link the shared python library with $(MODLIBS).
12271230

0 commit comments

Comments
 (0)