From 5290e28ba22a8a6123072388c4dae2d926692d56 Mon Sep 17 00:00:00 2001 From: tomgond Date: Wed, 3 Apr 2024 22:39:33 +0300 Subject: [PATCH 1/7] Add Time Difference to catagories.json under Date/Time --- src/core/config/Categories.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index e483c747..81225aed 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -115,8 +115,8 @@ "ROT8000", "XOR", "XOR Brute Force", - "Vigenère Encode", - "Vigenère Decode", + "Vigenère Encode", + "Vigenère Decode", "To Morse Code", "From Morse Code", "Bacon Cipher Encode", @@ -323,6 +323,7 @@ "Windows Filetime to UNIX Timestamp", "UNIX Timestamp to Windows Filetime", "DateTime Delta", + "Time Difference", "Extract dates", "Get Time", "Sleep" From 39df81acb53860e3e5c0a44ed9314f718a01f5bd Mon Sep 17 00:00:00 2001 From: tomgond Date: Wed, 3 Apr 2024 22:41:02 +0300 Subject: [PATCH 2/7] Add DATE_DELIM_OPTIONS We do not want ":" since it's common in time strings. Same goes for space. --- src/core/lib/Delim.mjs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/core/lib/Delim.mjs b/src/core/lib/Delim.mjs index c8cc637b..08be9d4f 100644 --- a/src/core/lib/Delim.mjs +++ b/src/core/lib/Delim.mjs @@ -46,6 +46,12 @@ export const HASH_DELIM_OPTIONS = ["Line feed", "CRLF", "Space", "Comma"]; */ export const IP_DELIM_OPTIONS = ["Line feed", "CRLF", "Space", "Comma", "Semi-colon"]; +/** + * Date delimiters + */ +export const DATE_DELIM_OPTIONS = ["Line feed", "CRLF", "Comma", "Semi-colon"]; + + /** * Split delimiters. */ From dbcce2e0afdc0df988012ab92045a3b689ec258e Mon Sep 17 00:00:00 2001 From: tomgond Date: Wed, 3 Apr 2024 22:43:15 +0300 Subject: [PATCH 3/7] Add TimeDifference Operation Allow to calculate the time between two input DateTimes (with delimiter between them). trailing newlines are ignored. Error is raised when needed. Output can be positive or negative depending on the order of the input. --- src/core/operations/TimeDifference.mjs | 76 ++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/core/operations/TimeDifference.mjs diff --git a/src/core/operations/TimeDifference.mjs b/src/core/operations/TimeDifference.mjs new file mode 100644 index 00000000..f8efd8ad --- /dev/null +++ b/src/core/operations/TimeDifference.mjs @@ -0,0 +1,76 @@ +/** + * @author tomgond [tom.gonda@gmail.com] + * @copyright Crown Copyright 2024 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; +import moment from "moment-timezone"; +import Utils from "../Utils.mjs"; +import {DATETIME_FORMATS} from "../lib/DateTime.mjs"; +import {DATE_DELIM_OPTIONS} from "../lib/Delim.mjs"; +import OperationError from "../errors/OperationError.mjs"; + +/** + * Calculate Time Difference operation + */ +class TimeDifference extends Operation { + + /** + * TimeDifference Constructor + */ + constructor() { + super(); + + this.name = "Time Difference"; + this.module = "DateTime"; + this.description = "Calculates the difference between two datetime values based on the provided format."; + this.inputType = "string"; + this.outputType = "string"; + this.args = [ + { + "name": "Built in formats", + "type": "populateOption", + "value": DATETIME_FORMATS, + "target": 1 + }, + { + "name": "Input format string", + "type": "binaryString", + "value": "DD/MM/YYYY HH:mm:ss" + }, + { + "name": "Delimiter", + "type": "option", + "value": DATE_DELIM_OPTIONS, + } + ]; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + const dateFormat = args[1]; + const delimiter = Utils.charRep(args[2]); + input = input.replace(/[\r\n]+$/, ""); + const dates = input.split(delimiter); + if (dates.length !== 2) { + throw new OperationError("Error: Input should contain exactly two datetime values separated by a delimiter."); + } + + const date1 = moment(dates[0], dateFormat); + const date2 = moment(dates[1], dateFormat); + + if (!date1.isValid() || !date2.isValid()) { + throw new OperationError("Error: One of the provided dates is invalid."); + } + + const diff = moment.duration(date2.diff(date1)); + return `Years:${diff.years()} Months:${diff.months()} Days:${diff.days()} Hours:${diff.hours()} Minutes:${diff.minutes()} Seconds:${diff.seconds()}`; + } +} + +export default TimeDifference; From 73124da19cae153bc4ba6459436263489ed379ab Mon Sep 17 00:00:00 2001 From: tomgond Date: Wed, 3 Apr 2024 22:44:01 +0300 Subject: [PATCH 4/7] Added test for timedifference operation. --- tests/operations/tests/DateTime.mjs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/operations/tests/DateTime.mjs b/tests/operations/tests/DateTime.mjs index 16848bcc..480b1d17 100644 --- a/tests/operations/tests/DateTime.mjs +++ b/tests/operations/tests/DateTime.mjs @@ -53,4 +53,26 @@ TestRegister.addTests([ }, ], }, + { + name: "Time Difference Positive", + input: "03/05/2024 13:03:22;03/06/2025 13:14:50", + expectedOutput: "Years:1 Months:1 Days:0 Hours:0 Minutes:11 Seconds:28", + recipeConfig: [ + { + op: "Time Difference", + args: ["Standard date and time", "DD/MM/YYYY HH:mm:ss", "Semi-colon"], + }, + ], + }, + { + name: "Time Difference Negative", + input: "03/05/2024 13:03:22,03/05/2024 11:30:15", + expectedOutput: "Years:0 Months:0 Days:0 Hours:-1 Minutes:-33 Seconds:-7", + recipeConfig: [ + { + op: "Time Difference", + args: ["Standard date and time", "DD/MM/YYYY HH:mm:ss", "Comma"], + }, + ], + }, ]); From 98e4a5d2fa98337dc7d47fc7fbc7de0fe5540561 Mon Sep 17 00:00:00 2001 From: tomgond Date: Wed, 3 Apr 2024 22:50:31 +0300 Subject: [PATCH 5/7] Fix category names MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "Vigenère Encode", "Vigenère Decode", --- src/core/config/Categories.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 81225aed..e81d8ca1 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -115,8 +115,8 @@ "ROT8000", "XOR", "XOR Brute Force", - "Vigenère Encode", - "Vigenère Decode", + "Vigenère Encode", + "Vigenère Decode", "To Morse Code", "From Morse Code", "Bacon Cipher Encode", From c01118f6a5ef8f93c24880979c67eb59657a59d8 Mon Sep 17 00:00:00 2001 From: tomgond Date: Mon, 10 Jun 2024 11:10:16 +0300 Subject: [PATCH 6/7] Change to make pull request run again --- src/core/operations/TimeDifference.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/operations/TimeDifference.mjs b/src/core/operations/TimeDifference.mjs index f8efd8ad..7da2e3a0 100644 --- a/src/core/operations/TimeDifference.mjs +++ b/src/core/operations/TimeDifference.mjs @@ -1,6 +1,6 @@ /** * @author tomgond [tom.gonda@gmail.com] - * @copyright Crown Copyright 2024 + * @copyright Crown Copyright 2024 * @license Apache-2.0 */ From 37aed94440b4936411271aa8776b1e54d9278fc8 Mon Sep 17 00:00:00 2001 From: tomgond Date: Sun, 8 Sep 2024 22:28:02 +0300 Subject: [PATCH 7/7] Update TimeDifference.mjs Another change to make build run --- src/core/operations/TimeDifference.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/operations/TimeDifference.mjs b/src/core/operations/TimeDifference.mjs index 7da2e3a0..f8efd8ad 100644 --- a/src/core/operations/TimeDifference.mjs +++ b/src/core/operations/TimeDifference.mjs @@ -1,6 +1,6 @@ /** * @author tomgond [tom.gonda@gmail.com] - * @copyright Crown Copyright 2024 + * @copyright Crown Copyright 2024 * @license Apache-2.0 */