Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save blopa/c9ee9b4fcfdb48f07dc0967de5d75d0d to your computer and use it in GitHub Desktop.
Save blopa/c9ee9b4fcfdb48f07dc0967de5d75d0d to your computer and use it in GitHub Desktop.
Code for post "Tracking my working hours on personal projects using Node.js"
async function calculateWorkHours() {
try {
const commits = await getCommitLog();
if (commits.length === 0) {
console.log('No commits found in this repository.');
return;
}
let totalWorkTime = 0;
let currentCommit = commits[0];
const maxInterval = 5 * (60 * 60 * 1000); // 5 hours in milliseconds
for (let i = 1; i < commits.length; i++) {
const nextCommit = commits[i];
const currentCommitTime = new Date(currentCommit.date).getTime();
const nextCommitTime = new Date(nextCommit.date).getTime();
if ((currentCommitTime - nextCommitTime) > maxInterval) {
totalWorkTime += currentCommitTime - new Date(commits[i - 1].date).getTime();
currentCommit = nextCommit;
}
}
totalWorkTime += new Date(currentCommit.date).getTime() - new Date(commits[commits.length - 1].date).getTime();
const totalWorkHours = totalWorkTime / (1000 * 60 * 60);
console.log(`Total work hours: ${totalWorkHours.toFixed(2)} hours`);
} catch (error) {
console.error('Error calculating work hours:', error);
}
}
calculateWorkHours();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment