add new operation: From/To MS-DOS Date and Time

This commit is contained in:
MikeCAT 2022-10-30 23:34:30 +09:00
parent ed8bd34915
commit a940a3252d
5 changed files with 420 additions and 0 deletions

View file

@ -290,6 +290,8 @@
"To UNIX Timestamp", "To UNIX Timestamp",
"Windows Filetime to UNIX Timestamp", "Windows Filetime to UNIX Timestamp",
"UNIX Timestamp to Windows Filetime", "UNIX Timestamp to Windows Filetime",
"From MS-DOS Date and Time",
"To MS-DOS Date and Time",
"Extract dates", "Extract dates",
"Get Time", "Get Time",
"Sleep" "Sleep"

View file

@ -0,0 +1,87 @@
/**
* @author mikecat
* @copyright Crown Copyright 2022
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
import moment from "moment-timezone";
import OperationError from "../errors/OperationError.mjs";
/**
* From MS-DOS Date and Time operation
*/
class FromMSDOSDateAndTime extends Operation {
/**
* FromMSDOSDateAndTime constructor
*/
constructor() {
super();
this.name = "From MS-DOS Date and Time";
this.module = "Default";
this.description = "Receives a space-separated pair of MS-DOS date and time (16-bit unsigned integers) in this order and returns the corresponding datetime in <code>yyyy-MM-dd HH:mm:ss</code> format.<br><br>Some examples of where MS-DOS date and time are used are ZIP archive file and FAT filesystem.";
this.infoURL = "https://learn.microsoft.com/en-us/windows/win32/sysinfo/ms-dos-date-and-time";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
"name": "Input Format",
"type": "option",
"value": ["Decimal", "Hex"]
},
{
"name": "Validate datetime",
"type": "boolean",
"value": true
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const inputFormat = args[0], validate = args[1];
const radixTable = {"Decimal": 10, "Hex": 16};
if (!(inputFormat in radixTable)) {
throw new OperationError("undefined input format");
}
const radix = radixTable[inputFormat];
const inputParts = input.split(/\s+/);
if (inputParts.length < 2) {
throw new OperationError("invalid input");
}
const dateInput = parseInt(inputParts[0], radix);
const timeInput = parseInt(inputParts[1], radix);
if (isNaN(dateInput) || dateInput < 0 || 0xffff < dateInput ||
isNaN(timeInput) || timeInput < 0 || 0xffff < timeInput) {
throw new OperationError("invalid input");
}
const year = ((dateInput >> 9) & 0x7f) + 1980;
const month = (dateInput >> 5) & 0x0f;
const date = dateInput & 0x1f;
const hour = (timeInput >> 11) & 0x1f;
const minute = (timeInput >> 5) & 0x3f;
const second = (timeInput & 0x1f) << 1;
if (validate) {
const m = moment([year, month - 1, date, hour, minute, second]);
if (!m.isValid()) {
throw new OperationError("invalid datetime");
}
}
const toTwoDigits = function(value) {
return (value >= 10 ? "" : "0") + value;
};
return "" + year + "-" + toTwoDigits(month) + "-" + toTwoDigits(date) +
" " + toTwoDigits(hour) + ":" + toTwoDigits(minute) + ":" + toTwoDigits(second);
}
}
export default FromMSDOSDateAndTime;

View file

