-6

Say something happens at a rate of 5000 per hour and there's some amount remaining (let's say 2500 for example's sake).

How would I get the hours, minutes, and seconds remaining?

I would think it'd be something along the lines of:

hour = floor(remaining / perHour)
mins = floor(remaining % perHour / 60)
secs = floor(remaining % perHour % 60)

However calculating that back up using the below returns the incorrect amount.

time = hour + (mins / 60) + (secs / 60 / 60)
time * perHour // 3472.2 - not 2500

The time should be shorter so I'm obviously calculating this wrong. I would appreciate it if someone were to explain where I went wrong.

EDIT: I'm aware this isn't the Maths SE but the Maths SE is supposed to be for high level maths, and this is the next best fit.


Figured out a method, but it may not be the best so I'll leave this question up for a while longer.

hour = floor(remaining / perHour)
mins = floor(remaining / perHour * 60) - hour * 60
secs = floor(remaining / perHour * 3600) - mins * 60 - hour * 3600
Spedwards
  • 4,167
  • 16
  • 49
  • 106
  • https://stackoverflow.com/q/19700283/691711 – zero298 Nov 17 '17 at 20:23
  • 1
    *"EDIT: I'm aware this isn't the Maths SE but the Maths SE is supposed to be for high level maths, and this is the next best fit."* StackOverflow is *definately* not the right site for this. – James Douglas Nov 17 '17 at 21:04
  • 1
    I'm voting to close this question as off-topic because it is about [math.se] instead of programming or software development. – Pang Nov 20 '17 at 07:00

1 Answers1

0

Our question is how much time remains, and we know the rate of the process. No sweat, we establish a rate ( Items/minute, Items/sec) and divide the number of remaining elements by the rate. We could use any unit of time we want as long as we keep it consistent -- personally, I like rates in per-second because Hz.

A Demo

function getRemainingTime(remainingItems) {
  const processRatePerHour = 5000;
  const processRate = (processRatePerHour / 60 / 60); // 1.3888 per second

  remainingSeconds = remainingItems / processRate;
  
  return {
    hours: (remainingSeconds / 3600),
    minutes: (remainingSeconds / 60),
    seconds: remainingSeconds
  };

}

function showProgress(remainingItems) {
  var time = getRemainingTime(remainingItems);

  var list = document.createElement('ul');
  
  var processEnd = moment().add(time.seconds, 's');

  list.innerHTML = `
    <ul>
      <h3> With ${remainingItems} remaining, we will be finished ${processEnd.fromNow()}. </h3>
      <li> Hours Remaining: ${time.hours} </li>
      <li> Minutes Remaining: ${time.minutes} </li>
      <li> Seconds Remaining: ${time.seconds} </li>
    </ul>`;

  document.body.appendChild(list);

}

// Let's try it out!

showProgress(4999);
showProgress(2500);
showProgress(100);
body {
  font-family: 'Helvetica', sans-serif;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.2/moment.min.js"></script>

<h2> Please Accept My Answer v.0.2. </h2>
jdbiochem
  • 187
  • 9
  • I'm trying to get the remaining time in hours, minutes, and seconds. Not the rate per second. Please read the question or explain what you mean. – Spedwards Nov 17 '17 at 18:32
  • I don't have an elapsed time. Just the hourly rate, and the remaining amount. – Spedwards Nov 17 '17 at 18:34
  • Changed the answer to reflect. This may be simpler than you think. All we need to do is divide the number of remaining items by the rate. – jdbiochem Nov 17 '17 at 19:47
  • 1
    Proud to release the beta version of 'Please Accept My Answer', a simple vanilla JS app that tells you how much time remains in your process. – jdbiochem Nov 17 '17 at 20:02