MCPcopy Create free account
hub / github.com/celery/celery / remaining

Function remaining

celery/utils/time.py:236–307  ·  view source on GitHub ↗

Calculate the real remaining time for a start date and a timedelta. For example, "how many seconds left for 30 seconds after start?" For relative schedules, the deadline is the last rounded local boundary at or before ``start + ends_in`` in real time, never at or before ``start``;

(
        start: datetime, ends_in: timedelta | ffwd, now: datetime | None = None,
        relative: bool = False)

Source from the content-addressed store, hash-verified

234
235
236def remaining(
237 start: datetime, ends_in: timedelta | ffwd, now: datetime | None = None,
238 relative: bool = False) -> timedelta:
239 """Calculate the real remaining time for a start date and a timedelta.
240
241 For example, "how many seconds left for 30 seconds after start?"
242
243 For relative schedules, the deadline is the last rounded local boundary at
244 or before ``start + ends_in`` in real time, never at or before ``start``;
245 day intervals use calendar days.
246
247 Arguments:
248 start (~datetime.datetime): Starting date.
249 ends_in (~datetime.timedelta, ~celery.utils.time.ffwd): The end delta.
250 relative (bool): If enabled the end time will be calculated
251 using :func:`delta_resolution` (i.e., rounded to the
252 resolution of `ends_in`).
253 now (~datetime.datetime): Current time and date.
254 Defaults to :func:`datetime.now(timezone.utc)`.
255
256 Returns:
257 ~datetime.timedelta: Remaining time.
258 """
259 now = now or datetime.now(datetime_timezone.utc)
260 if isinstance(ends_in, timedelta) and not (
261 relative and ends_in >= timedelta(days=1)):
262 end_date = add_seconds_to_datetime(start, ends_in.total_seconds())
263 else:
264 # Day-resolution relative schedules advance by local calendar days.
265 # An elapsed-time addition can land on the wrong date across DST.
266 end_date = start + ends_in
267 if relative:
268 candidates = _relative_end_dates(end_date, ends_in)
269 start_utc = start.astimezone(timezone.utc)
270 if ends_in >= timedelta(days=1):
271 # Use the first midnight after start, including a repeated
272 # midnight whose first occurrence has already passed.
273 future = [dt for dt in candidates if dt > start_utc]
274 end_date = min(future, default=end_date.astimezone(timezone.utc))
275 else:
276 end_utc = start_utc + ends_in
277 # Rounding may enter an ambiguous period even when the
278 # unrounded time is outside it. Choose by the UTC occurrence.
279 before_end = [dt for dt in candidates if dt <= end_utc]
280 rounded = max(before_end) if before_end else min(candidates)
281 if ends_in >= timedelta(minutes=1) and rounded <= start_utc:
282 # A partial-hour rollback can put the rounded boundary
283 # before start. Consider both the next clock boundary and
284 # the calendar boundary, including both occurrences.
285 unit = (timedelta(hours=1) if ends_in >= timedelta(hours=1)
286 else timedelta(minutes=1))
287 candidates += _relative_end_dates(end_date + unit, ends_in)
288 candidates += _relative_end_dates(start + ends_in, ends_in)
289 future = [dt for dt in candidates if dt > start_utc]
290 before_end = [dt for dt in future if dt <= end_utc]
291 rounded = (max(before_end) if before_end
292 else min(future, default=end_utc))
293 end_date = rounded

Callers 4

test_remainingFunction · 0.90
remaining_estimateMethod · 0.85
remaining_estimateMethod · 0.85

Calls 4

add_seconds_to_datetimeFunction · 0.85
_relative_end_datesFunction · 0.85
nowMethod · 0.45
formatMethod · 0.45

Tested by 2

test_remainingFunction · 0.72