MCPcopy Index your code
hub / github.com/python/cpython / computeRollover

Method computeRollover

Lib/logging/handlers.py:290–365  ·  view source on GitHub ↗

Work out the rollover time based on the specified time.

(self, currentTime)

Source from the content-addressed store, hash-verified

288 self.rolloverAt = self.computeRollover(t)
289
290 def computeRollover(self, currentTime):
291 """
292 Work out the rollover time based on the specified time.
293 """
294 result = currentTime + self.interval
295 # If we are rolling over at midnight or weekly, then the interval is already known.
296 # What we need to figure out is WHEN the next interval is. In other words,
297 # if you are rolling over at midnight, then your base interval is 1 day,
298 # but you want to start that one day clock at midnight, not now. So, we
299 # have to fudge the rolloverAt value in order to trigger the first rollover
300 # at the right time. After that, the regular interval will take care of
301 # the rest. Note that this code doesn't care about leap seconds. :)
302 if self.when == 'MIDNIGHT' or self.when.startswith('W'):
303 # This could be done with less code, but I wanted it to be clear
304 if self.utc:
305 t = time.gmtime(currentTime)
306 else:
307 t = time.localtime(currentTime)
308 currentHour = t[3]
309 currentMinute = t[4]
310 currentSecond = t[5]
311 currentDay = t[6]
312 # r is the number of seconds left between now and the next rotation
313 if self.atTime is None:
314 rotate_ts = _MIDNIGHT
315 else:
316 rotate_ts = ((self.atTime.hour * 60 + self.atTime.minute)*60 +
317 self.atTime.second)
318
319 r = rotate_ts - ((currentHour * 60 + currentMinute) * 60 +
320 currentSecond)
321 if r <= 0:
322 # Rotate time is before the current time (for example when
323 # self.rotateAt is 13:45 and it now 14:15), rotation is
324 # tomorrow.
325 r += _MIDNIGHT
326 currentDay = (currentDay + 1) % 7
327 result = currentTime + r
328 # If we are rolling over on a certain day, add in the number of days until
329 # the next rollover, but offset by 1 since we just calculated the time
330 # until the next day starts. There are three cases:
331 # Case 1) The day to rollover is today; in this case, do nothing
332 # Case 2) The day to rollover is further in the interval (i.e., today is
333 # day 2 (Wednesday) and rollover is on day 6 (Sunday). Days to
334 # next rollover is simply 6 - 2 - 1, or 3.
335 # Case 3) The day to rollover is behind us in the interval (i.e., today
336 # is day 5 (Saturday) and rollover is on day 3 (Thursday).
337 # Days to rollover is 6 - 5 + 3, or 4. In this case, it's the
338 # number of days left in the current week (1) plus the number
339 # of days in the next week until the rollover day (3).
340 # The calculations described in 2) and 3) above need to have a day added.
341 # This is because the above time calculation takes us to midnight on this
342 # day, i.e. the start of the next day.
343 if self.when.startswith('W'):
344 day = currentDay # 0 is Monday
345 if day != self.dayOfWeek:
346 if day < self.dayOfWeek:
347 daysToWait = self.dayOfWeek - day

Callers 7

__init__Method · 0.95
shouldRolloverMethod · 0.95
doRolloverMethod · 0.95
test_compute_rolloverFunction · 0.95
testMethod · 0.80

Calls 1

startswithMethod · 0.45

Tested by 4

test_compute_rolloverFunction · 0.76
testMethod · 0.64