Skip to content

Commit

Permalink
only counting passed hours in order to give a better estimation of fi…
Browse files Browse the repository at this point in the history
…nishing time
  • Loading branch information
Ortega-Dan committed Apr 19, 2023
1 parent 80e09ce commit 320c4aa
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@


Just hit Alt + i\
and the extension will let you know how many hours you've worked\
and the extension will let you know how many hours you've recorded\
(how many hours you have in your google calendar for the current day)\
and how you are in comparison with an 8-hours-a-day work schedule

The Alt + o option shows the actual hours that are recorded and that have also already passed, allowing you to know by displaying for you how many hours are missing in practice, and around what time you will be done for the day.

If you want to know a different day hit Alt + k\
and enter the day you want to check according to the instructions in the prompt presented\
Expand Down
49 changes: 34 additions & 15 deletions src/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,22 @@ function standardizeToInternationalTime(receivedFormatTimeString) {
return finalTime;
}

function getHoursDiffBetweenTwoDateObjs(startDateTime, endDateTime) {
var minutesDiff = ((endDateTime - startDateTime) / 60000)
minutesDiff = Math.floor(minutesDiff)

return minutesDiff / 60
}


function convertDecimalHoursToTimeFormat(hoursInDecimalFormat) {
return "" + parseInt(hoursInDecimalFormat) + ":" + ("" + parseInt((hoursInDecimalFormat - parseInt(hoursInDecimalFormat)) * 60)).padStart(2, "0")
}

function showHoursDiffTo(hoursSum, hoursThreshold, label, showFinishingTime = false) {
function showHoursDiffTo(hoursSum, hoursThreshold, label, displayFinishingTime = false) {

if ((hoursThreshold - hoursSum) < 0) {
alert("[" + label + "]\n\nYou've worked " +
alert("[" + label + "]\n\n" + (displayFinishingTime ? "You've worked " : "You have ") +
convertDecimalHoursToTimeFormat((hoursThreshold - hoursSum) * -1) + " extra hours")
} else if ((hoursThreshold - hoursSum) == 0) {
alert("[" + label + "]\n\nYou're done for " + (label.toLowerCase().includes("week") ? "the week" : "the day") + "!")
Expand All @@ -64,7 +71,7 @@ function showHoursDiffTo(hoursSum, hoursThreshold, label, showFinishingTime = fa

var hours = dateTime.getHours()
alert("[" + label + "]\n\n" + timeDiffString + " hours missing" +
(showFinishingTime ?
(displayFinishingTime ?
"\n\nFinishing by " + (hours == 12 ? hours : hours % 12) + ":" + ("" + dateTime.getMinutes()).padStart(2, "0") + (hours / 12 < 1.0 ? " am" : " pm") : ""))

}
Expand All @@ -82,16 +89,17 @@ $("html").keydown(function (event) {

// Variable to hold query (entered or inferred)
var requiredDayText
var nowDateTime = new Date()

var showFinishingTime = event.code === "KeyO"

// Set the query for the current day
if (event.code === "KeyI" || event.code === "KeyO") {

var todaysDate = new Date()

// Google Calendar format
requiredDayText = "" + todaysDate.getDate()
// requiredDayText = months[todaysDate.getMonth()] + " " + todaysDate.getDate() +
// ", " + todaysDate.getFullYear()
requiredDayText = "" + nowDateTime.getDate()
// requiredDayText = months[nowDateTime.getMonth()] + " " + nowDateTime.getDate() +
// ", " + nowDateTime.getFullYear()
}

// Prompting for the query when user requested
Expand All @@ -109,6 +117,7 @@ $("html").keydown(function (event) {

// Initializing variable to add times
var calendarTimeAdder = 0
var passedHoursAdder = 0

var events = []

Expand Down Expand Up @@ -169,14 +178,20 @@ $("html").keydown(function (event) {
var startDateTime = new Date(fromDate + " " + fromTime);
var endDateTime = new Date(toDate + " " + toTime);


var minutesDiff = ((endDateTime - startDateTime) / 60000)
minutesDiff = Math.floor(minutesDiff)

var hoursLength = minutesDiff / 60
var hoursLength = getHoursDiffBetweenTwoDateObjs(startDateTime, endDateTime)
console.log("Hours: " + hoursLength)
calendarTimeAdder += hoursLength

// checking how many minutes or hours have actually passed
// (for now only used in current day finish estimation with KeyO)
if (startDateTime < nowDateTime) {
if (endDateTime < nowDateTime) {
passedHoursAdder += hoursLength
} else {
passedHoursAdder += getHoursDiffBetweenTwoDateObjs(startDateTime, nowDateTime)
}
}

}
})

Expand All @@ -191,14 +206,18 @@ $("html").keydown(function (event) {
alert("No active events found for filter [" + requiredDayText + "]")
// alert("No events owned, confirmed, or pending confirmation found for filter [" + requiredDayText + "]")
} else {
alert("[" + requiredDayText + "]\n\n" + convertDecimalHoursToTimeFormat(calendarTimeAdder) + " hours worked")
alert("[" + requiredDayText + "]\n\n" + convertDecimalHoursToTimeFormat(showFinishingTime ? passedHoursAdder : calendarTimeAdder) + " hours " + (showFinishingTime ? "worked" : "recorded"))

if (requiredDayText == "Entire Week") {
// show hours diff for week query
showHoursDiffTo(calendarTimeAdder, weeklyHoursMargin, requiredDayText)
} else {
// Show hours diff for single day query
showHoursDiffTo(calendarTimeAdder, dailyHoursMargin, requiredDayText, event.code === "KeyO")
if (showFinishingTime) {
showHoursDiffTo(passedHoursAdder, dailyHoursMargin, requiredDayText, true)
} else {
showHoursDiffTo(calendarTimeAdder, dailyHoursMargin, requiredDayText)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<h3>Calendar Hours Counter</h3>
<ul style="min-width: 350px;">
<li>Alt + I: Current Day Hours and Diff </li>
<li>Alt + O: Current Day Hours and Diff (with estimated finishing time)</li>
<li>Alt + O: Current Day Hours already passed and Diff (with estimated finishing time)</li>
<li>Alt + K: Count Week Hours and Diff (with optional filter for different days according to instructions).</li>
</ul>
<p style="color: darkblue;">Works on Google Calendar for personal and enterprise versions, with English (US) language,
Expand Down

0 comments on commit 320c4aa

Please sign in to comment.