@ -0,0 +1,90 @@
/**
* @author mikecat
* @copyright Crown Copyright 2022
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
import moment from "moment-timezone";
import OperationError from "../errors/OperationError.mjs";
/**
* To MS-DOS Date and Time operation
*/
class ToMSDOSDateAndTime extends Operation {
/**
* ToMSDOSDateAndTime constructor
*/
constructor() {
super();
this.name = "To MS-DOS Date and Time";
this.module = "Default";
this.description = "Parses a datetime string and returns the corresponding space-separated pair of MS-DOS date and time in this order.<br>Each of date and time are represented as 16-bit unsigned integers.<br>Years between 1980 and 2107 (inclusive) are supported.<br><br>Some examples of where MS-DOS date and time are used are ZIP archive file and FAT filesystem.";
this.infoURL = "https://learn.microsoft.com/en-us/windows/win32/sysinfo/ms-dos-date-and-time";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
"name": "Output Format",
"type": "option",
"value": ["Decimal", "Hex"]
},
{
"name": "Show parsed datetime",
"type": "boolean",
"value": true
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const outputFormat = args[0], showDate = args[1];
const date = moment(input);
if (!date.isValid()) {
throw new OperationError("invalid input");
}
const outputFormatter = (function() {
switch (outputFormat) {
case "Decimal":
return function(value) {
return value.toString();
};
case "Hex":
return function(value) {
const result = value.toString(16);
if (result.length >= 4) return result;
return ("0000" + result).substring(result.length);
};
default:
throw new OperationError("undefined output format");
}
})();
const year = date.year();
if (year < 1980 || 2107 < year) {
throw new OperationError("out-of-range");
}
const dateOut =
((year - 1980) << 9) |
((date.month() + 1) << 5) |
date.date();
const timeOut =
(date.hour() << 11) |
(date.minute() << 5) |
(date.second() >> 1);
const output = outputFormatter(dateOut) + " " + outputFormatter(timeOut);
if (showDate) {
return output + " (" + date.format("yyyy-MM-DD HH:mm:ss") + ")";
}
return output;
}
}
export default ToMSDOSDateAndTime;

View file

@ -124,6 +124,7 @@ import "./tests/UnescapeString.mjs";
import "./tests/LS47.mjs"; import "./tests/LS47.mjs";
import "./tests/LZString.mjs"; import "./tests/LZString.mjs";
import "./tests/NTLM.mjs"; import "./tests/NTLM.mjs";
import "./tests/MSDOSDateAndTime.mjs";
// Cannot test operations that use the File type yet // Cannot test operations that use the File type yet
// import "./tests/SplitColourChannels.mjs"; // import "./tests/SplitColourChannels.mjs";

View file

