refactor date utils

This commit is contained in:
dkanada 2019-09-11 03:50:29 -07:00
parent a1c66f31fc
commit 2690381144
5 changed files with 40 additions and 20 deletions

View File

@ -1,7 +1,7 @@
define(["datetime"], function(datetime) { define(["datetime"], function(datetime) {
"use strict"; "use strict";
function humane_date(date_str) { function humaneDate(date_str) {
var format, time_formats = [ var format, time_formats = [
[90, "a minute"], [90, "a minute"],
[3600, "minutes", 60], [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"; 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 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
}
}); });

View File

@ -461,7 +461,7 @@ define(["datetime", "events", "itemHelper", "serverNotifications", "dom", "globa
if (!nowPlayingItem) { if (!nowPlayingItem) {
return { return {
html: "Last seen " + humane_date(session.LastActivityDate), html: "Last seen " + humaneDate(session.LastActivityDate),
image: imgUrl image: imgUrl
}; };
} }

View File

@ -86,7 +86,7 @@ define(["loading", "dom", "libraryMenu", "globalize", "scripts/imagehelper", "hu
deviceHtml += "<div class='cardText cardText-secondary'>"; deviceHtml += "<div class='cardText cardText-secondary'>";
if (device.LastUserName) { if (device.LastUserName) {
deviceHtml += device.LastUserName; deviceHtml += device.LastUserName;
deviceHtml += ", " + humane_date(device.DateLastActivity); deviceHtml += ", " + humaneDate(device.DateLastActivity);
} }
deviceHtml += "&nbsp;"; deviceHtml += "&nbsp;";
deviceHtml += "</div>"; deviceHtml += "</div>";

View File

@ -62,23 +62,11 @@ define(["jQuery", "loading", "events", "globalize", "serverNotifications", "huma
page.querySelector(".divScheduledTasks").innerHTML = html; 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) { function getTaskProgressHtml(task) {
var html = ""; var html = "";
if (task.State === "Idle") { if (task.State === "Idle") {
if (task.LastExecutionResult) { 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") { if (task.LastExecutionResult.Status === "Failed") {
html += " <span style='color:#FF0000;'>(" + globalize.translate("LabelFailed") + ")</span>"; html += " <span style='color:#FF0000;'>(" + globalize.translate("LabelFailed") + ")</span>";
} else if (task.LastExecutionResult.Status === "Cancelled") { } else if (task.LastExecutionResult.Status === "Cancelled") {

View File

@ -128,7 +128,7 @@ define(["loading", "dom", "globalize", "humanedate", "paper-icon-button-light",
function getLastSeenText(lastActivityDate) { function getLastSeenText(lastActivityDate) {
if (lastActivityDate) { if (lastActivityDate) {
return "Last seen " + humane_date(lastActivityDate); return "Last seen " + humaneDate(lastActivityDate);
} }
return ""; return "";
@ -207,7 +207,7 @@ define(["loading", "dom", "globalize", "humanedate", "paper-icon-button-light",
page.querySelector(".pending").innerHTML = users.map(getPendingUserHtml).join(""); page.querySelector(".pending").innerHTML = users.map(getPendingUserHtml).join("");
} }
// TODO cvium: maybe reuse for invitation system // TODO cvium: maybe reuse for invitation system
function cancelAuthorization(page, id) { function cancelAuthorization(page, id) {
loading.show(); loading.show();
@ -230,7 +230,7 @@ define(["loading", "dom", "globalize", "humanedate", "paper-icon-button-light",
// TODO cvium // TODO cvium
renderPendingGuests(page, []); renderPendingGuests(page, []);
// ApiClient.getJSON(ApiClient.getUrl("Connect/Pending")).then(function (pending) { // ApiClient.getJSON(ApiClient.getUrl("Connect/Pending")).then(function (pending) {
// //
// }); // });
} }