Skip to content

Commit 48359d2

Browse files
authored
Implement utc=False tests for TimedRotatingFileHandler
Added tests for utc=False in TimedRotatingFileHandler.
1 parent 9d38143 commit 48359d2

File tree

1 file changed

+66
-2
lines changed

1 file changed

+66
-2
lines changed

Lib/test/test_logging.py

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6658,7 +6658,6 @@ def test_invalid(self):
66586658
assertRaises(ValueError, logging.handlers.TimedRotatingFileHandler,
66596659
self.fn, 'W7', encoding="utf-8", delay=True)
66606660

6661-
# TODO: Test for utc=False.
66626661
def test_compute_rollover_daily_attime(self):
66636662
currentTime = 0
66646663
rh = logging.handlers.TimedRotatingFileHandler(
@@ -6698,7 +6697,41 @@ def test_compute_rollover_daily_attime(self):
66986697
finally:
66996698
rh.close()
67006699

6701-
# TODO: Test for utc=False.
6700+
# Test utc=False
6701+
rh = logging.handlers.TimedRotatingFileHandler(
6702+
self.fn, encoding="utf-8", when='MIDNIGHT',
6703+
utc=False, atTime=None)
6704+
try:
6705+
t = time.localtime(currentTime)
6706+
currentHour = t[3]
6707+
currentMinute = t[4]
6708+
currentSecond = t[5]
6709+
# Next rollover is at midnight, which is (24 - currentHour) hours from now
6710+
expected = currentTime + (24 - currentHour) * 3600 - currentMinute * 60 - currentSecond
6711+
actual = rh.computeRollover(currentTime)
6712+
self.assertEqual(actual, expected)
6713+
finally:
6714+
rh.close()
6715+
6716+
atTime = datetime.time(12, 0, 0)
6717+
rh = logging.handlers.TimedRotatingFileHandler(
6718+
self.fn, encoding="utf-8", when='MIDNIGHT',
6719+
utc=False, atTime=atTime)
6720+
try:
6721+
t = time.localtime(currentTime)
6722+
currentHour = t[3]
6723+
currentMinute = t[4]
6724+
currentSecond = t[5]
6725+
# Next rollover is at atTime today if current time < atTime, otherwise tomorrow
6726+
if currentHour < 12:
6727+
expected = currentTime + (12 - currentHour) * 3600 - currentMinute * 60 - currentSecond
6728+
else:
6729+
expected = currentTime + (36 - currentHour) * 3600 - currentMinute * 60 - currentSecond
6730+
actual = rh.computeRollover(currentTime)
6731+
self.assertEqual(actual, expected)
6732+
finally:
6733+
rh.close()
6734+
67026735
def test_compute_rollover_weekly_attime(self):
67036736
currentTime = int(time.time())
67046737
today = currentTime - currentTime % 86400
@@ -6753,6 +6786,37 @@ def test_compute_rollover_weekly_attime(self):
67536786
finally:
67546787
rh.close()
67556788

6789+
# Test utc=False
6790+
wday = time.localtime(today).tm_wday
6791+
for day in range(7):
6792+
rh = logging.handlers.TimedRotatingFileHandler(
6793+
self.fn, encoding="utf-8", when='W%d' % day, interval=1, backupCount=0,
6794+
utc=False, atTime=atTime)
6795+
try:
6796+
if wday > day:
6797+
expected = (7 - wday + day)
6798+
else:
6799+
expected = (day - wday)
6800+
expected *= 24 * 60 * 60
6801+
expected += 12 * 60 * 60
6802+
expected += today
6803+
6804+
actual = rh.computeRollover(today)
6805+
self.assertEqual(actual, expected)
6806+
6807+
actual = rh.computeRollover(today + 12 * 60 * 60 - 1)
6808+
self.assertEqual(actual, expected)
6809+
6810+
if day == wday:
6811+
expected += 7 * 24 * 60 * 60
6812+
actual = rh.computeRollover(today + 12 * 60 * 60)
6813+
self.assertEqual(actual, expected)
6814+
6815+
actual = rh.computeRollover(today + 13 * 60 * 60)
6816+
self.assertEqual(actual, expected)
6817+
finally:
6818+
rh.close()
6819+
67566820
def test_compute_files_to_delete(self):
67576821
# See bpo-46063 for background
67586822
wd = tempfile.mkdtemp(prefix='test_logging_')

0 commit comments

Comments
 (0)