Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds date parsing for stats #2

Merged
merged 1 commit into from
Mar 30, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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