Skip to content

Commit bf2950a

Browse files
committed
Merge branch 'bwhitn-master'
2 parents 2c2a0eb + 3eacc32 commit bf2950a

File tree

5 files changed

+119
-2
lines changed

5 files changed

+119
-2
lines changed

src/core/config/Categories.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ const Categories = [
193193
"Translate DateTime Format",
194194
"From UNIX Timestamp",
195195
"To UNIX Timestamp",
196+
"Windows Filetime to UNIX Timestamp",
197+
"UNIX Timestamp to Windows Filetime",
196198
"Extract dates",
197199
]
198200
},

src/core/config/OperationConfig.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,7 +2231,7 @@ const OperationConfig = {
22312231
]
22322232
},
22332233
"From UNIX Timestamp": {
2234-
description: "Converts a UNIX timestamp to a datetime string.<br><br>e.g. <code>978346800</code> becomes <code>Mon 1 January 2001 11:00:00 UTC</code>",
2234+
description: "Converts a UNIX timestamp to a datetime string.<br><br>e.g. <code>978346800</code> becomes <code>Mon 1 January 2001 11:00:00 UTC</code><br><br>A UNIX timestamp is a 32-bit value representing the number of seconds since January 1, 1970 UTC (the UNIX epoch).",
22352235
run: DateTime.runFromUnixTimestamp,
22362236
inputType: "number",
22372237
outputType: "string",
@@ -2244,7 +2244,7 @@ const OperationConfig = {
22442244
]
22452245
},
22462246
"To UNIX Timestamp": {
2247-
description: "Parses a datetime string in UTC and returns the corresponding UNIX timestamp.<br><br>e.g. <code>Mon 1 January 2001 11:00:00</code> becomes <code>978346800</code>",
2247+
description: "Parses a datetime string in UTC and returns the corresponding UNIX timestamp.<br><br>e.g. <code>Mon 1 January 2001 11:00:00</code> becomes <code>978346800</code><br><br>A UNIX timestamp is a 32-bit value representing the number of seconds since January 1, 1970 UTC (the UNIX epoch).",
22482248
run: DateTime.runToUnixTimestamp,
22492249
inputType: "string",
22502250
outputType: "number",
@@ -2261,6 +2261,32 @@ const OperationConfig = {
22612261
}
22622262
]
22632263
},
2264+
"Windows Filetime to UNIX Timestamp":{
2265+
description: "Converts a Windows Filetime value to a UNIX timestamp.<br><br>A Windows Filetime is a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 UTC.<br><br>A UNIX timestamp is a 32-bit value representing the number of seconds since January 1, 1970 UTC (the UNIX epoch).<br><br>This operation also supports UNIX timestamps in milliseconds, microseconds and nanoseconds.",
2266+
run: DateTime.runFromFiletimeToUnix,
2267+
inputType: "string",
2268+
outputType: "string",
2269+
args: [
2270+
{
2271+
name: "Output units",
2272+
type: "option",
2273+
value: DateTime.UNITS
2274+
}
2275+
]
2276+
},
2277+
"UNIX Timestamp to Windows Filetime":{
2278+
description: "Converts a UNIX timestamp to a Windows Filetime value.<br><br>A Windows Filetime is a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 UTC.<br><br>A UNIX timestamp is a 32-bit value representing the number of seconds since January 1, 1970 UTC (the UNIX epoch).<br><br>This operation also supports UNIX timestamps in milliseconds, microseconds and nanoseconds.",
2279+
run: DateTime.runToFiletimeFromUnix,
2280+
inputType: "string",
2281+
outputType: "string",
2282+
args: [
2283+
{
2284+
name: "Input units",
2285+
type: "option",
2286+
value: DateTime.UNITS
2287+
}
2288+
]
2289+
},
22642290
"Translate DateTime Format": {
22652291
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.",
22662292
run: DateTime.runTranslateFormat,

src/core/operations/DateTime.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {BigInteger} from "jsbn";
2+
13
/**
24
* Date and time operations.
35
*
@@ -78,6 +80,58 @@ const DateTime = {
7880
},
7981

8082

83+
/**
84+
* Windows Filetime to Unix Timestamp operation.
85+
*
86+
* @author bwhitn [[email protected]]
87+
* @param {string} input
88+
* @param {Object[]} args
89+
* @returns {string}
90+
*/
91+
runFromFiletimeToUnix: function(input, args) {
92+
let units = args[0];
93+
input = new BigInteger(input).subtract(new BigInteger("116444736000000000"));
94+
if (units === "Seconds (s)"){
95+
input = input.divide(new BigInteger("10000000"));
96+
} else if (units === "Milliseconds (ms)") {
97+
input = input.divide(new BigInteger("10000"));
98+
} else if (units === "Microseconds (μs)") {
99+
input = input.divide(new BigInteger("10"));
100+
} else if (units === "Nanoseconds (ns)") {
101+
input = input.multiply(new BigInteger("100"));
102+
} else {
103+
throw "Unrecognised unit";
104+
}
105+
return input.toString();
106+
},
107+
108+
109+
/**
110+
* Unix Timestamp to Windows Filetime operation.
111+
*
112+
* @author bwhitn [[email protected]]
113+
* @param {string} input
114+
* @param {Object[]} args
115+
* @returns {string}
116+
*/
117+
runToFiletimeFromUnix: function(input, args) {
118+
let units = args[0];
119+
input = new BigInteger(input);
120+
if (units === "Seconds (s)"){
121+
input = input.multiply(new BigInteger("10000000"));
122+
} else if (units === "Milliseconds (ms)") {
123+
input = input.multiply(new BigInteger("10000"));
124+
} else if (units === "Microseconds (μs)") {
125+
input = input.multiply(new BigInteger("10"));
126+
} else if (units === "Nanoseconds (ns)") {
127+
input = input.divide(new BigInteger("100"));
128+
} else {
129+
throw "Unrecognised unit";
130+
}
131+
return input.add(new BigInteger("116444736000000000")).toString();
132+
},
133+
134+
81135
/**
82136
* @constant
83137
* @default

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import "./tests/operations/ByteRepr.js";
1616
import "./tests/operations/CharEnc.js";
1717
import "./tests/operations/Code.js";
1818
import "./tests/operations/Compress.js";
19+
import "./tests/operations/DateTime.js";
1920
import "./tests/operations/FlowControl.js";
2021
import "./tests/operations/Image.js";
2122
import "./tests/operations/MorseCode.js";

test/tests/operations/DateTime.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* DateTime tests.
3+
*
4+
* @author bwhitn [[email protected]]
5+
*
6+
* @copyright Crown Copyright 2017
7+
* @license Apache-2.0
8+
*/
9+
import TestRegister from "../../TestRegister.js";
10+
11+
TestRegister.addTests([
12+
{
13+
name: "Filetime to Unix",
14+
input: "129207366395297693",
15+
expectedOutput: "1276263039529769300",
16+
recipeConfig: [
17+
{
18+
op: "Windows Filetime to UNIX Timestamp",
19+
args: ["Nanoseconds (ns)"],
20+
},
21+
],
22+
},
23+
{
24+
name: "Unix to Filetime",
25+
input: "1276263039529769300",
26+
expectedOutput: "129207366395297693",
27+
recipeConfig: [
28+
{
29+
op: "UNIX Timestamp to Windows Filetime",
30+
args: ["Nanoseconds (ns)"],
31+
},
32+
],
33+
},
34+
]);

0 commit comments

Comments
 (0)