Create timespent.py
This commit is contained in:
53
timespent.py
Normal file
53
timespent.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import subprocess
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
# Run git log command
|
||||
log_output = subprocess.check_output(
|
||||
['git', 'log', '--pretty=format:%h %an %ad', '--date=iso'],
|
||||
text=True
|
||||
)
|
||||
|
||||
# Parse commit dates
|
||||
commit_times = []
|
||||
for line in log_output.splitlines():
|
||||
parts = line.strip().split()
|
||||
if len(parts) < 4:
|
||||
continue
|
||||
# Commit hash, author, datetime string
|
||||
dt_str = " ".join(parts[2:4]) # "YYYY-MM-DD HH:MM:SS"
|
||||
try:
|
||||
dt = datetime.strptime(dt_str, "%Y-%m-%d %H:%M:%S")
|
||||
commit_times.append(dt)
|
||||
except ValueError:
|
||||
continue
|
||||
|
||||
# Sort commits chronologically
|
||||
commit_times.sort()
|
||||
|
||||
# Session grouping (commits < 1 hour apart are same session)
|
||||
SESSION_GAP = timedelta(hours=1)
|
||||
sessions = []
|
||||
if commit_times:
|
||||
start = commit_times[0]
|
||||
prev = commit_times[0]
|
||||
|
||||
for t in commit_times[1:]:
|
||||
if t - prev > SESSION_GAP:
|
||||
# Close previous session
|
||||
sessions.append((start, prev))
|
||||
start = t
|
||||
prev = t
|
||||
sessions.append((start, prev)) # last session
|
||||
|
||||
# Estimate durations
|
||||
total_time = timedelta()
|
||||
for start, end in sessions:
|
||||
duration = end - start
|
||||
# Add a minimum session length (e.g. 30 min) so single commits aren’t near-zero
|
||||
if duration < timedelta(minutes=30):
|
||||
duration = timedelta(minutes=30)
|
||||
total_time += duration
|
||||
|
||||
print(f"Number of commits: {len(commit_times)}")
|
||||
print(f"Number of sessions: {len(sessions)}")
|
||||
print(f"Estimated total coding time: {total_time} (~{total_time.total_seconds()/3600:.1f} hours)")
|
||||
Reference in New Issue
Block a user