Add utils module (date utilities and config loader)
This commit is contained in:
@@ -1,32 +1,62 @@
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from typing import Optional, Tuple
|
from typing import Tuple
|
||||||
|
|
||||||
|
|
||||||
def parse_date(date_str: Optional[str]) -> Optional[datetime]:
|
def get_date_range(days: int) -> Tuple[datetime, datetime]:
|
||||||
"""Parse a date string into a datetime object."""
|
"""Get date range for the last N days."""
|
||||||
if not date_str:
|
end_date = datetime.now()
|
||||||
return None
|
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 = [
|
formats = [
|
||||||
"%Y-%m-%d",
|
|
||||||
"%Y-%m-%d %H:%M:%S",
|
"%Y-%m-%d %H:%M:%S",
|
||||||
"%Y-%m-%dT%H:%M:%S",
|
"%Y-%m-%dT%H:%M:%S",
|
||||||
"%Y/%m/%d",
|
"%Y-%m-%d %H:%M:%S%z",
|
||||||
"%m/%d/%Y",
|
"%Y-%m-%dT%H:%M:%SZ",
|
||||||
"%d/%m/%Y",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
for fmt in formats:
|
for fmt in formats:
|
||||||
try:
|
try:
|
||||||
return datetime.strptime(date_str, fmt)
|
return datetime.strptime(timestamp_str, fmt)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
return None
|
raise ValueError(f"Cannot parse timestamp: {timestamp_str}")
|
||||||
|
|
||||||
|
|
||||||
def get_date_range(days: int) -> Tuple[datetime, datetime]:
|
def format_duration(seconds: float) -> str:
|
||||||
"""Get a date range for the last N days."""
|
"""Format duration in human-readable format."""
|
||||||
end_date = datetime.now()
|
if seconds < 60:
|
||||||
start_date = end_date - timedelta(days=days)
|
return f"{seconds:.1f}s"
|
||||||
return start_date, end_date
|
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%U")
|
||||||
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user