Calculating the delay in starting porridge cooking for tomorrow

Sometimes I live alone and put porridge in the slow cooker the night before so that it is ready by the time I have breakfast.

And before, I was constantly confused – what delayed start should I set in the evening so that the porridge would be ready at a certain time in the morning?

As a result, I made a table with formulas and wrote Google Apps Script code to know what exact time to set for delaying the start of cooking on the old multicooker.

The problem of choosing the time to cook porridge

Breakfast time is very important and I never skip it. I usually have breakfast quite early. The problem of calculating time is really relevant for me. I tried to calculate the delay of starting to cook porridge for tomorrow on a calculator – somehow strange and long.

Old multicooker

Old multicooker

Structured approach – table

I thought that to solve this problem I could use Google Sheets and create a formula that would calculate the delay time of the multicooker depending on the time when the porridge is needed ready.

In the first column I set the current time using the formula:

=TIME(HOUR(NOW()); MINUTE(NOW()); SECOND(NOW()))

In the second I indicate the time when the porridge should be ready, for example, 06:00.

In the third column I write the duration of cooking the porridge in hours, for example for buckwheat 00:40.

In the last column I have compiled a simple formula that will calculate the delay time for the multicooker to start.

The table is here.

Table "Delaying the start of cooking porridge until tomorrow"

Table “Delaying the start of cooking porridge until tomorrow”

Automate the process using Google Apps Script

Function CALCULATE_DELAY calculates the necessary delay in starting the multicooker so that the porridge is ready at the specified time. Only two parameters are required: the desired end time and the cooking time. I will describe what the code does:

  1. Checking that the input is treated as a string:

desiredCompletionTime = desiredCompletionTime.toString();
cookingTime = cookingTime.toString();

The function starts by converting the input parameters to strings to ensure that they can be processed correctly later.

  1. Getting the current time

var currentDateTime = new Date();
var currentHours = currentDateTime.getHours();
var currentMinutes = currentDateTime.getMinutes();

The current date and time are retrieved using the “new Date()” This gives the function a baseline for calculating latency.

  1. Parse the desired completion time

var desiredParts = desiredCompletionTime.split(':');
var desiredDateTime = new Date(currentDateTime);
desiredDateTime.setHours(parseInt(desiredParts[0], 10));
desiredDateTime.setMinutes(parseInt(desiredParts[1], 10));
desiredDateTime.setSeconds(0);

The desired end time is broken down into hours and minutes. These values ​​are then used to set the desired end time for the new date object (desiredDateTime»).

  1. If necessary, we will adjust the date for tomorrow.

if (desiredDateTime <= currentDateTime) {
  desiredDateTime.setDate(desiredDateTime.getDate() + 1);
}

If the desired end time is earlier than the current time, the function assumes that the end time is the next day and adjusts the date accordingly.

  1. Parsim cooking time

var cookingParts = cookingTime.split(':');
var cookingTimeMinutes = parseInt(cookingParts[0], 10) * 60 + parseInt(cookingParts[1], 10);

Cooking time is broken down into hours and minutes, then converted into total minutes for easy calculation.

  1. Calculate the start time of cooking

var startCookingTime = new Date(desiredDateTime.getTime() - cookingTimeMinutes * 60000);

The function calculates the exact time to start the multicooker by subtracting the cooking time (in milliseconds) from the desired end time.

  1. We calculate the required delay based on the current time in minutes

var delayMinutes = (startCookingTime - currentDateTime) / 60000;

The delay time in minutes is calculated by finding the difference between the cooking start time and the current time, then converting that difference from milliseconds to minutes.

  1. Convert the delay time into hours and minutes

var delayHours = Math.floor(delayMinutes / 60);
var delayRemainingMinutes = Math.round(delayMinutes % 60);

The delay time is then converted into hours and minutes for easy interpretation and adjustment on the slow cooker.

  1. Format the delay as HH:MM

Logger.log(`Результат:\n${('0' + delayHours).slice(-2) + ':' + ('0' + delayRemainingMinutes).slice(-2)}`)
return ('0' + delayHours).slice(-2) + ':' + ('0' + delayRemainingMinutes).slice(-2);

The delay time is formatted in “HH:MM” format and is logged for debugging purposes.

Testing the function CALCULATE_DELAY:

function test() {
  CALCULATE_DELAY("05:20:00", "00:40:00")
}

This test example calculates the delay start time for porridge that is due to be ready at 05:20 AM and takes 40 minutes to cook.

The entire code looks like this:

/**
 * Расчет времени отсрочки старта для мультиварки
 * 
 * https://habr.com/ru/articles/833648/
 */

function CALCULATE_DELAY(desiredCompletionTime, cookingTime) {
  desiredCompletionTime = desiredCompletionTime.toString();
  cookingTime = cookingTime.toString();
  var currentDateTime = new Date();
  var currentHours = currentDateTime.getHours();
  var currentMinutes = currentDateTime.getMinutes();
  var desiredParts = desiredCompletionTime.split(':');
  var desiredDateTime = new Date(currentDateTime);
  desiredDateTime.setHours(parseInt(desiredParts[0], 10));
  desiredDateTime.setMinutes(parseInt(desiredParts[1], 10));
  desiredDateTime.setSeconds(0);
  if (desiredDateTime <= currentDateTime) {
    desiredDateTime.setDate(desiredDateTime.getDate() + 1);
  }
  var cookingParts = cookingTime.split(':');
  var cookingTimeMinutes = parseInt(cookingParts[0], 10) * 60 + parseInt(cookingParts[1], 10);
  var startCookingTime = new Date(desiredDateTime.getTime() - cookingTimeMinutes * 60000);
  var delayMinutes = (startCookingTime - currentDateTime) / 60000;
  var delayHours = Math.floor(delayMinutes / 60);
  var delayRemainingMinutes = Math.round(delayMinutes % 60);
  Logger.log(`Результат:\n${('0' + delayHours).slice(-2) + ':' + ('0' + delayRemainingMinutes).slice(-2)}`)
  return ('0' + delayHours).slice(-2) + ':' + ('0' + delayRemainingMinutes).slice(-2);
}

function test() {
  CALCULATE_DELAY("05:20:00", "00:40:00")
}

Results

In the fast-paced world we live in, automating everyday tasks can save you valuable time and effort. This code, written in Google Apps Script, will help you calculate the exact time to delay the start of your multicooker.

Author: Mikhail Shardin

August 5, 2024

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *