From cc49e703dcd876093a6bf2665f54c37adf11ea04 Mon Sep 17 00:00:00 2001 From: ElementalAlchemist Date: Tue, 26 Oct 2021 22:12:57 -0500 Subject: [PATCH] Do a general cleanup of time conversion names to make times overall consistent --- thrimbletrimmer/scripts/common.js | 30 +++++++++---------- thrimbletrimmer/scripts/edit.js | 50 +++++++++++-------------------- thrimbletrimmer/scripts/stream.js | 25 +++++++++++----- 3 files changed, 50 insertions(+), 55 deletions(-) diff --git a/thrimbletrimmer/scripts/common.js b/thrimbletrimmer/scripts/common.js index e78fc32..c93086b 100644 --- a/thrimbletrimmer/scripts/common.js +++ b/thrimbletrimmer/scripts/common.js @@ -87,31 +87,31 @@ function updateVideoPlayer(newPlaylistURL) { player.src({ src: rangedPlaylistURL }); } -function parseInputTimeAsNumberOfSeconds(inputTime) { +function dateObjFromBusTime(busTime) { // We need to handle inputs like "-0:10:15" in a way that consistently makes the time negative. // Since we can't assign the negative sign to any particular part, we'll check for the whole thing here. let direction = 1; - if (inputTime.startsWith("-")) { - inputTime = inputTime.slice(1); + if (busTime.startsWith("-")) { + busTime = busTime.slice(1); direction = -1; } - const parts = inputTime.split(":", 3); - return (parseInt(parts[0]) + (parts[1] || 0) / 60 + (parts[2] || 0) / 3600) * 60 * 60 * direction; + const parts = busTime.split(":", 3); + const hours = (parts[0] || 0) * direction; + const minutes = (parts[1] || 0) * direction; + const seconds = (parts[2] || 0) * direction; + const time = new Date(globalBusStartTime); + time.setHours(time.getHours() + hours); + time.setMinutes(time.getMinutes() + minutes); + time.setSeconds(time.getSeconds() + seconds); + return time; } function dateObjFromWubloaderTime(wubloaderTime) { return new Date(`${wubloaderTime}Z`); } -function getWubloaderTimeFromDate(date) { - if (!date) { - return null; - } - return date.toISOString().substring(0, 19); // Trim milliseconds and "Z" marker -} - -function getWubloaderTimeFromDateWithMilliseconds(date) { +function wubloaderTimeFromDateObj(date) { if (!date) { return null; } @@ -134,10 +134,10 @@ function startAndEndTimeQueryStringParts() { let queryStringParts = []; if (startTime) { - queryStringParts.push(`start=${getWubloaderTimeFromDate(startTime)}`); + queryStringParts.push(`start=${wubloaderTimeFromDateObj(startTime)}`); } if (endTime) { - queryStringParts.push(`end=${getWubloaderTimeFromDate(endTime)}`); + queryStringParts.push(`end=${wubloaderTimeFromDateObj(endTime)}`); } return queryStringParts; } diff --git a/thrimbletrimmer/scripts/edit.js b/thrimbletrimmer/scripts/edit.js index cf71825..4942c2e 100644 --- a/thrimbletrimmer/scripts/edit.js +++ b/thrimbletrimmer/scripts/edit.js @@ -17,7 +17,7 @@ window.addEventListener("DOMContentLoaded", async (event) => { } const newStartField = document.getElementById("stream-time-setting-start"); - const newStart = dateObjFromInputTime(newStartField.value); + const newStart = dateObjFromBusTime(newStartField.value); if (!newStart) { addError("Failed to parse start time"); return; @@ -26,7 +26,7 @@ window.addEventListener("DOMContentLoaded", async (event) => { const newEndField = document.getElementById("stream-time-setting-end"); let newEnd = null; if (newEndField.value !== "") { - newEnd = dateObjFromInputTime(newEndField.value); + newEnd = dateObjFromBusTime(newEndField.value); if (!newEnd) { addError("Failed to parse end time"); return; @@ -54,8 +54,8 @@ window.addEventListener("DOMContentLoaded", async (event) => { } } - globalStartTimeString = getWubloaderTimeFromDateWithMilliseconds(newStart); - globalEndTimeString = getWubloaderTimeFromDateWithMilliseconds(newEnd); + globalStartTimeString = wubloaderTimeFromDateObj(newStart); + globalEndTimeString = wubloaderTimeFromDateObj(newEnd); updateSegmentPlaylist(); @@ -98,31 +98,25 @@ window.addEventListener("DOMContentLoaded", async (event) => { document.getElementById("stream-time-setting-start-pad").addEventListener("click", (_event) => { const startTimeField = document.getElementById("stream-time-setting-start"); let startTime = startTimeField.value; - startTime = parseInputTimeAsNumberOfSeconds(startTime); + startTime = dateObjFromBusTime(startTime); if (isNaN(startTime)) { addError("Couldn't parse entered start time for padding"); return; } - startTime -= 60; - const startTimeDate = new Date(globalBusStartTime); - startTimeDate.setSeconds(startTimeDate.getSeconds() + startTime); - startTime = getBusTimeFromDateObj(startTimeDate); - startTimeField.value = startTime; + startTime.setMinutes(startTime.getMinutes() - 1); + startTimeField.value = busTimeFromDateObj(startTime); }); document.getElementById("stream-time-setting-end-pad").addEventListener("click", (_event) => { const endTimeField = document.getElementById("stream-time-setting-end"); let endTime = endTimeField.value; - endTime = parseInputTimeAsNumberOfSeconds(endTime); + endTime = dateObjFromBusTime(endTime); if (isNaN(endTime)) { addError("Couldn't parse entered end time for padding"); return; } - endTime += 60; - const endTimeDate = new Date(globalBusStartTime); - endTimeDate.setSeconds(endTimeDate.getSeconds() + endTime); - endTime = getBusTimeFromDateObj(endTimeDate); - endTimeField.value = endTime; + endTime.setMinutes(endTime.getMinutes() + 1); + endTimeField.value = busTimeFromDateObj(endTime); }); const addRangeIcon = document.getElementById("add-range-definition"); @@ -245,9 +239,9 @@ async function initializeVideoInfo() { eventEndTime.setMinutes(eventEndTime.getMinutes() + 2); } - globalStartTimeString = getWubloaderTimeFromDateWithMilliseconds(eventStartTime); + globalStartTimeString = wubloaderTimeFromDateObj(eventStartTime); if (eventEndTime) { - globalEndTimeString = getWubloaderTimeFromDateWithMilliseconds(eventEndTime); + globalEndTimeString = wubloaderTimeFromDateObj(eventEndTime); } else { document.getElementById("waveform").classList.add("hidden"); } @@ -282,13 +276,13 @@ async function initializeVideoInfo() { if (earliestStartTime && earliestStartTime < eventStartTime) { earliestStartTime.setMinutes(earliestStartTime.getMinutes() - 1); - globalStartTimeString = getWubloaderTimeFromDateWithMilliseconds(earliestStartTime); + globalStartTimeString = wubloaderTimeFromDateObj(earliestStartTime); } if (latestEndTime && latestEndTime > eventEndTime) { // If we're getting the time from a previous draft edit, we have seconds, so one minute is enough latestEndTime.setMinutes(latestEndTime.getMinutes() + 1); - globalEndTimeString = getWubloaderTimeFromDateWithMilliseconds(latestEndTime); + globalEndTimeString = wubloaderTimeFromDateObj(latestEndTime); } } @@ -466,10 +460,10 @@ function getBusTimeFromTimeString(timeString) { return ""; } const time = dateObjFromWubloaderTime(timeString); - return getBusTimeFromDateObj(time); + return busTimeFromDateObj(time); } -function getBusTimeFromDateObj(time) { +function busTimeFromDateObj(time) { const busTimeMilliseconds = time - globalBusStartTime; let remainingBusTimeSeconds = busTimeMilliseconds / 1000; @@ -638,11 +632,11 @@ function generateDownloadURL(timeRanges, downloadType, allowHoles, quality) { for (const range of timeRanges) { let timeRangeString = ""; if (range.hasOwnProperty("start")) { - timeRangeString += getWubloaderTimeFromDateWithMilliseconds(range.start); + timeRangeString += wubloaderTimeFromDateObj(range.start); } timeRangeString += ","; if (range.hasOwnProperty("end")) { - timeRangeString += getWubloaderTimeFromDateWithMilliseconds(range.end); + timeRangeString += wubloaderTimeFromDateObj(range.end); } queryParts.push(`range=${timeRangeString}`); } @@ -1057,14 +1051,6 @@ function wubloaderTimeFromVideoHumanTime(videoHumanTime) { return wubloaderTimeFromVideoPlayerTime(videoPlayerTime); } -function dateObjFromInputTime(inputTime) { - const inputSeconds = parseInputTimeAsNumberOfSeconds(inputTime); - if (isNaN(inputSeconds)) { - return null; - } - return new Date(globalBusStartTime.getTime() + 1000 * inputSeconds); -} - function getPlaylistData() { const player = getVideoJS(); // Currently, this only supports a single playlist. We only give one playlist (or master playlist file) to VideoJS, diff --git a/thrimbletrimmer/scripts/stream.js b/thrimbletrimmer/scripts/stream.js index c3715d5..40fdda6 100644 --- a/thrimbletrimmer/scripts/stream.js +++ b/thrimbletrimmer/scripts/stream.js @@ -38,9 +38,7 @@ function getStartTime() { case 1: return dateObjFromWubloaderTime(globalStartTimeString); case 2: - return new Date( - globalBusStartTime.getTime() + 1000 * parseInputTimeAsNumberOfSeconds(globalStartTimeString) - ); + return dateObjFromBusTime(globalStartTimeString); case 3: return new Date( new Date().getTime() - 1000 * parseInputTimeAsNumberOfSeconds(globalStartTimeString) @@ -57,9 +55,7 @@ function getEndTime() { case 1: return dateObjFromWubloaderTime(globalEndTimeString); case 2: - return new Date( - globalBusStartTime.getTime() + 1000 * parseInputTimeAsNumberOfSeconds(globalEndTimeString) - ); + return dateObjFromBusTime(globalEndTimeString); case 3: return new Date( new Date().getTime() - 1000 * parseInputTimeAsNumberOfSeconds(globalEndTimeString) @@ -67,6 +63,19 @@ function getEndTime() { } } +function parseInputTimeAsNumberOfSeconds(inputTime) { + // We need to handle inputs like "-0:10:15" in a way that consistently makes the time negative. + // Since we can't assign the negative sign to any particular part, we'll check for the whole thing here. + let direction = 1; + if (inputTime.startsWith("-")) { + inputTime = inputTime.slice(1); + direction = -1; + } + + const parts = inputTime.split(":", 3); + return (parseInt(parts[0]) + (parts[1] || 0) / 60 + (parts[2] || 0) / 3600) * 60 * 60 * direction; +} + function updateTimeSettings() { updateStoredTimeSettings(); if (globalLoadedVideoPlayer) { @@ -88,8 +97,8 @@ function updateTimeSettings() { } function generateDownloadURL(startTime, endTime, downloadType, allowHoles, quality) { - const startURLTime = getWubloaderTimeFromDate(startTime); - const endURLTime = getWubloaderTimeFromDate(endTime); + const startURLTime = wubloaderTimeFromDateObj(startTime); + const endURLTime = wubloaderTimeFromDateObj(endTime); const queryParts = [`type=${downloadType}`, `allow_holes=${allowHoles}`]; if (startURLTime) {