From d2ff03cea49e37a71b7d1e90eeeac60625eb7241 Mon Sep 17 00:00:00 2001 From: tomgond Date: Wed, 21 Feb 2024 19:55:09 +0200 Subject: [PATCH 1/8] Update DateTime.mjs Add test for time-delta --- 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 6276890d0..df6d66770 100644 --- a/tests/operations/tests/DateTime.mjs +++ b/tests/operations/tests/DateTime.mjs @@ -31,4 +31,26 @@ TestRegister.addTests([ }, ], }, + { + name: "DateTime Delta Positive", + input: "20/02/2024 13:36:00", + expectedOutput: "20/02/2024 13:37:00", + recipeConfig: [ + { + op: "DateTime Delta", + args: ["Standard date and time", "DD/MM/YYYY HH:mm:ss", "+0.0:01:0"], + }, + ], + }, + { + name: "DateTime Delta Negative", + input: "20/02/2024 14:37:00", + expectedOutput: "20/02/2024 13:37:00", + recipeConfig: [ + { + op: "DateTime Delta", + args: ["Standard date and time", "DD/MM/YYYY HH:mm:ss", "-0.1:00:0"], + }, + ], + }, ]); From 4dc4c7edd2a388792a5915d3abf2b4969620e17c Mon Sep 17 00:00:00 2001 From: tomgond Date: Wed, 21 Feb 2024 19:56:42 +0200 Subject: [PATCH 2/8] Update Categories.json Add DateTime Delta to categories --- src/core/config/Categories.json | 1 + 1 file changed, 1 insertion(+) diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 4f1b3328b..d68c1713d 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -312,6 +312,7 @@ "To UNIX Timestamp", "Windows Filetime to UNIX Timestamp", "UNIX Timestamp to Windows Filetime", + "DateTime Delta", "Extract dates", "Get Time", "Sleep" From 6331c203064563c628fe38f58cffb2206ce97afa Mon Sep 17 00:00:00 2001 From: tomgond Date: Wed, 21 Feb 2024 19:58:13 +0200 Subject: [PATCH 3/8] Add code for DateTime Delta to calculate operation --- src/core/operations/DateTimeDelta.mjs | 109 ++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/core/operations/DateTimeDelta.mjs diff --git a/src/core/operations/DateTimeDelta.mjs b/src/core/operations/DateTimeDelta.mjs new file mode 100644 index 000000000..6a7b8f7a1 --- /dev/null +++ b/src/core/operations/DateTimeDelta.mjs @@ -0,0 +1,109 @@ +/** + * @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 {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 + */ +class DateTimeDelta extends Operation { + + /** + * DateTimeDelta constructor + */ + constructor() { + super(); + + this.name = "DateTime Delta"; + this.module = "Default"; + this.description = "Calculates a new DateTime value given an input DateTime value and a time difference (delta) from the input DateTime value."; + this.infoURL = ""; + 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": "Time Delta", + "type": "binaryString", + "value": "+0.00:00:00" + } + + ]; + } + + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + const inputFormat = args[1], + inputTimezone = "UTC"; + const deltaStr = args[2]; + + let date = ""; + try { + date = moment.tz(input, inputFormat, inputTimezone); + if (!date || date.format() === "Invalid date") throw Error; + } catch (err) { + return `Invalid format.\n\n${FORMAT_EXAMPLES}`; + } + + const deltaDict = parseTimeString(deltaStr); + let newDate; + if (deltaDict.sign === "-") { + newDate = date.add(-deltaDict.days, "days") + .add(-deltaDict.hours, "hours") + .add(-deltaDict.minutes, "minutes") + .add(-deltaDict.seconds, "seconds"); + + } else { + newDate = date.add(deltaDict.days, "days") + .add(deltaDict.hours, "hours") + .add(deltaDict.minutes, "minutes") + .add(deltaDict.seconds, "seconds"); + } + return newDate.tz(inputTimezone).format(inputFormat.replace(/[<>]/g, "")); + } +} + +export default DateTimeDelta; From 4e9567f5396d1d700cb204655c6793bdaf2221ff Mon Sep 17 00:00:00 2001 From: tomgond Date: Thu, 22 Feb 2024 21:49:24 +0200 Subject: [PATCH 4/8] Update DateTimeDelta.mjs Some change to re-run tests. --- src/core/operations/DateTimeDelta.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/operations/DateTimeDelta.mjs b/src/core/operations/DateTimeDelta.mjs index 6a7b8f7a1..585ee3703 100644 --- a/src/core/operations/DateTimeDelta.mjs +++ b/src/core/operations/DateTimeDelta.mjs @@ -10,7 +10,7 @@ import {DATETIME_FORMATS, FORMAT_EXAMPLES} from "../lib/DateTime.mjs"; /** * @param {string} timeString - * @returns {string} + * @returns {string} */ function parseTimeString(timeString) { // Split the string into its components From e85acee509757df7bc750ad99ce8a8aca089d182 Mon Sep 17 00:00:00 2001 From: tomgond Date: Fri, 23 Feb 2024 07:09:04 +0200 Subject: [PATCH 5/8] Update DateTimeDelta.mjs Another commit for re-build --- src/core/operations/DateTimeDelta.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/operations/DateTimeDelta.mjs b/src/core/operations/DateTimeDelta.mjs index 585ee3703..66949aced 100644 --- a/src/core/operations/DateTimeDelta.mjs +++ b/src/core/operations/DateTimeDelta.mjs @@ -32,7 +32,7 @@ function parseTimeString(timeString) { } /** - * DateTime Delta operation + * DateTime Delta operation */ class DateTimeDelta extends Operation { From 56f92afbf4b15b27ac6f20a1e3621841ed838f55 Mon Sep 17 00:00:00 2001 From: tomgond Date: Mon, 1 Apr 2024 19:41:44 +0300 Subject: [PATCH 6/8] Change time-delta argument to be per time unit Day, hour, minute, second. Instead of a single string. --- src/core/operations/DateTimeDelta.mjs | 83 +++++++++++++-------------- 1 file changed, 41 insertions(+), 42 deletions(-) diff --git a/src/core/operations/DateTimeDelta.mjs b/src/core/operations/DateTimeDelta.mjs index 66949aced..c923374d6 100644 --- a/src/core/operations/DateTimeDelta.mjs +++ b/src/core/operations/DateTimeDelta.mjs @@ -9,30 +9,7 @@ import moment from "moment-timezone"; 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 */ class DateTimeDelta extends Operation { @@ -61,9 +38,29 @@ class DateTimeDelta extends Operation { "value": "DD/MM/YYYY HH:mm:ss" }, { - "name": "Time Delta", - "type": "binaryString", - "value": "+0.00:00:00" + "name": "Time Operation", + "type": "option", + "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} */ run(input, args) { - const inputFormat = args[1], - inputTimezone = "UTC"; - const deltaStr = args[2]; - + const inputTimezone = "UTC"; + const inputFormat = args[1]; + const operationType = args[2]; + const daysDelta = args[3]; + const hoursDelta = args[4]; + const minutesDelta = args[5]; + const secondsDelta = args[6]; let date = ""; + try { date = moment.tz(input, inputFormat, inputTimezone); if (!date || date.format() === "Invalid date") throw Error; } catch (err) { return `Invalid format.\n\n${FORMAT_EXAMPLES}`; } - - const deltaDict = parseTimeString(deltaStr); let newDate; - if (deltaDict.sign === "-") { - newDate = date.add(-deltaDict.days, "days") - .add(-deltaDict.hours, "hours") - .add(-deltaDict.minutes, "minutes") - .add(-deltaDict.seconds, "seconds"); + if (operationType === "Add") { + newDate = date.add(daysDelta, "days") + .add(hoursDelta, "hours") + .add(minutesDelta, "minutes") + .add(secondsDelta, "seconds"); } else { - newDate = date.add(deltaDict.days, "days") - .add(deltaDict.hours, "hours") - .add(deltaDict.minutes, "minutes") - .add(deltaDict.seconds, "seconds"); + newDate = date.add(-daysDelta, "days") + .add(-hoursDelta, "hours") + .add(-minutesDelta, "minutes") + .add(-secondsDelta, "seconds"); } return newDate.tz(inputTimezone).format(inputFormat.replace(/[<>]/g, "")); } From dfedfa9f4cab634d7c332580e133116ab33a2c48 Mon Sep 17 00:00:00 2001 From: tomgond Date: Mon, 1 Apr 2024 19:42:56 +0300 Subject: [PATCH 7/8] Fix test to fit new time-delta format --- tests/operations/tests/DateTime.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/operations/tests/DateTime.mjs b/tests/operations/tests/DateTime.mjs index df6d66770..16848bcc4 100644 --- a/tests/operations/tests/DateTime.mjs +++ b/tests/operations/tests/DateTime.mjs @@ -38,7 +38,7 @@ TestRegister.addTests([ recipeConfig: [ { op: "DateTime Delta", - args: ["Standard date and time", "DD/MM/YYYY HH:mm:ss", "+0.0:01:0"], + args: ["Standard date and time", "DD/MM/YYYY HH:mm:ss", "Add", 0, 0, 1, 0], }, ], }, @@ -49,7 +49,7 @@ TestRegister.addTests([ recipeConfig: [ { op: "DateTime Delta", - args: ["Standard date and time", "DD/MM/YYYY HH:mm:ss", "-0.1:00:0"], + args: ["Standard date and time", "DD/MM/YYYY HH:mm:ss", "Subtract", 0, 1, 0, 0], }, ], }, From c7952715028a3382420d4d2b6c9314c9f416784e Mon Sep 17 00:00:00 2001 From: a3957273 <89583054+a3957273@users.noreply.github.com> Date: Tue, 2 Apr 2024 20:27:48 +0000 Subject: [PATCH 8/8] Change output to 'html' --- src/core/operations/DateTimeDelta.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/operations/DateTimeDelta.mjs b/src/core/operations/DateTimeDelta.mjs index c923374d6..350906066 100644 --- a/src/core/operations/DateTimeDelta.mjs +++ b/src/core/operations/DateTimeDelta.mjs @@ -24,7 +24,7 @@ class DateTimeDelta extends Operation { this.description = "Calculates a new DateTime value given an input DateTime value and a time difference (delta) from the input DateTime value."; this.infoURL = ""; this.inputType = "string"; - this.outputType = "string"; + this.outputType = "html"; this.args = [ { "name": "Built in formats",