Applied linter to the 2 new operations 'NTPTimestampToUNIXTimestamp' and 'UNIXTimestampToNTPTimestamp'

This commit is contained in:
kossithedon 2024-05-19 01:39:24 +02:00
parent f0e00716d8
commit 8b6ccc8686
No known key found for this signature in database
GPG key ID: C482A9E4338AB371
3 changed files with 69 additions and 78 deletions

View file

@ -38,7 +38,6 @@ class NTPTimestampToUNIXTimestamp extends Operation {
"type": "option", "type": "option",
"value": ["Seconds (s)", "Milliseconds (ms)", "Microseconds (μs)", "Nanoseconds (ns)"] "value": ["Seconds (s)", "Milliseconds (ms)", "Microseconds (μs)", "Nanoseconds (ns)"]
} }
]; ];
} }
@ -56,36 +55,38 @@ class NTPTimestampToUNIXTimestamp extends Operation {
input = new RemoveWhitespace().run(input, [true, true, true, true, true, false]); input = new RemoveWhitespace().run(input, [true, true, true, true, true, false]);
} }
let ntpTimestampSecondsPart;
let ntpTimestampFractionsPart;
if (format.startsWith("Hex")) { if (format.startsWith("Hex")) {
if (input.length != 16) { if (input.length !== 16) {
return `Error: NTP Timestamp should be 64 bits long`; return `Error: NTP Timestamp should be 64 bits long`;
} }
if (format === "Hex (little-endian)") { if (format === "Hex (little-endian)") {
// Convert little-endian to big-endian // Convert little-endian to big-endian
input = new SwapEndianness().run(input, ["Raw", input.length, false]); input = new SwapEndianness().run(input, ["Raw", input.length, false]);
} }
// Getting the 32 bits (8 first hexa values) long seconds part // Getting the 32 bits (8 first hexa values) long seconds part
const hex_ntp_timestamp_seconds_part = input.substring(0, 8); const hexNtpTimestampSecondsPart = input.substring(0, 8);
// Getting the 32 bits (8 last hexa values) long seconds fractions part // Getting the 32 bits (8 last hexa values) long seconds fractions part
const hex_ntp_timestamp_fractions_part = input.substring(input.length - 8, input.length); const hexNtpTimestampFractionsPart = input.substring(input.length - 8, input.length);
// Convert hexadecimal values to decimal values // Convert hexadecimal values to decimal values
var ntp_timestamp_seconds_part = new BigNumber(hex_ntp_timestamp_seconds_part, 16); ntpTimestampSecondsPart = new BigNumber(hexNtpTimestampSecondsPart, 16);
var ntp_timestamp_fractions_part = new BigNumber(hex_ntp_timestamp_fractions_part, 16); ntpTimestampFractionsPart = new BigNumber(hexNtpTimestampFractionsPart, 16);
} else if (format == "Fixed-point decimal") { } else if (format === "Fixed-point decimal") {
// Get the seconds and the seconds fractions parts of the timestamp separated by a "." // Get the seconds and the seconds fractions parts of the timestamp separated by a "."
const pf_ntp_timestamp_seconds_and_fractions_parts = String(input).split("."); const pfNtpTimestampSecondsAndFractionsParts = String(input).split(".");
var ntp_timestamp_seconds_part = new Number(pf_ntp_timestamp_seconds_and_fractions_parts[0]); ntpTimestampSecondsPart = new Number(pfNtpTimestampSecondsAndFractionsParts[0]);
var ntp_timestamp_fractions_part = pf_ntp_timestamp_seconds_and_fractions_parts[1]; ntpTimestampFractionsPart = pfNtpTimestampSecondsAndFractionsParts[1];
if (ntp_timestamp_fractions_part == null) { if (ntpTimestampFractionsPart === null) {
ntp_timestamp_fractions_part = 0 ntpTimestampFractionsPart = 0;
} else { } else {
ntp_timestamp_fractions_part = new Number(pf_ntp_timestamp_seconds_and_fractions_parts[1]); ntpTimestampFractionsPart = new Number(pfNtpTimestampSecondsAndFractionsParts[1]);
} }
} else { } else {
@ -93,45 +94,40 @@ class NTPTimestampToUNIXTimestamp extends Operation {
} }
// Set the maximum unsigned positive integer representable in 32 bits // Set the maximum unsigned positive integer representable in 32 bits
const max_uint32=new Number(Math.pow(2, 32)) const maxUint32=new Number(Math.pow(2, 32));
// Check whether the seconds and the seconds fractions parts values do // Check whether the seconds and the seconds fractions parts values do
// not exceeds the maximum positive integer representable in 32 bits // not exceeds the maximum positive integer representable in 32 bits
if (ntp_timestamp_seconds_part > max_uint32) if (ntpTimestampSecondsPart > maxUint32) {
{ return `Error: Timestamp seconds part should be 32 bits long. The seconds part '${ntpTimestampSecondsPart}' of the provided NTP timestamp exceeds the maximum positive integer representable in 32 bits '${maxUint32}'`;
return `Error: Timestamp seconds part should be 32 bits long. The seconds part '${ntp_timestamp_seconds_part}' of the provided NTP timestamp exceeds the maximum positive integer representable in 32 bits '${max_uint32}'`;
} }
if (ntpTimestampFractionsPart > maxUint32) {
if (ntp_timestamp_fractions_part > max_uint32) return `Error: Timestamp fractions seconds part should be 32 bits long. The fractions seconds part '${ntpTimestampFractionsPart}' of the provided NTP timestamp exceeds the maximum positive integer representable in 32 bits '${maxUint32}'`;
{
return `Error: Timestamp fractions seconds part should be 32 bits long. The fractions seconds part '${ntp_timestamp_fractions_part}' of the provided NTP timestamp exceeds the maximum positive integer representable in 32 bits '${max_uint32}'`;
} }
// Convert the NTP timestamp seconds part value (seconds elapsed since 01 january // Convert the NTP timestamp seconds part value (seconds elapsed since 01 january
// 1900 midnight) to UNIX timestamp (seconds elapsed since 01 january 1970 midnight) // 1900 midnight) to UNIX timestamp (seconds elapsed since 01 january 1970 midnight)
const unix_timestamp_seconds_part = ntp_timestamp_seconds_part - new Number("2208988800"); const unixTimestampSecondsPart = ntpTimestampSecondsPart - new Number("2208988800");
// Convert the NTP timestamp seconds fractions part value to seconds // Convert the NTP timestamp seconds fractions part value to seconds
const unix_timestamp_fractions_part = ntp_timestamp_fractions_part / new Number(Math.pow(2, 32)); const unixTimestampFractionsPart = ntpTimestampFractionsPart / new Number(Math.pow(2, 32));
// Addition the seconds part value to the seconds fractions part value // Addition the seconds part value to the seconds fractions part value
// to form the UNIX timestamp in seconds // to form the UNIX timestamp in seconds
let unix_timestamp=unix_timestamp_seconds_part + unix_timestamp_fractions_part; let unixTimestamp=unixTimestampSecondsPart + unixTimestampFractionsPart;
// Convert seconds Unix timestamp to requested result unit // Convert seconds Unix timestamp to requested result unit
if (unit === "Seconds (s)") { if (unit === "Seconds (s)") {
return String(unix_timestamp); return String(unixTimestamp);
} else if (unit === "Milliseconds (ms)") { } else if (unit === "Milliseconds (ms)") {
unix_timestamp = unix_timestamp * new Number("1000"); unixTimestamp = unixTimestamp * new Number("1000");
} else if (unit === "Microseconds (μs)") { } else if (unit === "Microseconds (μs)") {
unix_timestamp = unix_timestamp * new Number(Math.pow(10, 6)); unixTimestamp = unixTimestamp * new Number(Math.pow(10, 6));
} else if (unit === "Nanoseconds (ns)") { } else if (unit === "Nanoseconds (ns)") {
unix_timestamp = unix_timestamp * new Number(Math.pow(10, 9)); unixTimestamp = unixTimestamp * new Number(Math.pow(10, 9));
} else { } else {
throw new OperationError("Unrecognised unit"); throw new OperationError("Unrecognised unit");
} }
return String(unix_timestamp); return String(unixTimestamp);
} }
} }

