3

So, I have this web server that parses an XML-file with a certain 'time-period'.

The time-period is presented as follows (I can't change this):

00:05:00 00:09:15 01:13:15 in an HH:MM:SS format.

I want to do an _.filter() select on all objects with a date between for instance 00:00:00 and 00:10:00, and then a filter with all objects between 00:10:00 and 00:20:00.

EDIT (clarification): With the time periods are 'times watched'. Let's say there are 50 entries of the time period, ranging from 00:00:00 to 1:30:00, but I want to make a new list that contains

  • The time period
  • The average 'times watched' in that time period.

This means I need some way to say: 'Select all time periods between zero minutes and 10 minutes. Give me a list of those. Compute the mean'.

For that, I would need to have some possibility to do arithmetic operations, seeing I would like to

  • Compare dates
  • Add 10 minutes on each iteration

However, the Javascript Date object doesn't seem to handle 'just times' that well, neither does the moment.js library. How would I go for this?

Mats Willemsen
  • 815
  • 1
  • 9
  • 25
  • Could you clarify what you mean in your 4th paragraph (filter)? I'm taking that you want to run `_.filter()` twice, once checking for `00:00:00` to `00:10:00`, and the second `00:10:00` to `00:20:00`. Also, about "Add 10 minutes on each iteration", is that for all of them in the filter, or selected ones within the filter? – Qantas 94 Heavy Aug 25 '13 at 13:54
  • 1
    I added some clarification! – Mats Willemsen Aug 25 '13 at 14:05
  • 1
    possible duplicate of [Convert HH:MM:SS string to seconds only in javascript](http://stackoverflow.com/questions/9640266/convert-hhmmss-string-to-seconds-only-in-javascript) – WiredPrairie Aug 25 '13 at 14:40

2 Answers2

1

Here is how I would do in JS:

times=['00:05:00', '00:09:15', '01:13:15'];
  1. First get seconds

    function getsec(time){
      arr=time.split(':');
      return (+arr[0])*3600 + (+arr[1])*60 + (+arr[2]);
    }
    
    times.map(getsec)
    [ 300, 555, 4395 ]
    
  2. Then filter times into slots of particular time period

    function filter(last, curr, index, array){
      chk=parseInt(curr/600); //slots for 10 minutes
      if (typeof last[chk] === 'undefined')
      {
        last[chk]=[];
        last[chk].push(curr);
      }
      else
        last[chk].push(curr);
      return(last)
    }
    
    times.map(getsec).reduce(filter,[]);
    [ [ 300, 555 ], , , , , , , [ 4395 ] ]
    

    Returns array of arrays. Each child array contains times within slots in increasing order

  3. Calculate mean

    function avg(last, curr, index, array){
      if(index==array.length-1)
      return (last+curr)/array.length;
      else
      return last+curr;
    }
    
    times.map(getsec).reduce(avg,0);
    1750
    
    function getavg(arr){
      return arr.reduce(avg,0);
    }
    
    times.map(getsec).reduce(filter,[]).map(getavg);
    [ 427.5, , , , , , , 4395 ]
    

    Since we are dealing with array of arrays we need a wrapper.

Finally I would say that if you have many entries, databases would be faster.

user568109
  • 47,225
  • 17
  • 99
  • 123
-1

Simplest way would be for you to convert everything into seconds, and then do your thing.

inSeconds = function(time) {
  var secs, arr = [];
  arr = time.split(':').map(function(item) { return parseInt(item) });
  secs = arr[0]*3600 + arr[1]*60 + arr[2];
  return secs;
}
kumarharsh
  • 18,961
  • 8
  • 72
  • 100