@ -0,0 +1,240 @@
/**
* @author mikecat
* @copyright Crown Copyright 2022
* @license Apache-2.0
*/
import TestRegister from "../../lib/TestRegister.mjs";
TestRegister.addTests([
{
"name": "From MS-DOS Date and Time",
"input": "21854 25692",
"expectedOutput": "2022-10-30 12:34:56",
"recipeConfig": [
{
"op": "From MS-DOS Date and Time",
"args": ["Decimal", true],
},
],
},
{
"name": "From MS-DOS Date and Time: minimum date",
"input": "33 0",
"expectedOutput": "1980-01-01 00:00:00",
"recipeConfig": [
{
"op": "From MS-DOS Date and Time",
"args": ["Decimal", true],
},
],
},
{
"name": "From MS-DOS Date and Time: maximum date",
"input": "65439 49021",
"expectedOutput": "2107-12-31 23:59:58",
"recipeConfig": [
{
"op": "From MS-DOS Date and Time",
"args": ["Decimal", true],
},
],
},
{
"name": "From MS-DOS Date and Time: month too small",
"input": "21534 25692",
"expectedOutput": "invalid datetime",
"recipeConfig": [
{
"op": "From MS-DOS Date and Time",
"args": ["Decimal", true],
},
],
},
{
"name": "From MS-DOS Date and Time: month too large",
"input": "21950 25692",
"expectedOutput": "invalid datetime",
"recipeConfig": [
{
"op": "From MS-DOS Date and Time",
"args": ["Decimal", true],
},
],
},
{
"name": "From MS-DOS Date and Time: date too small",
"input": "21824 25692",
"expectedOutput": "invalid datetime",
"recipeConfig": [
{
"op": "From MS-DOS Date and Time",
"args": ["Decimal", true],
},
],
},
{
"name": "From MS-DOS Date and Time: date too large",
"input": "21823 25692",
"expectedOutput": "invalid datetime",
"recipeConfig": [
{
"op": "From MS-DOS Date and Time",
"args": ["Decimal", true],
},
],
},
{
"name": "From MS-DOS Date and Time: hour too large",
"input": "21854 50268",
"expectedOutput": "invalid datetime",
"recipeConfig": [
{
"op": "From MS-DOS Date and Time",
"args": ["Decimal", true],
},
],
},
{
"name": "From MS-DOS Date and Time: minute too large",
"input": "21854 26524",
"expectedOutput": "invalid datetime",
"recipeConfig": [
{
"op": "From MS-DOS Date and Time",
"args": ["Decimal", true],
},
],
},
{
"name": "From MS-DOS Date and Time: second too large",
"input": "21854 25694",
"expectedOutput": "invalid datetime",
"recipeConfig": [
{
"op": "From MS-DOS Date and Time",
"args": ["Decimal", true],
},
],
},
{
"name": "From MS-DOS Date and Time: hexadecimal input",
"input": "2a47 75b1",
"expectedOutput": "2001-02-07 14:45:34",
"recipeConfig": [
{
"op": "From MS-DOS Date and Time",
"args": ["Hex", true],
},
],
},
{
"name": "From MS-DOS Date and Time: disable validation",
"input": "21954 55711",
"expectedOutput": "2022-14-02 27:12:62",
"recipeConfig": [
{
"op": "From MS-DOS Date and Time",
"args": ["Decimal", false],
},
],
},
{
"name": "From MS-DOS Date and Time: ignore extra elements",
"input": "18137 10735 21566",
"expectedOutput": "2015-06-25 05:15:30",
"recipeConfig": [
{
"op": "From MS-DOS Date and Time",
"args": ["Decimal", true],
},
],
},
{
"name": "To MS-DOS Date and Time",
"input": "2022-10-30 13:24:56",
"expectedOutput": "21854 27420 (2022-10-30 13:24:56)",
"recipeConfig": [
{
"op": "To MS-DOS Date and Time",
"args": ["Decimal", true],
},
],
},
{
"name": "To MS-DOS Date and Time: minimum year",
"input": "1980-01-01 00:00:00",
"expectedOutput": "33 0 (1980-01-01 00:00:00)",
"recipeConfig": [
{
"op": "To MS-DOS Date and Time",
"args": ["Decimal", true],
},
],
},
{
"name": "To MS-DOS Date and Time: maximum year",
"input": "2107-12-31 23:59:59",
"expectedOutput": "65439 49021 (2107-12-31 23:59:59)",
"recipeConfig": [
{
"op": "To MS-DOS Date and Time",
"args": ["Decimal", true],
},
],
},
{
"name": "To MS-DOS Date and Time: minimum year - 1",
"input": "1979-12-31 23:59:59",
"expectedOutput": "out-of-range",
"recipeConfig": [
{
"op": "To MS-DOS Date and Time",
"args": ["Decimal", true],
},
],
},
{
"name": "To MS-DOS Date and Time: maximum year + 1",
"input": "2108-01-01 00:00:00",
"expectedOutput": "out-of-range",
"recipeConfig": [
{
"op": "To MS-DOS Date and Time",
"args": ["Decimal", true],
},
],
},
{
"name": "To MS-DOS Date and Time: hexadecimal output",
"input": "2004-09-13 15:02:28",
"expectedOutput": "312d 784e (2004-09-13 15:02:28)",
"recipeConfig": [
{
"op": "To MS-DOS Date and Time",
"args": ["Hex", true],
},
],
},
{
"name": "To MS-DOS Date and Time: hexadecimal output, small values",
"input": "1985-05-23 00:04:48",
"expectedOutput": "0ab7 0098 (1985-05-23 00:04:48)",
"recipeConfig": [
{
"op": "To MS-DOS Date and Time",
"args": ["Hex", true],
},
],
},
{
"name": "To MS-DOS Date and Time: no parsed datetime",
"input": "1998-11-06 21:37:52",
"expectedOutput": "9574 44218",
"recipeConfig": [
{
"op": "To MS-DOS Date and Time",
"args": ["Decimal", false],
},
],
},
]);