MCPcopy Index your code
hub / github.com/RustPython/RustPython / computeRollover

Method computeRollover

Lib/logging/handlers.py:286–361  ·  view source on GitHub ↗

Work out the rollover time based on the specified time.

(self, currentTime)

Source from the content-addressed store, hash-verified

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