|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +//#region Types |
| 4 | + |
| 5 | +/** |
| 6 | + * @typedef {Object} TimeUnits |
| 7 | + * @property {number} year |
| 8 | + * @property {number} month |
| 9 | + * @property {number} week |
| 10 | + * @property {number} day |
| 11 | + * @property {number} hour |
| 12 | + * @property {number} minute |
| 13 | + * @property {number} second |
| 14 | + */ |
| 15 | + |
| 16 | +//#endregion |
| 17 | + |
| 18 | +//#region Constants |
| 19 | + |
| 20 | +/** One minute in seconds. */ |
| 21 | +const ONE_MIN = 60; |
| 22 | + |
| 23 | +/** One hour in seconds. */ |
| 24 | +const ONE_HOUR = 60 * ONE_MIN; |
| 25 | + |
| 26 | +/** One day in seconds. */ |
| 27 | +const ONE_DAY = 24 * ONE_HOUR; |
| 28 | + |
| 29 | +/** One week in seconds. */ |
| 30 | +const ONE_WEEK = 7 * ONE_DAY; |
| 31 | + |
| 32 | +/** One month in seconds. */ |
| 33 | +const ONE_MONTH = 4 * ONE_WEEK; |
| 34 | + |
| 35 | +/** One year in seconds. */ |
| 36 | +const ONE_YEAR = 52 * ONE_WEEK; |
| 37 | + |
| 38 | +//#endregion |
| 39 | + |
| 40 | +//#region Classes |
| 41 | + |
| 42 | +/** Represents the difference between two times. */ |
| 43 | +export class TimeDiff { |
| 44 | + /** @type {Date} */ |
| 45 | + #endTime; |
| 46 | + |
| 47 | + /** @type {Date} */ |
| 48 | + #startTime; |
| 49 | + |
| 50 | + /** @type {Map<string, number>} */ |
| 51 | + #units = new Map([ |
| 52 | + ["year", ONE_YEAR], |
| 53 | + ["month", ONE_MONTH], |
| 54 | + ["week", ONE_WEEK], |
| 55 | + ["day", ONE_DAY], |
| 56 | + ["hour", ONE_HOUR], |
| 57 | + ["minute", ONE_MIN], |
| 58 | + ]); |
| 59 | + |
| 60 | + //#region Public API |
| 61 | + |
| 62 | + /** |
| 63 | + * @param {Date} startTime The start of the date range. |
| 64 | + * @param {Date} endTime The end of the date range. |
| 65 | + */ |
| 66 | + constructor(startTime, endTime) { |
| 67 | + this.#validateTimes(startTime, endTime); |
| 68 | + this.#startTime = startTime; |
| 69 | + this.#endTime = endTime; |
| 70 | + } |
| 71 | + |
| 72 | + toString() { |
| 73 | + if (this.value == 0) { |
| 74 | + return "No difference"; |
| 75 | + } |
| 76 | + |
| 77 | + return Object.entries(this.#timeToUnits()) |
| 78 | + .filter((entry) => entry[1] > 0) |
| 79 | + .map(this.#entryToString) |
| 80 | + .join(", "); |
| 81 | + } |
| 82 | + |
| 83 | + /** The time difference rounded to the nearest second. */ |
| 84 | + get value() { |
| 85 | + const diffMs = this.#endTime - this.#startTime; |
| 86 | + return Math.round(diffMs / 1000); |
| 87 | + } |
| 88 | + |
| 89 | + //#endregion |
| 90 | + |
| 91 | + /** |
| 92 | + * Converts a `TimeUnits` value to a string. |
| 93 | + * @param {[keyof TimeUnits, number]} entry |
| 94 | + */ |
| 95 | + #entryToString(entry) { |
| 96 | + const [unitName, value] = entry; |
| 97 | + let str = `${value.toLocaleString()} ${unitName}`; |
| 98 | + |
| 99 | + if (value != 1) { |
| 100 | + str += "s"; |
| 101 | + } |
| 102 | + |
| 103 | + return str; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Tests that a time is valid. |
| 108 | + * @param {Date} time The time to validate. |
| 109 | + */ |
| 110 | + #isValidTime(time) { |
| 111 | + return time instanceof Date && !isNaN(time.valueOf()); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Returns the time difference as a dictionary, with each key representing |
| 116 | + * a unit of time (year, month, etc.). |
| 117 | + * @returns {TimeUnits} |
| 118 | + */ |
| 119 | + #timeToUnits() { |
| 120 | + const units = {}; |
| 121 | + let diffSeconds = this.value; |
| 122 | + |
| 123 | + this.#units.forEach((seconds, unit) => { |
| 124 | + units[unit] = 0; |
| 125 | + |
| 126 | + while (diffSeconds >= seconds) { |
| 127 | + diffSeconds -= seconds; |
| 128 | + units[unit] += 1; |
| 129 | + } |
| 130 | + }); |
| 131 | + |
| 132 | + units.second = diffSeconds >= 0 ? diffSeconds : 0; |
| 133 | + return units; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Validates that the start and end times form a valid date range. |
| 138 | + * @param {Date} startTime The start of the date range. |
| 139 | + * @param {Date} endTime The end of the date range. |
| 140 | + * @throws If either the `startTime` or `endTime` are invalid. |
| 141 | + */ |
| 142 | + #validateTimes(startTime, endTime) { |
| 143 | + if (!this.#isValidTime(startTime)) { |
| 144 | + throw new Error("Start time is invalid."); |
| 145 | + } |
| 146 | + |
| 147 | + if (!this.#isValidTime(endTime)) { |
| 148 | + throw new Error("End time is invalid."); |
| 149 | + } |
| 150 | + |
| 151 | + if (endTime < startTime) { |
| 152 | + throw new Error("The end time must be greater than the start time."); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +//#endregion |
0 commit comments