From c180557f523fcda0070872d7dd11daeffc6e939a Mon Sep 17 00:00:00 2001 From: Jarzon Date: Wed, 1 Feb 2017 13:30:38 -0500 Subject: [PATCH] Using decodeSteamTime() to parse date Adding to .decodeSteamTime() the ability to parse steam relative time --- components/groups.js | 3 ++- components/helpers.js | 25 ++++++++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/components/groups.js b/components/groups.js index 89e1391..845a000 100644 --- a/components/groups.js +++ b/components/groups.js @@ -2,6 +2,7 @@ var SteamCommunity = require('../index.js'); var SteamID = require('steamid'); var xml2js = require('xml2js'); var Cheerio = require('cheerio'); +var Helpers = require('./helpers.js'); SteamCommunity.prototype.getGroupMembers = function(gid, callback, members, link, addresses, addressIdx) { members = members || []; @@ -545,7 +546,7 @@ SteamCommunity.prototype.getAllGroupComments = function(gid, from, count, callba var $selector = $(this).find(".commentthread_author_link"); comment.authorName = $($selector).find("bdi").text(); comment.authorId = $($selector).attr("href").replace(/http:\/\/steamcommunity.com\/(id|profiles)\//, ""); - comment.date = $(this).find(".commentthread_comment_timestamp").text().trim(); + comment.date = Helpers.decodeSteamTime($(this).find(".commentthread_comment_timestamp").text().trim()); $selector = $(this).find(".commentthread_comment_text"); comment.commentId = $($selector).attr("id").replace("comment_content_", ""); diff --git a/components/helpers.js b/components/helpers.js index ec71ea3..d33d391 100644 --- a/components/helpers.js +++ b/components/helpers.js @@ -13,11 +13,26 @@ exports.isSteamID = function(input) { }; exports.decodeSteamTime = function(time) { - var parts = time.split('@'); - if (!parts[0].match(/,/)) { - // no year, assume current year - parts[0] += ", " + (new Date()).getFullYear(); + var date = new Date(); + + if (time.includes("@")) { + var parts = time.split('@'); + if (!parts[0].includes(",")) { + // no year, assume current year + parts[0] += ", " + date.getFullYear(); + } + + date = new Date(parts.join('@').replace(/(am|pm)/, ' $1') + " UTC"); // add a space so JS can decode it + } else { + // Relative date + var amount = time.replace(/(\d) (minutes|hour|hours) ago/, "$1"); + + if(time.includes("minutes")) { + date.setMinutes(date.getMinutes() - amount); + } else if(time.match(/hour|hours/)) { + date.setHours(date.getHours() - amount); + } } - return new Date(parts.join('@').replace(/(am|pm)/, ' $1') + " UTC"); // add a space so JS can decode it + return date; };