diff --git a/src/components/humanedate.js b/src/components/humanedate.js index ac7cff1ef6..9abaa952c2 100644 --- a/src/components/humanedate.js +++ b/src/components/humanedate.js @@ -1,7 +1,7 @@ define(["datetime"], function(datetime) { "use strict"; - function humane_date(date_str) { + function humaneDate(date_str) { var format, time_formats = [ [90, "a minute"], [3600, "minutes", 60], @@ -24,5 +24,37 @@ define(["datetime"], function(datetime) { if (seconds < format[0]) return 2 == format.length ? format[1] + " ago" : Math.round(seconds / format[2]) + " " + format[1] + " ago"; return seconds > 47304e5 ? Math.round(seconds / 47304e5) + " centuries ago" : date_str } - return window.humane_date = humane_date, humane_date + + function humaneElapsed(firstDateStr, secondDateStr) { + // TODO replace this whole script with a library or something + var dateOne = new Date(firstDateStr); + var dateTwo = new Date(secondDateStr); + var delta = (dateTwo.getTime() - dateOne.getTime()) / 1e3; + + var days = Math.floor(delta % 31536e3 / 86400); + var hours = Math.floor(delta % 31536e3 % 86400 / 3600); + var minutes = Math.floor(delta % 31536e3 % 86400 % 3600 / 60); + var seconds = Math.round(delta % 31536e3 % 86400 % 3600 % 60); + + var elapsed = ""; + elapsed += 1 == days ? days + " day " : ""; + elapsed += days > 1 ? days + " days " : ""; + elapsed += 1 == hours ? hours + " hour " : ""; + elapsed += hours > 1 ? hours + " hours " : ""; + elapsed += 1 == minutes ? minutes + " minute " : ""; + elapsed += minutes > 1 ? minutes + " minutes " : ""; + elapsed += elapsed.length > 0 ? "and " : ""; + elapsed += 1 == seconds ? seconds + " second" : ""; + elapsed += 0 == seconds || seconds > 1 ? seconds + " seconds" : ""; + + return elapsed; + } + + window.humaneDate = humaneDate; + window.humaneElapsed = humaneElapsed; + + return { + humaneDate: humaneDate, + humaneElapsed: humaneElapsed + } }); \ No newline at end of file diff --git a/src/controllers/dashboardpage.js b/src/controllers/dashboardpage.js index be3ee9e081..4f41136e9f 100644 --- a/src/controllers/dashboardpage.js +++ b/src/controllers/dashboardpage.js @@ -461,7 +461,7 @@ define(["datetime", "events", "itemHelper", "serverNotifications", "dom", "globa if (!nowPlayingItem) { return { - html: "Last seen " + humane_date(session.LastActivityDate), + html: "Last seen " + humaneDate(session.LastActivityDate), image: imgUrl }; } diff --git a/src/controllers/devices.js b/src/controllers/devices.js index ef24f19172..aa2854558d 100644 --- a/src/controllers/devices.js +++ b/src/controllers/devices.js @@ -86,7 +86,7 @@ define(["loading", "dom", "libraryMenu", "globalize", "scripts/imagehelper", "hu deviceHtml += "
"; if (device.LastUserName) { deviceHtml += device.LastUserName; - deviceHtml += ", " + humane_date(device.DateLastActivity); + deviceHtml += ", " + humaneDate(device.DateLastActivity); } deviceHtml += " "; deviceHtml += "
"; diff --git a/src/controllers/scheduledtaskspage.js b/src/controllers/scheduledtaskspage.js index 4ca89b92de..acb88d53e2 100644 --- a/src/controllers/scheduledtaskspage.js +++ b/src/controllers/scheduledtaskspage.js @@ -62,23 +62,11 @@ define(["jQuery", "loading", "events", "globalize", "serverNotifications", "huma page.querySelector(".divScheduledTasks").innerHTML = html; } - function humane_elapsed(firstDateStr, secondDateStr) { - var dt1 = new Date(firstDateStr), - dt2 = new Date(secondDateStr), - seconds = (dt2.getTime() - dt1.getTime()) / 1e3, - numdays = Math.floor(seconds % 31536e3 / 86400), - numhours = Math.floor(seconds % 31536e3 % 86400 / 3600), - numminutes = Math.floor(seconds % 31536e3 % 86400 % 3600 / 60), - numseconds = Math.round(seconds % 31536e3 % 86400 % 3600 % 60), - elapsedStr = ""; - return elapsedStr += 1 == numdays ? numdays + " day " : "", elapsedStr += numdays > 1 ? numdays + " days " : "", elapsedStr += 1 == numhours ? numhours + " hour " : "", elapsedStr += numhours > 1 ? numhours + " hours " : "", elapsedStr += 1 == numminutes ? numminutes + " minute " : "", elapsedStr += numminutes > 1 ? numminutes + " minutes " : "", elapsedStr += elapsedStr.length > 0 ? "and " : "", elapsedStr += 1 == numseconds ? numseconds + " second" : "", elapsedStr += 0 == numseconds || numseconds > 1 ? numseconds + " seconds" : "" - } - function getTaskProgressHtml(task) { var html = ""; if (task.State === "Idle") { if (task.LastExecutionResult) { - html += globalize.translate("LabelScheduledTaskLastRan").replace("{0}", humane_date(task.LastExecutionResult.EndTimeUtc)).replace("{1}", humane_elapsed(task.LastExecutionResult.StartTimeUtc, task.LastExecutionResult.EndTimeUtc)); + html += globalize.translate("LabelScheduledTaskLastRan").replace("{0}", humaneDate(task.LastExecutionResult.EndTimeUtc)).replace("{1}", humaneElapsed(task.LastExecutionResult.StartTimeUtc, task.LastExecutionResult.EndTimeUtc)); if (task.LastExecutionResult.Status === "Failed") { html += " (" + globalize.translate("LabelFailed") + ")"; } else if (task.LastExecutionResult.Status === "Cancelled") { diff --git a/src/controllers/userprofilespage.js b/src/controllers/userprofilespage.js index c0d1d0d87b..a3eadf9347 100644 --- a/src/controllers/userprofilespage.js +++ b/src/controllers/userprofilespage.js @@ -128,7 +128,7 @@ define(["loading", "dom", "globalize", "humanedate", "paper-icon-button-light", function getLastSeenText(lastActivityDate) { if (lastActivityDate) { - return "Last seen " + humane_date(lastActivityDate); + return "Last seen " + humaneDate(lastActivityDate); } return ""; @@ -207,7 +207,7 @@ define(["loading", "dom", "globalize", "humanedate", "paper-icon-button-light", page.querySelector(".pending").innerHTML = users.map(getPendingUserHtml).join(""); } - + // TODO cvium: maybe reuse for invitation system function cancelAuthorization(page, id) { loading.show(); @@ -230,7 +230,7 @@ define(["loading", "dom", "globalize", "humanedate", "paper-icon-button-light", // TODO cvium renderPendingGuests(page, []); // ApiClient.getJSON(ApiClient.getUrl("Connect/Pending")).then(function (pending) { - // + // // }); }