Do a general cleanup of time conversion names to make times overall consistent

pull/239/head
ElementalAlchemist 3 years ago committed by Mike Lang
parent 7fdc262822
commit cc49e703dc

@ -87,31 +87,31 @@ function updateVideoPlayer(newPlaylistURL) {
player.src({ src: rangedPlaylistURL }); 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. // 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. // Since we can't assign the negative sign to any particular part, we'll check for the whole thing here.
let direction = 1; let direction = 1;
if (inputTime.startsWith("-")) { if (busTime.startsWith("-")) {
inputTime = inputTime.slice(1); busTime = busTime.slice(1);
direction = -1; direction = -1;
} }
const parts = inputTime.split(":", 3); const parts = busTime.split(":", 3);
return (parseInt(parts[0]) + (parts[1] || 0) / 60 + (parts[2] || 0) / 3600) * 60 * 60 * direction; 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) { function dateObjFromWubloaderTime(wubloaderTime) {
return new Date(`${wubloaderTime}Z`); return new Date(`${wubloaderTime}Z`);
} }
function getWubloaderTimeFromDate(date) { function wubloaderTimeFromDateObj(date) {
if (!date) {
return null;
}
return date.toISOString().substring(0, 19); // Trim milliseconds and "Z" marker
}
function getWubloaderTimeFromDateWithMilliseconds(date) {
if (!date) { if (!date) {
return null; return null;
} }
@ -134,10 +134,10 @@ function startAndEndTimeQueryStringParts() {
let queryStringParts = []; let queryStringParts = [];
if (startTime) { if (startTime) {
queryStringParts.push(`start=${getWubloaderTimeFromDate(startTime)}`); queryStringParts.push(`start=${wubloaderTimeFromDateObj(startTime)}`);
} }
if (endTime) { if (endTime) {
queryStringParts.push(`end=${getWubloaderTimeFromDate(endTime)}`); queryStringParts.push(`end=${wubloaderTimeFromDateObj(endTime)}`);
} }
return queryStringParts; return queryStringParts;
} }

@ -17,7 +17,7 @@ window.addEventListener("DOMContentLoaded", async (event) => {
} }
const newStartField = document.getElementById("stream-time-setting-start"); const newStartField = document.getElementById("stream-time-setting-start");
const newStart = dateObjFromInputTime(newStartField.value); const newStart = dateObjFromBusTime(newStartField.value);
if (!newStart) { if (!newStart) {
addError("Failed to parse start time"); addError("Failed to parse start time");
return; return;
@ -26,7 +26,7 @@ window.addEventListener("DOMContentLoaded", async (event) => {
const newEndField = document.getElementById("stream-time-setting-end"); const newEndField = document.getElementById("stream-time-setting-end");
let newEnd = null; let newEnd = null;
if (newEndField.value !== "") { if (newEndField.value !== "") {
newEnd = dateObjFromInputTime(newEndField.value); newEnd = dateObjFromBusTime(newEndField.value);
if (!newEnd) { if (!newEnd) {
addError("Failed to parse end time"); addError("Failed to parse end time");
return; return;
@ -54,8 +54,8 @@ window.addEventListener("DOMContentLoaded", async (event) => {
} }
} }
globalStartTimeString = getWubloaderTimeFromDateWithMilliseconds(newStart); globalStartTimeString = wubloaderTimeFromDateObj(newStart);
globalEndTimeString = getWubloaderTimeFromDateWithMilliseconds(newEnd); globalEndTimeString = wubloaderTimeFromDateObj(newEnd);
updateSegmentPlaylist(); updateSegmentPlaylist();
@ -98,31 +98,25 @@ window.addEventListener("DOMContentLoaded", async (event) => {
document.getElementById("stream-time-setting-start-pad").addEventListener("click", (_event) => { document.getElementById("stream-time-setting-start-pad").addEventListener("click", (_event) => {
const startTimeField = document.getElementById("stream-time-setting-start"); const startTimeField = document.getElementById("stream-time-setting-start");
let startTime = startTimeField.value; let startTime = startTimeField.value;
startTime = parseInputTimeAsNumberOfSeconds(startTime); startTime = dateObjFromBusTime(startTime);
if (isNaN(startTime)) { if (isNaN(startTime)) {
addError("Couldn't parse entered start time for padding"); addError("Couldn't parse entered start time for padding");
return; return;
} }
startTime -= 60; startTime.setMinutes(startTime.getMinutes() - 1);
const startTimeDate = new Date(globalBusStartTime); startTimeField.value = busTimeFromDateObj(startTime);
startTimeDate.setSeconds(startTimeDate.getSeconds() + startTime);
startTime = getBusTimeFromDateObj(startTimeDate);
startTimeField.value = startTime;
}); });
document.getElementById("stream-time-setting-end-pad").addEventListener("click", (_event) => { document.getElementById("stream-time-setting-end-pad").addEventListener("click", (_event) => {
const endTimeField = document.getElementById("stream-time-setting-end"); const endTimeField = document.getElementById("stream-time-setting-end");
let endTime = endTimeField.value; let endTime = endTimeField.value;
endTime = parseInputTimeAsNumberOfSeconds(endTime); endTime = dateObjFromBusTime(endTime);
if (isNaN(endTime)) { if (isNaN(endTime)) {
addError("Couldn't parse entered end time for padding"); addError("Couldn't parse entered end time for padding");
return; return;
} }
endTime += 60; endTime.setMinutes(endTime.getMinutes() + 1);
const endTimeDate = new Date(globalBusStartTime); endTimeField.value = busTimeFromDateObj(endTime);
endTimeDate.setSeconds(endTimeDate.getSeconds() + endTime);
endTime = getBusTimeFromDateObj(endTimeDate);
endTimeField.value = endTime;
}); });
const addRangeIcon = document.getElementById("add-range-definition"); const addRangeIcon = document.getElementById("add-range-definition");
@ -245,9 +239,9 @@ async function initializeVideoInfo() {
eventEndTime.setMinutes(eventEndTime.getMinutes() + 2); eventEndTime.setMinutes(eventEndTime.getMinutes() + 2);
} }
globalStartTimeString = getWubloaderTimeFromDateWithMilliseconds(eventStartTime); globalStartTimeString = wubloaderTimeFromDateObj(eventStartTime);
if (eventEndTime) { if (eventEndTime) {
globalEndTimeString = getWubloaderTimeFromDateWithMilliseconds(eventEndTime); globalEndTimeString = wubloaderTimeFromDateObj(eventEndTime);
} else { } else {
document.getElementById("waveform").classList.add("hidden"); document.getElementById("waveform").classList.add("hidden");
} }
@ -282,13 +276,13 @@ async function initializeVideoInfo() {
if (earliestStartTime && earliestStartTime < eventStartTime) { if (earliestStartTime && earliestStartTime < eventStartTime) {
earliestStartTime.setMinutes(earliestStartTime.getMinutes() - 1); earliestStartTime.setMinutes(earliestStartTime.getMinutes() - 1);
globalStartTimeString = getWubloaderTimeFromDateWithMilliseconds(earliestStartTime); globalStartTimeString = wubloaderTimeFromDateObj(earliestStartTime);
} }
if (latestEndTime && latestEndTime > eventEndTime) { if (latestEndTime && latestEndTime > eventEndTime) {
// If we're getting the time from a previous draft edit, we have seconds, so one minute is enough // If we're getting the time from a previous draft edit, we have seconds, so one minute is enough
latestEndTime.setMinutes(latestEndTime.getMinutes() + 1); latestEndTime.setMinutes(latestEndTime.getMinutes() + 1);
globalEndTimeString = getWubloaderTimeFromDateWithMilliseconds(latestEndTime); globalEndTimeString = wubloaderTimeFromDateObj(latestEndTime);
} }
} }
@ -466,10 +460,10 @@ function getBusTimeFromTimeString(timeString) {
return ""; return "";
} }
const time = dateObjFromWubloaderTime(timeString); const time = dateObjFromWubloaderTime(timeString);
return getBusTimeFromDateObj(time); return busTimeFromDateObj(time);
} }
function getBusTimeFromDateObj(time) { function busTimeFromDateObj(time) {
const busTimeMilliseconds = time - globalBusStartTime; const busTimeMilliseconds = time - globalBusStartTime;
let remainingBusTimeSeconds = busTimeMilliseconds / 1000; let remainingBusTimeSeconds = busTimeMilliseconds / 1000;
@ -638,11 +632,11 @@ function generateDownloadURL(timeRanges, downloadType, allowHoles, quality) {
for (const range of timeRanges) { for (const range of timeRanges) {
let timeRangeString = ""; let timeRangeString = "";
if (range.hasOwnProperty("start")) { if (range.hasOwnProperty("start")) {
timeRangeString += getWubloaderTimeFromDateWithMilliseconds(range.start); timeRangeString += wubloaderTimeFromDateObj(range.start);
} }
timeRangeString += ","; timeRangeString += ",";
if (range.hasOwnProperty("end")) { if (range.hasOwnProperty("end")) {
timeRangeString += getWubloaderTimeFromDateWithMilliseconds(range.end); timeRangeString += wubloaderTimeFromDateObj(range.end);
} }
queryParts.push(`range=${timeRangeString}`); queryParts.push(`range=${timeRangeString}`);
} }
@ -1057,14 +1051,6 @@ function wubloaderTimeFromVideoHumanTime(videoHumanTime) {
return wubloaderTimeFromVideoPlayerTime(videoPlayerTime); return wubloaderTimeFromVideoPlayerTime(videoPlayerTime);
} }
function dateObjFromInputTime(inputTime) {
const inputSeconds = parseInputTimeAsNumberOfSeconds(inputTime);
if (isNaN(inputSeconds)) {
return null;
}
return new Date(globalBusStartTime.getTime() + 1000 * inputSeconds);
}
function getPlaylistData() { function getPlaylistData() {
const player = getVideoJS(); const player = getVideoJS();
// Currently, this only supports a single playlist. We only give one playlist (or master playlist file) to VideoJS, // Currently, this only supports a single playlist. We only give one playlist (or master playlist file) to VideoJS,

@ -38,9 +38,7 @@ function getStartTime() {
case 1: case 1:
return dateObjFromWubloaderTime(globalStartTimeString); return dateObjFromWubloaderTime(globalStartTimeString);
case 2: case 2:
return new Date( return dateObjFromBusTime(globalStartTimeString);
globalBusStartTime.getTime() + 1000 * parseInputTimeAsNumberOfSeconds(globalStartTimeString)
);
case 3: case 3:
return new Date( return new Date(
new Date().getTime() - 1000 * parseInputTimeAsNumberOfSeconds(globalStartTimeString) new Date().getTime() - 1000 * parseInputTimeAsNumberOfSeconds(globalStartTimeString)
@ -57,9 +55,7 @@ function getEndTime() {
case 1: case 1:
return dateObjFromWubloaderTime(globalEndTimeString); return dateObjFromWubloaderTime(globalEndTimeString);
case 2: case 2:
return new Date( return dateObjFromBusTime(globalEndTimeString);
globalBusStartTime.getTime() + 1000 * parseInputTimeAsNumberOfSeconds(globalEndTimeString)
);
case 3: case 3:
return new Date( return new Date(
new Date().getTime() - 1000 * parseInputTimeAsNumberOfSeconds(globalEndTimeString) 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() { function updateTimeSettings() {
updateStoredTimeSettings(); updateStoredTimeSettings();
if (globalLoadedVideoPlayer) { if (globalLoadedVideoPlayer) {
@ -88,8 +97,8 @@ function updateTimeSettings() {
} }
function generateDownloadURL(startTime, endTime, downloadType, allowHoles, quality) { function generateDownloadURL(startTime, endTime, downloadType, allowHoles, quality) {
const startURLTime = getWubloaderTimeFromDate(startTime); const startURLTime = wubloaderTimeFromDateObj(startTime);
const endURLTime = getWubloaderTimeFromDate(endTime); const endURLTime = wubloaderTimeFromDateObj(endTime);
const queryParts = [`type=${downloadType}`, `allow_holes=${allowHoles}`]; const queryParts = [`type=${downloadType}`, `allow_holes=${allowHoles}`];
if (startURLTime) { if (startURLTime) {

Loading…
Cancel
Save