Skip to content

Commit

Permalink
wrote date rounding function, needs tests, faulty
Browse files Browse the repository at this point in the history
  • Loading branch information
atruskie committed Jan 29, 2015
1 parent 93d603e commit 0a19e26
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/app/d3Bindings/eventDistribution/distributionVisualisation.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ angular
var niceLow = d3.time.second.floor(low);
var niceHigh = d3.time.second.ceil(floor);

var m = moment();
var m = roundDate(tileSizePixels, low);

// use d3's in built range functionality to generate steps
var steps = d3.time.seconds(niceLow, niceHigh, tileSizeSeconds);
Expand All @@ -208,7 +208,33 @@ angular
tileSizeSeconds
}

function roundDate()
function roundDate(roundToSeconds, roundStyle, date) {
if (arguments.length === 2) {
date = roundStyle;
roundStyle = "round";
}
else if (arguments.length !== 3) {
throw new Error("Expected either 2 or 3 arguments");
}

var roundToMilliseconds = roundToSeconds * 1000,
unix = date.getTime(),
remainder = unix % roundToMilliseconds,
result;

if (roundStyle === "round") {
roundStyle = unix / remainder >= 0.5 ? "ceil" : "floor";
}

if (roundStyle === "floor") {
result = new Date(unix - remainder)
}
else if (roundStyle === "ceil") {
result = new Date(unix + (roundToMilliseconds - remainder));
}

return result;
}
}
}
]
Expand Down

0 comments on commit 0a19e26

Please sign in to comment.