-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
time_utils.py
63 lines (51 loc) · 1.62 KB
/
time_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import time
from collections.abc import Mapping
from datetime import datetime, timedelta
from django.utils import timezone
from django.utils.timesince import timesince
from django.utils.translation import gettext as _
from sentry.models.group import Group
DAY_IN_SEC = 24 * 60 * 60
def get_approx_start_time(group: Group):
event = group.get_recommended_event_for_environments()
if event is None:
return None
occurrence = event.occurrence
if occurrence is None:
return None
regression_time = occurrence.evidence_data.get("breakpoint", None)
return regression_time
def time_since(value: datetime):
"""
Display the relative time
"""
now = timezone.now()
if value < (now - timedelta(days=5)):
return value.date()
diff = timesince(value, now)
if diff == timesince(now, now):
return "Just now"
if diff == "1 day":
return _("Yesterday")
return f"{diff} ago"
def get_relative_time(
anchor: int, relative_days: int, retention_days: int = 90
) -> Mapping[str, datetime]:
max_time = time.time()
min_time = max_time - retention_days * DAY_IN_SEC
before_time = anchor - relative_days * DAY_IN_SEC
before_datetime = (
datetime.fromtimestamp(before_time)
if before_time >= min_time
else datetime.fromtimestamp(min_time)
)
after_time = anchor + relative_days * DAY_IN_SEC
after_datetime = (
datetime.fromtimestamp(after_time)
if after_time <= max_time
else datetime.fromtimestamp(max_time)
)
return {
"start": before_datetime,
"end": after_datetime,
}