title: How to calculate time span from timestamps?
body: <p>I have quite an interesting task at work - I need to find out how much time user spent doing something and all I have is timestamps of his savings. I know for a fact that user saves after each small portion of a work, so they is not far apart.</p>
  
  <p>The obvious solution would be to find out how much time one small item could possibly take and then just go through sorted timestamps and if the difference between current one and previous one is more than that, it means user had a coffee break, and if it's less, we can just add up this difference into total sum. Simple example code to illustrate that:</p>
  
  <pre><code>var prev_timestamp = null;
  var total_time = 0;
  foreach (timestamp in timestamps) {
      if (prev_timestamp != null) {
           var diff = timestamp - prev_timestamp;
           if (diff &lt; threshold) {
                total_time += diff;
           }
      }
      prev_timestamp = timestamp;
  }
  </code></pre>
  
  <p>The problem is, while I know about how much time is spent on one small portion, I don't want to depend on it. What if some user just that much slower than my predictions, I don't want him to be left without paycheck. So I was thinking, could there be some clever math solution to this problem, that could work without knowledge of what time interval is acceptable? </p>
  
  <p>PS. Sorry for misunderstanding, of course no one would pay people based on this numbers and even if they would, they understand that it is just an approximation. But I'd like to find a solution that would emit numbers as close to real life as possible.</p>