0

I have an array of date/timestamps saved as seconds, I'm looking to count the number of items that are between two timestamps (all times are assumed to be at midnight). I've cut down a larger function to the section that the problem seems to reside, let me know if you need any additional code.

var fullDay = 86399;
var end = parseInt(dataArray[0]) + parseInt(fullDay);

    for (var i = 0; i < numberOfDays; i++) {
        dataSegments[i] = sensorEventTotal(dataArray, end, previousEnd);
        previousEnd = end;
        end = parseInt(end) + parseInt(fullDay);
    }
    console.log(dataSegments.toString());

SensorEventTotal:

function sensorEventTotal (dataArray, end, previousEnd){
    var counter = 0;
    $.each(dataArray, function(i, item) {
        if (parseInt(item) < end && parseInt(item) > previousEnd) {
            counter++;
        }
    });
    previousEnd = end;
    return counter;
}

What I'm trying to do is to take the first date/time stamp and add 24 hours (fullDay value is 24 hours in seconds), I'm then looking to use that "end time" as the start point for the next loop with another 24 hours added onto that and so on.

In the end I'd want an array where each index stores the number of occurrences for each day e.g. dataSegments = [23,123,32,34] - so 23 events on day one, 123 events on day two etc.

At the moment this is the result I'm getting for dataSegments:

115,0,0,0,0,0

EDIT:

Sample of data in dataArray:

1496077569,1496077568,1496077567,1496077564,1496077563,1496077562,1496072956

Full array:

1496077569,1496077568,1496077567,1496077564,1496077563,1496077562,1496072956,1496072955,1496072951,1496072950,1496072949,1496072948,1496072809,1496072805,1496072804,1496072803,1495815090,1495815089,1495815088,1495807282,1495807281,1495807280,1495807279,1495807277,1495807276,1495807275,1495807274,1495807273,1495807267,1495807266,1495807265,1495805409,1495805408,1495805407,1495805406,1495805381,1495805380,1495805379,1495803061,1495803060,1495803059,1495803059,1495803000,1495802999,1495802998,1495786283,1495786282,1495786281,1495728263,1495728262,1495728261,1495728258,1495728257,1495728256,1495727698,1495727697,1495727696,1495727695,1495727694,1495727693,1495727491,1495727490,1495727489,1495727486,1495727485,1495727484,1495724286,1495724285,1495724284,1495724279,1495724278,1495724277,1495720363,1495720358,1495720357,1495720356,1495719373,1495719372,1495719368,1495719367,1495719366,1495717302,1495717301,1495717299,1495717298,1495717297,1495717296,1495713310,1495713309,1495713308,1495713305,1495713304,1495713303,1495713303,1495707902,1495707901,1495707897,1495707896,1495707895,1495707615,1495707611,1495707610,1495707609,1495707608,1495704627,1495704626,1495704625,1495704623,1495704622,1495704621,1495704133,1495704132,1495704128,1495704127,1495704126
5
  • Side note : you don't need jQuery to iterate an array, dataArray.forEach( item => {..}) is enough. Commented May 30, 2017 at 14:56
  • I checked using this site epochconverter.com and the values seem to match what I need. Commented May 30, 2017 at 15:00
  • 3
    Would it be possible for you to provide a sample dataArray in your question? Commented May 30, 2017 at 15:00
  • I've added it in as an edit. Commented May 30, 2017 at 15:02
  • 1
    Sorry, I noticed the mistake in my question, it was meant to say seconds! Commented May 30, 2017 at 15:05

2 Answers 2

3

This is what I managed to come up with. I hope code is clear just from variable names alone, given that the logic is very similar to yours.

const SECONDS_IN_DAY = 24 * 3600;
let events = [1496077569,1496077568,1496077567,1496077564,1496077563,1496077562,1496072956,1496072955,1496072951,1496072950,1496072949,1496072948,1496072809,1496072805,1496072804,1496072803,1495815090,1495815089,1495815088,1495807282,1495807281,1495807280,1495807279,1495807277,1495807276,1495807275,1495807274,1495807273,1495807267,1495807266,1495807265,1495805409,1495805408,1495805407,1495805406,1495805381,1495805380,1495805379,1495803061,1495803060,1495803059,1495803059,1495803000,1495802999,1495802998,1495786283,1495786282,1495786281,1495728263,1495728262,1495728261,1495728258,1495728257,1495728256,1495727698,1495727697,1495727696,1495727695,1495727694,1495727693,1495727491,1495727490,1495727489,1495727486,1495727485,1495727484,1495724286,1495724285,1495724284,1495724279,1495724278,1495724277,1495720363,1495720358,1495720357,1495720356,1495719373,1495719372,1495719368,1495719367,1495719366,1495717302,1495717301,1495717299,1495717298,1495717297,1495717296,1495713310,1495713309,1495713308,1495713305,1495713304,1495713303,1495713303,1495707902,1495707901,1495707897,1495707896,1495707895,1495707615,1495707611,1495707610,1495707609,1495707608,1495704627,1495704626,1495704625,1495704623,1495704622,1495704621,1495704133,1495704132,1495704128,1495704127,1495704126];
events = events.reverse();
let midnight = events[0] - events[0] % SECONDS_IN_DAY; // midnight before the first event
const eventsPerDay = []; // results array
const nrDays = 7; // lets count events for one week
let daysCounted = 0, eventsChecked = 0;
while (daysCounted < nrDays) {
    midnight += SECONDS_IN_DAY;
    let currentEvent = events[eventsChecked];
    let eventsInThisDay = 0;
    while (currentEvent < midnight) {
        eventsInThisDay++;
        eventsChecked++;
        currentEvent = events[eventsChecked];
    }
    eventsPerDay[daysCounted] = eventsInThisDay;
    daysCounted++;
}
console.log(eventsPerDay);

Notice that I reverse the sample array before running my execution. That is because your sample starts at May 29 and ends at May 25, so it's running backwards in time.

I encourage you to try your own code on a reversed array, might very well be the case that your solution is correct.

If you do not want to reverse the array, you could "reverse the counting" by going from the latest midnight to the first midnight, subtracting 1 day on each iteration.

Sign up to request clarification or add additional context in comments.

Comments

0

In my opininion (and coming example), you can do it a bit simpler. Also, I've noticed a little problem in your function: if your dataArray and number of days (let's call them N) were big, you would have to iterate over the data array N number of times. It could become inefficient. Luckily you can do it in one loop iteration:

let sensorEventTotal = (data, start, daysNum) => {
  // We create array of length daysNum filled with 0's.
  let days = new Array(daysNum);
  days.fill(0);

  for(let entry of data) {
    // If below the start timestamp, continue loop.
    if(entry < start) continue;

    // We calculate which day it is.
    let index = parseInt((entry - start) / fullDay);

    // We check if the entry is not from days we do not count.
    if(index < daysNum)
      days[index]++;
  }

  return days;
}

Code with working examples: http://jsbin.com/lekiboruki/edit?js,console.

EDIT: You didn't mention if your dataArray is sorted. My answer would also work on unsorted arrays.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.