|
| 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; |
0 commit comments