From 7526f4d7b19c981affaab838ff7a6eeaaefe1894 Mon Sep 17 00:00:00 2001 From: n1073645 Date: Mon, 1 Jun 2020 13:47:51 +0100 Subject: [PATCH 1/2] Generate Epoch Time Operation Added --- src/core/config/Categories.json | 1 + src/core/operations/GenerateCurrentEpoch.mjs | 49 ++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/core/operations/GenerateCurrentEpoch.mjs diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 77e3d319..dbc003e9 100755 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -261,6 +261,7 @@ "Windows Filetime to UNIX Timestamp", "UNIX Timestamp to Windows Filetime", "Extract dates", + "Generate Current Epoch", "Sleep" ] }, diff --git a/src/core/operations/GenerateCurrentEpoch.mjs b/src/core/operations/GenerateCurrentEpoch.mjs new file mode 100644 index 00000000..a49ddc44 --- /dev/null +++ b/src/core/operations/GenerateCurrentEpoch.mjs @@ -0,0 +1,49 @@ +/** + * @author n1073645 [n1073645@gmail.com] + * @copyright Crown Copyright 2020 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; + +/** + * GenerateCurrentEpoch operation + */ +class GenerateCurrentEpoch extends Operation { + + /** + * GenerateCurrentEpoch constructor + */ + constructor() { + super(); + + this.name = "Generate Current Epoch"; + this.module = "Default"; + this.description = "Generates the current time(in seconds/milliseconds) since the UNIX epoch."; + this.infoURL = "https://wikipedia.org/wiki/Unix_time"; + this.inputType = "string"; + this.outputType = "string"; + this.args = [ + { + name: "Granularity", + type: "option", + value: ["Milliseconds", "Seconds"] + } + ]; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + if (args[0] === "Milliseconds") + return (new Date()).getTime().toString(); + else + return Math.round((new Date()).getTime() / 1000).toString(); + } + +} + +export default GenerateCurrentEpoch; From 61a1c44f26d699d8a0949c095b9e3113cf722868 Mon Sep 17 00:00:00 2001 From: n1474335 Date: Thu, 11 Feb 2021 18:47:44 +0000 Subject: [PATCH 2/2] Renamed 'Generate Current Epoch' to 'Get Time'. It now uses the W3C High Resolution Time API and supports microsecond granularity --- src/core/config/Categories.json | 2 +- src/core/operations/GenerateCurrentEpoch.mjs | 49 ---------------- src/core/operations/GetTime.mjs | 62 ++++++++++++++++++++ 3 files changed, 63 insertions(+), 50 deletions(-) delete mode 100644 src/core/operations/GenerateCurrentEpoch.mjs create mode 100644 src/core/operations/GetTime.mjs diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 25a8b46e..257f4742 100755 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -267,7 +267,7 @@ "Windows Filetime to UNIX Timestamp", "UNIX Timestamp to Windows Filetime", "Extract dates", - "Generate Current Epoch", + "Get Time", "Sleep" ] }, diff --git a/src/core/operations/GenerateCurrentEpoch.mjs b/src/core/operations/GenerateCurrentEpoch.mjs deleted file mode 100644 index a49ddc44..00000000 --- a/src/core/operations/GenerateCurrentEpoch.mjs +++ /dev/null @@ -1,49 +0,0 @@ -/** - * @author n1073645 [n1073645@gmail.com] - * @copyright Crown Copyright 2020 - * @license Apache-2.0 - */ - -import Operation from "../Operation.mjs"; - -/** - * GenerateCurrentEpoch operation - */ -class GenerateCurrentEpoch extends Operation { - - /** - * GenerateCurrentEpoch constructor - */ - constructor() { - super(); - - this.name = "Generate Current Epoch"; - this.module = "Default"; - this.description = "Generates the current time(in seconds/milliseconds) since the UNIX epoch."; - this.infoURL = "https://wikipedia.org/wiki/Unix_time"; - this.inputType = "string"; - this.outputType = "string"; - this.args = [ - { - name: "Granularity", - type: "option", - value: ["Milliseconds", "Seconds"] - } - ]; - } - - /** - * @param {string} input - * @param {Object[]} args - * @returns {string} - */ - run(input, args) { - if (args[0] === "Milliseconds") - return (new Date()).getTime().toString(); - else - return Math.round((new Date()).getTime() / 1000).toString(); - } - -} - -export default GenerateCurrentEpoch; diff --git a/src/core/operations/GetTime.mjs b/src/core/operations/GetTime.mjs new file mode 100644 index 00000000..36a0427b --- /dev/null +++ b/src/core/operations/GetTime.mjs @@ -0,0 +1,62 @@ +/** + * @author n1073645 [n1073645@gmail.com] + * @copyright Crown Copyright 2020 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; +import {UNITS} from "../lib/DateTime.mjs"; + +/** + * Get Time operation + */ +class GetTime extends Operation { + + /** + * GetTime constructor + */ + constructor() { + super(); + + this.name = "Get Time"; + this.module = "Default"; + this.description = "Generates a timestamp showing the amount of time since the UNIX epoch (1970-01-01 00:00:00 UTC). Uses the W3C High Resolution Time API."; + this.infoURL = "https://wikipedia.org/wiki/Unix_time"; + this.inputType = "string"; + this.outputType = "number"; + this.args = [ + { + name: "Granularity", + type: "option", + value: UNITS + } + ]; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {number} + */ + run(input, args) { + const nowMs = (performance.timeOrigin + performance.now()), + granularity = args[0]; + + switch (granularity) { + case "Nanoseconds (ns)": + return Math.round(nowMs * 1000 * 1000); + case "Microseconds (μs)": + return Math.round(nowMs * 1000); + case "Milliseconds (ms)": + return Math.round(nowMs); + case "Seconds (s)": + return Math.round(nowMs / 1000); + default: + throw new OperationError("Unknown granularity value: " + granularity); + } + } + +} + +export default GetTime;