Skip to content

Commit

Permalink
Merge pull request #2 from LanaCoyote/master
Browse files Browse the repository at this point in the history
Adds date parsing for stats
  • Loading branch information
Giovanni committed Mar 30, 2017
2 parents 66d24ec + 0436810 commit 9a52abd
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/overwatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ const PLATFORMS = {
PC : "pc"
}

const parseDate = function(str) {
if (str.indexOf(':') > 0) {
let intervals = str.split(':').reverse();
intervals.at0 = (idx) => intervals[idx] || 0;
// UTC date gives us milliseconds since the unix epoch
return Date.UTC(1970, 0, intervals.at0(3) + 1, intervals.at0(2), intervals.at0(1), intervals.at0(0));
}
if (str.endsWith('s')) str = str.substr(0, str.length - 1); // remove trailing s
if (str.endsWith("second")) return parseInt(str) * 1000;
if (str.endsWith("minute")) return parseInt(str) * 60000;
if (str.endsWith("hour")) return parseInt(str) * 3600000;
if (str.endsWith("day")) return parseInt(str) * 86400000;
return parseInt(str);
}

let OverwatchProvider = function() {
var self = this;

Expand All @@ -29,7 +44,9 @@ let OverwatchProvider = function() {
}

String.prototype.cast = function() {
return this.indexOf('.') > 0 ? parseFloat(this) : parseInt(this.replace(/,/g,''));
if (this.indexOf('.') > 0) return parseFloat(this);
if (this.indexOf(':') > 0 || this.split(' ').length > 1) return parseDate(this);
return parseInt(this.replace(/,/g,''));
}

let getUrl = (platform, region, tag) => {
Expand Down

0 comments on commit 9a52abd

Please sign in to comment.