View file

@ -37,7 +37,6 @@ class NTPTimestampToUNIXTimestamp extends Operation {
"type": "option", "type": "option",
"value": ["Fixed-point decimal", "Hex (big-endian)", "Hex (little-endian)"] "value": ["Fixed-point decimal", "Hex (big-endian)", "Hex (little-endian)"]
} }
]; ];
} }
@ -55,62 +54,59 @@ class NTPTimestampToUNIXTimestamp extends Operation {
input = new RemoveWhitespace().run(input, [true, true, true, true, true, false]); input = new RemoveWhitespace().run(input, [true, true, true, true, true, false]);
} }
let unix_timestamp_seconds; let unixTimestampSeconds;
// Convert the provided Unix timestmap to seconds Unix timestamps // Convert the provided Unix timestmap to seconds Unix timestamps
if (unit === "Seconds (s)") { if (unit === "Seconds (s)") {
unix_timestamp_seconds = input; unixTimestampSeconds = input;
} else if (unit === "Milliseconds (ms)") { } else if (unit === "Milliseconds (ms)") {
unix_timestamp_seconds = input / new Number("1000"); unixTimestampSeconds = input / new Number("1000");
} else if (unit === "Microseconds (μs)") { } else if (unit === "Microseconds (μs)") {
unix_timestamp_seconds = input / new Number(Math.pow(10, 6)); unixTimestampSeconds = input / new Number(Math.pow(10, 6));
} else if (unit === "Nanoseconds (ns)") { } else if (unit === "Nanoseconds (ns)") {
unix_timestamp_seconds = input / new Number(Math.pow(10, 9)); unixTimestampSeconds = input / new Number(Math.pow(10, 9));
} else { } else {
throw new OperationError("Unrecognised unit"); throw new OperationError("Unrecognised unit");
} }
// Get the seconds and the fractions seconds parts of the UNIX timestamp // Get the seconds and the fractions seconds parts of the UNIX timestamp
const unix_timestamp_seconds_part = Math.floor(unix_timestamp_seconds); const unixTimestampSecondsPart = Math.floor(unixTimestampSeconds);
const unix_timestamp_fractions_part = unix_timestamp_seconds % 1; const unixTimestampFractionsPart = unixTimestampSeconds % 1;
// The greatest seconds value is the maximum unsigned positive integer representable // The greatest seconds value is the maximum unsigned positive integer representable
// in 32 bits (2**32) - 2208988800 (seconds elapsed from NTP Epoch and UNIX Epoch) // in 32 bits (2**32) - 2208988800 (seconds elapsed from NTP Epoch and UNIX Epoch)
const greatest_seconds_value = Math.pow(2, 32) - 2208988800 const greatestSecondsValue = Math.pow(2, 32) - 2208988800;
// Check whether the seconds value part do not exceeds the greatest seconds value // Check whether the seconds value part do not exceeds the greatest seconds value
if (unix_timestamp_seconds_part > greatest_seconds_value) if (unixTimestampSecondsPart > greatestSecondsValue) {
{ return `Error: The NTP Timestamp seconds part '${unixTimestampSecondsPart}' exceeds the greatest authorized seconds value ${greatestSecondsValue} due to an incorrect provided UNIX timestamp`;
return `Error: The NTP Timestamp seconds part '${unix_timestamp_seconds_part}' exceeds the greatest authorized seconds value ${greatest_seconds_value} due to an incorrect provided UNIX timestamp`;
} }
// Convert the UNIX timestamp seconds part value (seconds elapsed since 01 january // Convert the UNIX timestamp seconds part value (seconds elapsed since 01 january
// 1970 midnight) to NTP timestamp (seconds elapsed since 01 january 1900 midnight) // 1970 midnight) to NTP timestamp (seconds elapsed since 01 january 1900 midnight)
var ntp_timestamp_seconds_part = unix_timestamp_seconds_part + new Number("2208988800"); const ntpTimestampSecondsPart = unixTimestampSecondsPart + new Number("2208988800");
// Convert the NTP timestamp seconds fractions part value to seconds // Convert the NTP timestamp seconds fractions part value to seconds
var ntp_timestamp_fractions_part = unix_timestamp_fractions_part * (Math.pow(2, 32)); const ntpTimestampFractionsPart = unixTimestampFractionsPart * (Math.pow(2, 32));
if (format.startsWith("Hex")) { if (format.startsWith("Hex")) {
// Convert Unix timestamp seconds and seconds fractions parts from decimal to hexadecimal // Convert Unix timestamp seconds and seconds fractions parts from decimal to hexadecimal
const hex_ntp_timestamp_seconds_part = ntp_timestamp_seconds_part.toString(16); const hexNtpTimestampSecondsPart = ntpTimestampSecondsPart.toString(16);
var hex_ntp_timestamp_fractions_part = ntp_timestamp_fractions_part.toString(16); let hexNtptimestampfractionsPart = ntpTimestampFractionsPart.toString(16);
if (hex_ntp_timestamp_fractions_part == 0) { if (hexNtptimestampfractionsPart === 0) {
// pad hexadecimal seconds fractions part // pad hexadecimal seconds fractions part
hex_ntp_timestamp_fractions_part = "00000000" hexNtptimestampfractionsPart = "00000000";
} }
// Concatenate seconds part hexadecimal value to seconds fractions part // Concatenate seconds part hexadecimal value to seconds fractions part
// hexadecimal value to form the big-endian hexadecimal Unix timestamp // hexadecimal value to form the big-endian hexadecimal Unix timestamp
const be_hex_ntp_timestamp = hex_ntp_timestamp_seconds_part + hex_ntp_timestamp_fractions_part; const beHexNtpTimestamp = hexNtpTimestampSecondsPart + hexNtptimestampfractionsPart;
if (format === "Hex (little-endian)") { if (format === "Hex (little-endian)") {
// Convert big-endian to little-endian // Convert big-endian to little-endian
const le_hex_ntp_timestamp = new SwapEndianness().run(be_hex_ntp_timestamp, ["Raw", 16, false]); const leHexNtpTimestamp = new SwapEndianness().run(beHexNtpTimestamp, ["Raw", 16, false]);
return le_hex_ntp_timestamp; return leHexNtpTimestamp;
} else if (format === "Hex (big-endian)") { } else if (format === "Hex (big-endian)") {
return be_hex_ntp_timestamp; return beHexNtpTimestamp;
} else { } else {
throw new OperationError("Unrecognised format"); throw new OperationError("Unrecognised format");
} }
@ -118,14 +114,13 @@ class NTPTimestampToUNIXTimestamp extends Operation {
} else if (format === "Fixed-point decimal") { } else if (format === "Fixed-point decimal") {
// Construct the NTP timestamp by concatenating the seconds part // Construct the NTP timestamp by concatenating the seconds part
// value to the seconds fractions part value separeted by a "." // value to the seconds fractions part value separeted by a "."
const pf_ntp_timestamp=ntp_timestamp_seconds_part+'.'+ntp_timestamp_fractions_part; const pfNtpTimestamp=ntpTimestampSecondsPart+"."+ntpTimestampFractionsPart;
return pf_ntp_timestamp; return pfNtpTimestamp;
} else { } else {
throw new OperationError("Unrecognised format"); throw new OperationError("Unrecognised format");
} }
} }
} }
export default NTPTimestampToUNIXTimestamp; export default NTPTimestampToUNIXTimestamp;

View file

@ -159,7 +159,7 @@ TestRegister.addTests([
recipeConfig: [ recipeConfig: [
{ {
op: "UNIX Timestamp to NTP Timestamp", op: "UNIX Timestamp to NTP Timestamp",
args: ["Seconds (s)", "Floating-point", ], args: ["Seconds (s)", "Floating-point"],
}, },
], ],
}, },