Skip to content

Commit 2000938

Browse files
authored
Merge pull request #1732 from tomgond/date-delta
2 parents 1d4c810 + c795271 commit 2000938

3 files changed

Lines changed: 131 additions & 0 deletions

File tree

src/core/config/Categories.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@
322322
"To UNIX Timestamp",
323323
"Windows Filetime to UNIX Timestamp",
324324
"UNIX Timestamp to Windows Filetime",
325+
"DateTime Delta",
325326
"Extract dates",
326327
"Get Time",
327328
"Sleep"
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* @author tomgond [tom.gonda@gmail.com]
3+
* @copyright Crown Copyright 2024
4+
* @license Apache-2.0
5+
*/
6+
7+
import Operation from "../Operation.mjs";
8+
import moment from "moment-timezone";
9+
import {DATETIME_FORMATS, FORMAT_EXAMPLES} from "../lib/DateTime.mjs";
10+
11+
/**
12+
* DateTime Delta operation
13+
*/
14+
class DateTimeDelta extends Operation {
15+
16+
/**
17+
* DateTimeDelta constructor
18+
*/
19+
constructor() {
20+
super();
21+
22+
this.name = "DateTime Delta";
23+
this.module = "Default";
24+
this.description = "Calculates a new DateTime value given an input DateTime value and a time difference (delta) from the input DateTime value.";
25+
this.infoURL = "";
26+
this.inputType = "string";
27+
this.outputType = "html";
28+
this.args = [
29+
{
30+
"name": "Built in formats",
31+
"type": "populateOption",
32+
"value": DATETIME_FORMATS,
33+
"target": 1
34+
},
35+
{
36+
"name": "Input format string",
37+
"type": "binaryString",
38+
"value": "DD/MM/YYYY HH:mm:ss"
39+
},
40+
{
41+
"name": "Time Operation",
42+
"type": "option",
43+
"value": ["Add", "Subtract"]
44+
},
45+
{
46+
"name": "Days",
47+
"type": "number",
48+
"value": 0
49+
},
50+
{
51+
"name": "Hours",
52+
"type": "number",
53+
"value": 0
54+
},
55+
{
56+
"name": "Minutes",
57+
"type": "number",
58+
"value": 0
59+
},
60+
{
61+
"name": "Seconds",
62+
"type": "number",
63+
"value": 0
64+
}
65+
66+
];
67+
}
68+
69+
70+
/**
71+
* @param {string} input
72+
* @param {Object[]} args
73+
* @returns {string}
74+
*/
75+
run(input, args) {
76+
const inputTimezone = "UTC";
77+
const inputFormat = args[1];
78+
const operationType = args[2];
79+
const daysDelta = args[3];
80+
const hoursDelta = args[4];
81+
const minutesDelta = args[5];
82+
const secondsDelta = args[6];
83+
let date = "";
84+
85+
try {
86+
date = moment.tz(input, inputFormat, inputTimezone);
87+
if (!date || date.format() === "Invalid date") throw Error;
88+
} catch (err) {
89+
return `Invalid format.\n\n${FORMAT_EXAMPLES}`;
90+
}
91+
let newDate;
92+
if (operationType === "Add") {
93+
newDate = date.add(daysDelta, "days")
94+
.add(hoursDelta, "hours")
95+
.add(minutesDelta, "minutes")
96+
.add(secondsDelta, "seconds");
97+
98+
} else {
99+
newDate = date.add(-daysDelta, "days")
100+
.add(-hoursDelta, "hours")
101+
.add(-minutesDelta, "minutes")
102+
.add(-secondsDelta, "seconds");
103+
}
104+
return newDate.tz(inputTimezone).format(inputFormat.replace(/[<>]/g, ""));
105+
}
106+
}
107+
108+
export default DateTimeDelta;

tests/operations/tests/DateTime.mjs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,26 @@ TestRegister.addTests([
3131
},
3232
],
3333
},
34+
{
35+
name: "DateTime Delta Positive",
36+
input: "20/02/2024 13:36:00",
37+
expectedOutput: "20/02/2024 13:37:00",
38+
recipeConfig: [
39+
{
40+
op: "DateTime Delta",
41+
args: ["Standard date and time", "DD/MM/YYYY HH:mm:ss", "Add", 0, 0, 1, 0],
42+
},
43+
],
44+
},
45+
{
46+
name: "DateTime Delta Negative",
47+
input: "20/02/2024 14:37:00",
48+
expectedOutput: "20/02/2024 13:37:00",
49+
recipeConfig: [
50+
{
51+
op: "DateTime Delta",
52+
args: ["Standard date and time", "DD/MM/YYYY HH:mm:ss", "Subtract", 0, 1, 0, 0],
53+
},
54+
],
55+
},
3456
]);

0 commit comments

Comments
 (0)