modifications to FILETIME

This commit is contained in:
Bwhit1 2017-06-10 23:42:02 -04:00
parent a144f65dcf
commit 003e076b00
2 changed files with 45 additions and 14 deletions

View file

@ -2261,19 +2261,31 @@ const OperationConfig = {
} }
] ]
}, },
"From Windows Filetime To UNIX":{ "Windows Filetime to UNIX Timestamp":{
description: "Converts a Windows Filetime timestamp to a datetime format", description: "Converts a Windows Filetime timestamp to a datetime format",
run: DateTime.runFromFiletimeToUnix, run: DateTime.runFromFiletimeToUnix,
inputType: "string", inputType: "string",
outputType: "string", outputType: "string",
args: [] args: [
{
name: "Units",
type: "Option",
value: DateTime.UNITS
}
]
}, },
"To Windows Filetime From UNIX":{ "UNIX Timestamp to Windows Filetime":{
description: "Parses a datetime string in UTC and returns the corresponding Windows Filetime timestamp", description: "Parses a datetime string in UTC and returns the corresponding Windows Filetime timestamp",
run: DateTime.runToFiletimeFromUnix, run: DateTime.runToFiletimeFromUnix,
inputType: "string", inputType: "string",
outputType: "string", outputType: "string",
args: [] args: [
{
name: "Units",
type: "Option",
value: DateTime.UNITS
}
]
}, },
"Translate DateTime Format": { "Translate DateTime Format": {
description: "Parses a datetime string in one format and re-writes it in another.<br><br>Run with no input to see the relevant format string examples.", description: "Parses a datetime string in one format and re-writes it in another.<br><br>Run with no input to see the relevant format string examples.",

View file

@ -1,4 +1,6 @@
import Decimal from "../lib/decimal.min.js"; //import Decimal from "../lib/decimal.min.js";
import Utils from "../Utils.js";
import {BigInteger} from "jsbn";
/** /**
* Date and time operations. * Date and time operations.
@ -83,35 +85,52 @@ const DateTime = {
/** /**
* Converts a Windows FILETIME to Unix Epoch time. * Converts a Windows FILETIME to Unix Epoch time.
* *
* @author bwhitn [brian.m.whitney@outlook.com]
* @param {string} input * @param {string} input
* @param {Object[]} args (not used) * @param {Object[]} args (not used)
* @returns {string} * @returns {string}
*/ */
runFromFiletimeToUnix: function(input, args) { runFromFiletimeToUnix: function(input, args) {
input = new Decimal(input); let units = args[0], offset = new BigInteger("116444736000000000");
input = input.sub(116444736000000000).div(10000000); input = new BigInteger(input,16).subtract(offset);
if (input.gte(0) && input.lt(Math.pow(2, 31))){ if (units === "Seconds (s)"){
return input.toString(); input = input.divide(new BigInteger("10000000"));
} else if (units === "Milliseconds (ms)") {
input = input.divide(new BigInteger("10000"));
} else if (units === "Microseconds (μs)") {
input = input.divide(new BigInteger("10"));
} else if (units === "Nanoseconds (ns)") {
input = input.multiply(new BigInteger("100"));
} else { } else {
throw "Date " + input + " is not a valid date"; throw "The value " + input + " cannot be expressed as a UNIX timestamp.";
} }
return input.toString();
}, },
/** /**
* Converts a Unix Epoch time to Windows FILETIME. * Converts a Unix Epoch time to Windows FILETIME.
* *
* @author bwhitn [brian.m.whitney@outlook.com]
* @param {string} input * @param {string} input
* @param {Object[]} args (not used) * @param {Object[]} args (not used)
* @returns {string} * @returns {string}
*/ */
runToFiletimeFromUnix: function(input, args) { runToFiletimeFromUnix: function(input, args) {
input = new Decimal(input); let units = args[0], offset = new BigInteger("116444736000000000");
if (input.gte(0) && input.lt(Math.pow(2, 31))){ input = new BigInteger(input);
return input.mul(10000000).add(116444736000000000).toHex(); if (units === "Seconds (s)"){
input = input.multiply(new BigInteger("10000000")).add(offset);
} else if (units === "Milliseconds (ms)") {
input = input.multiply(new BigInteger("10000")).add(offset);
} else if (units === "Microseconds (μs)") {
input = input.multiply(new BigInteger("10")).add(offset);
} else if (units === "Nanoseconds (ns)") {
input = input.divide(new BigInteger("100")).add(offset);
} else { } else {
throw "Date " + input + " is not a valid date"; throw "The value " + input + " cannot be expressed as a UNIX timestamp.";
} }
return input.toString();
}, },