66 lines
1.7 KiB
Python
66 lines
1.7 KiB
Python
from datetime import datetime, timedelta
|
|
from typing import Tuple
|
|
|
|
|
|
def get_date_range(days: int) -> Tuple[datetime, datetime]:
|
|
"""Get date range for the last N days."""
|
|
end_date = datetime.now()
|
|
start_date = end_date - timedelta(days=days)
|
|
return start_date, end_date
|
|
|
|
|
|
def parse_timestamp(timestamp_str: str) -> datetime:
|
|
"""Parse a timestamp string to datetime."""
|
|
formats = [
|
|
"%Y-%m-%d %H:%M:%S",
|
|
"%Y-%m-%dT%H:%M:%S",
|
|
"%Y-%m-%d %H:%M:%S%z",
|
|
"%Y-%m-%dT%H:%M:%SZ",
|
|
]
|
|
|
|
for fmt in formats:
|
|
try:
|
|
return datetime.strptime(timestamp_str, fmt)
|
|
except ValueError:
|
|
continue
|
|
|
|
raise ValueError(f"Cannot parse timestamp: {timestamp_str}")
|
|
|
|
|
|
def format_duration(seconds: float) -> str:
|
|
"""Format duration in human-readable format."""
|
|
if seconds < 60:
|
|
return f"{seconds:.1f}s"
|
|
elif seconds < 3600:
|
|
minutes = seconds / 60
|
|
return f"{minutes:.1f}m"
|
|
elif seconds < 86400:
|
|
hours = seconds / 3600
|
|
return f"{hours:.1f}h"
|
|
else:
|
|
days = seconds / 86400
|
|
return f"{days:.1f}d"
|
|
|
|
|
|
def group_by_period(
|
|
commits: list,
|
|
period: str = "day",
|
|
) -> dict:
|
|
"""Group commits by time period."""
|
|
result = {}
|
|
for commit in commits:
|
|
if period == "hour":
|
|
key = commit.timestamp.strftime("%Y-%m-%d %H:00")
|
|
elif period == "day":
|
|
key = commit.timestamp.strftime("%Y-%m-%d")
|
|
elif period == "week":
|
|
key = commit.timestamp.strftime("%Y-%W")
|
|
elif period == "month":
|
|
key = commit.timestamp.strftime("%Y-%m")
|
|
else:
|
|
key = commit.timestamp.strftime("%Y-%m-%d")
|
|
|
|
result[key] = result.get(key, 0) + 1
|
|
|
|
return result
|