|
| 1 | +/** |
| 2 | + * IsoDate – a lightweight class for representing calendar dates. |
| 3 | + * |
| 4 | + * Supported input formats: |
| 5 | + * - YYYY-MM-DD (e.g., 2025-01-01, equivalent to 2025-01-01T00:00:00.000Z/2025-01-01T23:59:59.999Z) |
| 6 | + * - YYYY-MM (e.g., 2025-01, equivalent to 2025-01-01T00:00:00.000Z/2025-01-31T23:59:59.999Z) |
| 7 | + * - YYYY (e.g., 2025, equivalent to 2025-01-01T00:00:00.000Z/2025-12-31T23:59:59.999Z) |
| 8 | + * |
| 9 | + * As shown in the example above, all IsoDate is assumed to be UTC (i.e., timezone = 'Z') |
| 10 | + * |
| 11 | + * Note we do not support (even though it is allowed by ISO 8601) |
| 12 | + * - "compact date" (i.e., YYYYMMDD) |
| 13 | + * - years previous to 1 AD (i.e., zero and negative years) |
| 14 | + * - years after 2500 |
| 15 | + * |
| 16 | + * The class validates monthly day rules and leap‑year rules. |
| 17 | + * It provides a normalized `toString()` output: |
| 18 | + * - full date → YYYY-MM-DD |
| 19 | + * - year‑month → YYYY-MM |
| 20 | + * - year only → YYYY |
| 21 | + * |
| 22 | + * Example: |
| 23 | + * const d = IsoDate.parse('2025-01-01'); |
| 24 | + * console.log(d.year, d.month, d.day); // 2025 1 1 |
| 25 | + * console.log(d.toString()); // "2025-01-01" |
| 26 | + * |
| 27 | + * AI Usage: |
| 28 | + * - This code was originally generated using |
| 29 | + * 1. OpenAI/GPT OSS 120b on Roo Code |
| 30 | + * 2. Gemini 2.5 Flash and 2.5 Pro |
| 31 | + * then modified to fix incorrect implementations and fit project needs. |
| 32 | + * The first commit contains these corrections so that all code committed |
| 33 | + * works as designed. |
| 34 | + */ |
| 35 | + |
| 36 | +export class IsoDate { |
| 37 | + |
| 38 | + /** Full year (e.g., 2025) */ |
| 39 | + public readonly year: number; |
| 40 | + /** Month number 1‑12 (optional) */ |
| 41 | + public readonly month?: number; |
| 42 | + /** Day number 1‑31 (optional, requires month) */ |
| 43 | + public readonly day?: number; |
| 44 | + |
| 45 | + protected constructor(year: number, month?: number, day?: number) { |
| 46 | + this.year = year; |
| 47 | + if (month !== undefined) this.month = month; |
| 48 | + if (day !== undefined) this.day = day; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Parse a string into a IsoDate. |
| 53 | + * Throws an Error if the string does not match any supported format |
| 54 | + * or if the date components are out of range. |
| 55 | + */ |
| 56 | + public static parse(value: string): IsoDate { |
| 57 | + // Regex with named capture groups for clarity. |
| 58 | + // 1. YYYY‑MM‑DD |
| 59 | + // 2. we do not allow YYYYMMDD anymore |
| 60 | + // 3. YYYY‑MM |
| 61 | + // 4. YYYY |
| 62 | + const regex = |
| 63 | + // GPT OSS 120b generated regex |
| 64 | + // /^(?<year>\d{4})(?:[-]?(?<month>\d{2})(?:[-]?(?<day>\d{2})?)?)?$/; |
| 65 | + /^(?<year>\d{4})(?:[-](?<month>\d{2})(?:[-](?<day>\d{2})?)?)?$/; |
| 66 | + |
| 67 | + const match = regex.exec(value); |
| 68 | + if (!match || !match.groups) { |
| 69 | + throw new Error(`Invalid calendar date format: "${value}": must be one of YYYY-MM-DD, YYYY-MM, or YYYY`); |
| 70 | + } |
| 71 | + |
| 72 | + const year = Number(match.groups.year); |
| 73 | + const monthStr = match.groups.month; |
| 74 | + const dayStr = match.groups.day; |
| 75 | + |
| 76 | + // Validate year range (reasonable limits) |
| 77 | + if (year < 1 || year > 2500) { |
| 78 | + throw new Error(`Year out of range: ${year}`); |
| 79 | + } |
| 80 | + |
| 81 | + // If month is present, validate it. |
| 82 | + if (monthStr !== undefined) { |
| 83 | + const month = Number(monthStr); |
| 84 | + if (month < 1 || month > 12) { |
| 85 | + throw new Error(`Month out of range: ${monthStr}`); |
| 86 | + } |
| 87 | + |
| 88 | + // If day is present, validate day according to month & leap year. |
| 89 | + if (dayStr !== undefined) { |
| 90 | + const day = Number(dayStr); |
| 91 | + const maxDay = IsoDate.daysInMonth(year, month); |
| 92 | + if (day < 1 || day > maxDay) { |
| 93 | + throw new Error( |
| 94 | + `Day out of range for ${year}-${String(month).padStart( |
| 95 | + 2, |
| 96 | + '0' |
| 97 | + )}: ${dayStr}` |
| 98 | + ); |
| 99 | + } |
| 100 | + return new IsoDate(year, month, day); |
| 101 | + } |
| 102 | + |
| 103 | + // Month only (no day) |
| 104 | + return new IsoDate(year, month); |
| 105 | + } |
| 106 | + |
| 107 | + // Year only |
| 108 | + return new IsoDate(year); |
| 109 | + } |
| 110 | + |
| 111 | + /** Return true if the stored year is a leap year. */ |
| 112 | + public isLeapYear(): boolean { |
| 113 | + return IsoDate.isLeapYear(this.year); |
| 114 | + } |
| 115 | + |
| 116 | + /** Return true if the stored year is a leap year. */ |
| 117 | + public isYear(): boolean { |
| 118 | + return this.toString().length === 4; |
| 119 | + } |
| 120 | + |
| 121 | + /** Return true if the stored year is a leap year. */ |
| 122 | + public isMonth(): boolean { |
| 123 | + return this.toString().length === 7; |
| 124 | + } |
| 125 | + |
| 126 | + /** Return true if the stored year is a leap year. */ |
| 127 | + public isDate(): boolean { |
| 128 | + return this.toString().length === 10; |
| 129 | + } |
| 130 | + |
| 131 | + |
| 132 | + /** Normalized string representation. */ |
| 133 | + public toString(): string { |
| 134 | + const y = String(this.year).padStart(4, '0'); |
| 135 | + if (this.month !== undefined) { |
| 136 | + const m = String(this.month).padStart(2, '0'); |
| 137 | + if (this.day !== undefined) { |
| 138 | + const d = String(this.day).padStart(2, '0'); |
| 139 | + return `${y}-${m}-${d}`; |
| 140 | + } |
| 141 | + return `${y}-${m}`; |
| 142 | + } |
| 143 | + return y; |
| 144 | + } |
| 145 | + |
| 146 | + /** Static function that returns true iff year is a leap‑year */ |
| 147 | + public static isLeapYear(year: number): boolean { |
| 148 | + return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; |
| 149 | + } |
| 150 | + |
| 151 | + /** Static function that returns number of days in a given month/year. */ |
| 152 | + public static daysInMonth(year: number, month: number): number { |
| 153 | + switch (month) { |
| 154 | + case 2: |
| 155 | + return IsoDate.isLeapYear(year) ? 29 : 28; |
| 156 | + case 4: |
| 157 | + case 6: |
| 158 | + case 9: |
| 159 | + case 11: |
| 160 | + return 30; |
| 161 | + default: |
| 162 | + return 31; |
| 163 | + } |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +/* Utility function to check if a string is a valid ISO date according to IsoDate parsing rules |
| 168 | + */ |
| 169 | +export function isValidIsoDate(value: string): boolean { |
| 170 | + try { |
| 171 | + IsoDate.parse(value); |
| 172 | + return true; |
| 173 | + } catch { |
| 174 | + return false; |
| 175 | + } |
| 176 | +} |
0 commit comments