summaryrefslogtreecommitdiff
path: root/plugins/claude-security/scripts/lib/duration.py
blob: 4df78f8e5f0782d495f26f23d667ccb355b264ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
"""How long a run took, from the start time that the run recorded."""

from __future__ import annotations

from datetime import datetime

from .strictjson import is_str


def elapsed_seconds(started_at: object, now: datetime) -> int | None:
    """Whole seconds from a run's recorded started_at to now, floored at 0.

    None when started_at is absent, unparseable or timezone-naive.
    """
    if not is_str(started_at):
        return None
    try:
        started = datetime.fromisoformat(started_at)
    except ValueError:
        return None
    return max(int((now - started).total_seconds()), 0) if started.tzinfo else None