Skip to content

Instantly share code, notes, and snippets.

@faveoled
Last active July 27, 2023 12:52
Show Gist options
  • Save faveoled/63922bded076b7edc0956503b19caa86 to your computer and use it in GitHub Desktop.
Save faveoled/63922bded076b7edc0956503b19caa86 to your computer and use it in GitHub Desktop.
Salary Timer. Calculates how much you earned at current point in time during the day
// Set the constants for schedule and pay rate
const schedule = {
start: "09:00", // Start of schedule in HH:mm format
end: "17:00", // End of schedule in HH:mm format
breakStart: "12:00", // Start of break time in HH:mm format
breakEnd: "13:00" // End of break time in HH:mm format
};
const payRate = 100; // Pay rate in dollars per day
// Get the current time in HH:mm format
let currentTime = new Date().toLocaleTimeString("en-US", {
hour12: false,
hour: "2-digit",
minute: "2-digit"
});
// Convert a time string to minutes
function timeToMinutes(time) {
let [hour, minute] = time.split(":");
return parseInt(hour) * 60 + parseInt(minute);
}
// Calculate the percentage of pay rate based on the current time and schedule
function calculatePercentage(currentTime, schedule) {
// Convert the times to minutes
let currentMinutes = timeToMinutes(currentTime);
let startMinutes = timeToMinutes(schedule.start);
let endMinutes = timeToMinutes(schedule.end);
let breakStartMinutes = timeToMinutes(schedule.breakStart);
let breakEndMinutes = timeToMinutes(schedule.breakEnd);
// Check if the current time is within the schedule
if (currentMinutes < startMinutes) {
return 0; // Before the start of schedule, no pay
} else if (
currentMinutes >= breakStartMinutes &&
currentMinutes <= breakEndMinutes
) {
return (breakStartMinutes - startMinutes) / (endMinutes - startMinutes); // Within break time, pay is fixed at the start of break
} else if (currentMinutes >= endMinutes) {
return 1; // After the end of schedule, full pay
} else {
return (currentMinutes - startMinutes) / (endMinutes - startMinutes); // Within working hours, pay is proportional to the time worked
}
}
// Calculate the floating point number with two decimal points
let percentage = calculatePercentage(currentTime, schedule);
let number = percentage * payRate;
number = number.toFixed(2); // Round to two decimal points
// tests
function assert(cond) {
if (!cond) {
throw Error("Test failure")
}
}
assert(timeToMinutes("00:00") === 0);
assert(timeToMinutes("23:59") === 1439);
assert(timeToMinutes("12:34") === 754);
assert(calculatePercentage("08:59", schedule) === 0);
assert(calculatePercentage("17:01", schedule) === 1);
assert(calculatePercentage("12:00", schedule) === 0.375);
assert(calculatePercentage("12:30", schedule) === 0.375);
assert(calculatePercentage("13:00", schedule) === 0.375);
assert(calculatePercentage("09:00", schedule) === 0);
assert(calculatePercentage("10:30", schedule) === 0.1875);
assert(calculatePercentage("11:59", schedule) === 0.3729166666666667);
assert(calculatePercentage("13:01", schedule) === 0.5020833333333333);
assert(calculatePercentage("15:15", schedule) === 0.78125);
assert(calculatePercentage("17:00", schedule) === 1);
// Print the number
print(number);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment