- Add complete CLI command implementations with Typer subcommands - Implement utils/time.py with duration formatting and datetime utilities - Implement utils/path.py with project path management utilities - Wire up all commands to main CLI entry point
92 lines
2.2 KiB
Python
92 lines
2.2 KiB
Python
"""Time utility functions."""
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
def format_duration(minutes: int) -> str:
|
|
"""Format duration in minutes to human-readable string.
|
|
|
|
Args:
|
|
minutes: Duration in minutes.
|
|
|
|
Returns:
|
|
Human-readable duration string (e.g., "1h 30m", "45m").
|
|
"""
|
|
if minutes < 60:
|
|
return f"{minutes}m"
|
|
hours = minutes // 60
|
|
remaining_minutes = minutes % 60
|
|
if remaining_minutes == 0:
|
|
return f"{hours}h"
|
|
return f"{hours}h {remaining_minutes}m"
|
|
|
|
|
|
def parse_duration(duration_str: str) -> int:
|
|
"""Parse duration string to minutes.
|
|
|
|
Args:
|
|
duration_str: Duration string like "1h 30m", "45m", "2h".
|
|
|
|
Returns:
|
|
Duration in minutes.
|
|
"""
|
|
total_minutes = 0
|
|
duration_str = duration_str.lower().strip()
|
|
|
|
# Parse hours
|
|
if "h" in duration_str:
|
|
parts = duration_str.split()
|
|
for part in parts:
|
|
if "h" in part:
|
|
total_minutes += int(part.replace("h", "")) * 60
|
|
elif "m" in part:
|
|
total_minutes += int(part.replace("m", ""))
|
|
|
|
# If no hours, try just minutes
|
|
if total_minutes == 0 and "m" in duration_str:
|
|
total_minutes = int(duration_str.replace("m", ""))
|
|
elif total_minutes == 0:
|
|
try:
|
|
total_minutes = int(duration_str)
|
|
except ValueError:
|
|
return 0
|
|
|
|
return total_minutes
|
|
|
|
|
|
def is_recent(dt: datetime, hours: int = 24) -> bool:
|
|
"""Check if a datetime is within the specified hours.
|
|
|
|
Args:
|
|
dt: Datetime to check.
|
|
hours: Number of hours to consider as "recent".
|
|
|
|
Returns:
|
|
True if dt is within the specified hours.
|
|
"""
|
|
return datetime.now() - dt < timedelta(hours=hours)
|
|
|
|
|
|
def format_datetime(dt: datetime) -> str:
|
|
"""Format datetime to standard string.
|
|
|
|
Args:
|
|
dt: Datetime to format.
|
|
|
|
Returns:
|
|
Formatted string in YYYY-MM-DD HH:MM format.
|
|
"""
|
|
return dt.strftime("%Y-%m-%d %H:%M")
|
|
|
|
|
|
def format_date(dt: datetime) -> str:
|
|
"""Format datetime to date string.
|
|
|
|
Args:
|
|
dt: Datetime to format.
|
|
|
|
Returns:
|
|
Formatted string in YYYY-MM-DD format.
|
|
"""
|
|
return dt.strftime("%Y-%m-%d")
|