Change time-delta argument to be per time unit

Day, hour, minute, second. Instead of a single string.
This commit is contained in:
tomgond 2024-04-01 19:41:44 +03:00 committed by GitHub
parent d59ebdd0dc
commit 56f92afbf4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -8,29 +8,6 @@ import Operation from "../Operation.mjs";
import moment from "moment-timezone"; import moment from "moment-timezone";
import {DATETIME_FORMATS, FORMAT_EXAMPLES} from "../lib/DateTime.mjs"; import {DATETIME_FORMATS, FORMAT_EXAMPLES} from "../lib/DateTime.mjs";
/**
* @param {string} timeString
* @returns {string}
*/
function parseTimeString(timeString) {
// Split the string into its components
const parts = timeString.split(":");
// Extract the sign, days, hours, minutes, and seconds
const sign = parts[0][0] === "-" ? "-" : "+";
const days = parseInt(parts[0].split(".")[0].slice(1), 10);
const hours = parseInt(parts[0].split(".")[1], 10);
const minutes = parseInt(parts[1], 10);
const seconds = parseInt(parts[2], 10);
return {
sign,
days,
hours,
minutes,
seconds
};
}
/** /**
* DateTime Delta operation * DateTime Delta operation
*/ */
@ -61,9 +38,29 @@ class DateTimeDelta extends Operation {
"value": "DD/MM/YYYY HH:mm:ss" "value": "DD/MM/YYYY HH:mm:ss"
}, },
{ {
"name": "Time Delta", "name": "Time Operation",
"type": "binaryString", "type": "option",
"value": "+0.00:00:00" "value": ["Add", "Subtract"]
},
{
"name": "Days",
"type": "number",
"value": 0
},
{
"name": "Hours",
"type": "number",
"value": 0
},
{
"name": "Minutes",
"type": "number",
"value": 0
},
{
"name": "Seconds",
"type": "number",
"value": 0
} }
]; ];
@ -76,31 +73,33 @@ class DateTimeDelta extends Operation {
* @returns {string} * @returns {string}
*/ */
run(input, args) { run(input, args) {
const inputFormat = args[1], const inputTimezone = "UTC";
inputTimezone = "UTC"; const inputFormat = args[1];
const deltaStr = args[2]; const operationType = args[2];
const daysDelta = args[3];
const hoursDelta = args[4];
const minutesDelta = args[5];
const secondsDelta = args[6];
let date = ""; let date = "";
try { try {
date = moment.tz(input, inputFormat, inputTimezone); date = moment.tz(input, inputFormat, inputTimezone);
if (!date || date.format() === "Invalid date") throw Error; if (!date || date.format() === "Invalid date") throw Error;
} catch (err) { } catch (err) {
return `Invalid format.\n\n${FORMAT_EXAMPLES}`; return `Invalid format.\n\n${FORMAT_EXAMPLES}`;
} }
const deltaDict = parseTimeString(deltaStr);
let newDate; let newDate;
if (deltaDict.sign === "-") { if (operationType === "Add") {
newDate = date.add(-deltaDict.days, "days") newDate = date.add(daysDelta, "days")
.add(-deltaDict.hours, "hours") .add(hoursDelta, "hours")
.add(-deltaDict.minutes, "minutes") .add(minutesDelta, "minutes")
.add(-deltaDict.seconds, "seconds"); .add(secondsDelta, "seconds");
} else { } else {
newDate = date.add(deltaDict.days, "days") newDate = date.add(-daysDelta, "days")
.add(deltaDict.hours, "hours") .add(-hoursDelta, "hours")
.add(deltaDict.minutes, "minutes") .add(-minutesDelta, "minutes")
.add(deltaDict.seconds, "seconds"); .add(-secondsDelta, "seconds");
} }
return newDate.tz(inputTimezone).format(inputFormat.replace(/[<>]/g, "")); return newDate.tz(inputTimezone).format(inputFormat.replace(/[<>]/g, ""));
} }