mirror of
https://github.com/zulip/zulip.git
synced 2025-10-24 00:23:49 +00:00
Fixes #2665. Regenerated by tabbott with `lint --fix` after a rebase and change in parameters. Note from tabbott: In a few cases, this converts technical debt in the form of unsorted imports into different technical debt in the form of our largest files having very long, ugly import sequences at the start. I expect this change will increase pressure for us to split those files, which isn't a bad thing. Signed-off-by: Anders Kaseorg <anders@zulip.com>
33 lines
1.2 KiB
Python
33 lines
1.2 KiB
Python
from datetime import datetime, timedelta
|
|
from typing import List, Optional
|
|
|
|
from analytics.lib.counts import CountStat
|
|
from zerver.lib.timestamp import floor_to_day, floor_to_hour, verify_UTC
|
|
|
|
|
|
# If min_length is None, returns end_times from ceiling(start) to floor(end), inclusive.
|
|
# If min_length is greater than 0, pads the list to the left.
|
|
# So informally, time_range(Sep 20, Sep 22, day, None) returns [Sep 20, Sep 21, Sep 22],
|
|
# and time_range(Sep 20, Sep 22, day, 5) returns [Sep 18, Sep 19, Sep 20, Sep 21, Sep 22]
|
|
def time_range(start: datetime, end: datetime, frequency: str,
|
|
min_length: Optional[int]) -> List[datetime]:
|
|
verify_UTC(start)
|
|
verify_UTC(end)
|
|
if frequency == CountStat.HOUR:
|
|
end = floor_to_hour(end)
|
|
step = timedelta(hours=1)
|
|
elif frequency == CountStat.DAY:
|
|
end = floor_to_day(end)
|
|
step = timedelta(days=1)
|
|
else:
|
|
raise AssertionError(f"Unknown frequency: {frequency}")
|
|
|
|
times = []
|
|
if min_length is not None:
|
|
start = min(start, end - (min_length-1)*step)
|
|
current = end
|
|
while current >= start:
|
|
times.append(current)
|
|
current -= step
|
|
return list(reversed(times))
|