* Asserts that the given date format is free from common mistakes. Throws an * error if one is found (except for the case of all "Y", in which case we just * log a warning). This should only be called in development mode.
(parts: string[])
| 150 | * log a warning). This should only be called in development mode. |
| 151 | */ |
| 152 | function assertValidDateFormat(parts: string[]) { |
| 153 | if (parts.some((part) => /^Y+$/.test(part)) && !parts.some((part) => /^w+$/.test(part))) { |
| 154 | // "Y" indicates "week-based year", which differs from the actual calendar |
| 155 | // year for a few days around Jan 1 most years. Unless "w" is also |
| 156 | // present (e.g. a date like "2024-W52") this is likely a mistake. Users |
| 157 | // probably meant "y" instead. |
| 158 | const message = `Suspicious use of week-based year "Y" in date pattern "${parts.join( |
| 159 | '', |
| 160 | )}". Did you mean to use calendar year "y" instead?`; |
| 161 | if (parts.length === 1) { |
| 162 | // NOTE: allow "YYYY" with just a warning, since it's used in tests. |
| 163 | console.error(formatRuntimeError(RuntimeErrorCode.SUSPICIOUS_DATE_FORMAT, message)); |
| 164 | } else { |
| 165 | throw new RuntimeError(RuntimeErrorCode.SUSPICIOUS_DATE_FORMAT, message); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Create a new Date object with the given date value, and the time set to midnight. |
no test coverage detected
searching dependent graphs…