Skip to content

Fix time zone edge cases, addMonths, and isSameDay; match numeral-script literal digits - #157

Merged
knowledgecode merged 14 commits into
masterfrom
develop
Dec 4, 2026
Merged

knowledgecode merged 14 commits into
masterfrom
develop

Conversation

@knowledgecode

Copy link
Copy Markdown
Owner

Summary

Release 4.9.0: fixes for dates at the edges of the calendar with a time zone, addMonths day clamping, and isSameDay with Invalid Dates; numeral-script digits in pattern literals; internal refactors and a documentation pass.

Feature

  • parse: with a numeral system other than latn, a digit in literal text (bracketed or not) matches the same digit in Latin or that system's script, so parse() reads back what format() writes for patterns such as '[Q٣] YYYY'. preparse() positions (_index, _length) count the decoded string, and [] matches as an empty literal

Fixes

  • Time zones: years before 1 CE keep their sign (format printed 6 for -5), addYears no longer throws on reaching year 0, and dates at the ends of the Date range no longer give a NaN offset, a garbled date, or Invalid Date. fromParts and dtfToParts work through 400-year cycles
  • addMonths: clamp the day against the truncated target month before setting it, so 1.5 months after January 31 with a time zone gives February 28 (was March 31), and a rollover into a DST gap no longer keeps the gap's hour shift in local time
  • isSameDay: return false when either date is invalid; two Invalid Dates used to give true. Documented in the API reference, the migration guide, and the migration skill

Refactors

  • Reuse one global RegExp per numeral system; skip the literal regex for tokens without a leading bracket in format and Duration; simplify the addDays UTC branch, the parse hour precedence, the getDateTimeFormat cache, and getISOWeek

Build / tooling

  • Emit .d.ts and .d.cts from one Rollup pass per entry; the build spec now covers the timezone modules' CJS interop and .d.cts
  • Remove redundant ESLint and Vitest settings
  • Update devDependencies

Tests

  • New: time zone edge cases for format, parse, addDays, addYears, addMonths; numeral-script literals; shorter input to the microsecond/nanosecond S tokens; isSameDay with Invalid Dates
  • Tidy: locale specs share their cases through tests/locales/helper.ts; timezone tests check zone_name against the module path

Docs

  • Describe how the microsecond/nanosecond S tokens set milliseconds (they were documented as read and discarded); skill references updated to match
  • Correct the minimum browsers (Firefox 84+, Safari 14.1+) and TypeScript (4.7+; bundler needs 5.0), and the LMT offset in a parse output
  • Write internal links as site-absolute /date-and-time/<route> paths

Test plan

  • npm run lint, npm test pass
  • npm run build && npx vitest run tests/build passes
  • Each commit passes tsc, ESLint, and Vitest on its own
  • The fixed cases were reproduced against the v4.8.0 sources before the fix

Drop the @typescript-eslint/no-extraneous-class override and the prefer-promise-reject-errors and @stylistic/padding-line-between-statements rules, and the Vitest fsModuleCache and isolate options, leaving include as the only test setting.
Give each type bundle two outputs, the second renaming to .d.cts and rewriting the default export to export =, instead of a second config per entry, and drop the esbuild minify: false option, which is its default. Extend the build spec to the timezone modules' CJS interop and .d.cts output.
Test the zone name directly in the UTC branch of addDays, cache every recognized zone in getDateTimeFormat, reuse one global RegExp per numeral system for decode, check for a leading bracket before matching a literal in format and Duration, state the hour precedence of parse as one condition, and drop the Math.round that the whole-week difference in getISOWeek never needed.
With a time zone, a year before 1 CE lost its sign (format printed 6 for -5 with a garbled offset, and parsing January 1, 0001 in Tokyo gave a date in year -1), addYears threw on reaching year 0, and dates at the ends of the Date range gave a NaN offset, a garbled date, or Invalid Date. fromParts now passes Date.UTC() only the month within a 400-year cycle and adds the rest as days, dtfToParts moves a time outside 1600-2399 by whole 400-year cycles before formatToParts and restores the year, getTimezoneOffset searches any local time within a day of the Date range, and format detects an Invalid Date from the time instead of the year.
With a time zone, a fractional month moved the day before it was clamped, so 1.5 months after January 31 gave March 31 instead of February 28; in local time, a month that rolled over into a DST gap kept the gap's hour shift after stepping back with setDate(0). Compute the last day of the truncated target month first and set the month and day together in local time, UTC, and a time zone alike, using a 400-year cycle so the lookup also holds for years 0-99, before 1 CE, and at the ends of the Date range.
With a numeral system other than latn, a digit in literal text, bracketed or not, now matches the same digit in either Latin or the numeral system's script, so parse() reads back what format() writes for a pattern such as '[Q٣] YYYY'. preparse() decodes the input once up front, so _index and _length count positions in the decoded string, and an empty pair of brackets matches as an empty literal.
isSameDay compared toDateString() results, which are both 'Invalid Date' for two Invalid Dates, so it returned true. It now returns false whenever either date is invalid. Document the change in the API reference, the migration guide, and the migration skill.
Move the format and parse cases repeated in every locale spec into tests/locales/helper.ts, so each spec lists only its locale's strings and calls testLocale.
Name each timezone test after the module's path under src/timezones/ and assert that zone_name equals it, instead of only checking that it is a string.
…econds

The S tokens of the microsecond and nanosecond plugins set the milliseconds and drop the extra digits, reading shorter input as if it had leading zeros, while the f and F tokens only skip their digits; the docs said every token was read and discarded. Correct the plugin guide and the skill references, and cover the shorter-input case in the plugin specs.
List Firefox 84+ and Safari 14.1+ as the minimum browsers and TypeScript 4.7+ as the minimum for the package's exports-based types, note that moduleResolution bundler needs TypeScript 5.0, and show the local mean time offset (GMT-0752) that Date prints for January 1, 0001 in Los Angeles.
Replace relative links such as ./format and ../plugins with /date-and-time/<route> paths in lowercase, which resolve under the site base from every page.
@knowledgecode
knowledgecode merged commit 3d8ef5a into master Dec 4, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant