mirror of
https://github.com/gchq/CyberChef
synced 2025-01-21 00:43:53 +00:00
Change time-delta argument to be per time unit
Day, hour, minute, second. Instead of a single string.
This commit is contained in:
parent
d59ebdd0dc
commit
56f92afbf4
1 changed files with 41 additions and 42 deletions
|
@ -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, ""));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue