Skip to content

Fix weekday names for dates before day 0 - #112

Open
eigen-is wants to merge 1 commit into
borgar:masterfrom
eigen-is:fix/weekday-negative-serial
Open

eigen-is wants to merge 1 commit into
borgar:masterfrom
eigen-is:fix/weekday-negative-serial

Conversation

@eigen-is

Copy link
Copy Markdown

A date before day 0 (a negative serial) gets no weekday name, or the name of the day after.

import { format } from 'numfmt';
console.log(format('dddd', -7, { leap1900: false }));                      // ""
console.log(format('yyyy-mm-dd dddd', -141639, { leap1900: false }));      // "1512-03-15 "
console.log(format('yyyy-mm-dd hh:mm dddd', -0.25, { leap1900: false }));  // "1899-12-29 18:00 Saturday"

Expected: Saturday, 1512-03-15 Friday and 1899-12-29 18:00 Friday. Excel has no dates before 1900, so the reference here is Google Sheets, where =TEXT(-141639,"dddd") is Friday and =TEXT(-141500,"yyyy-mm-dd dddd") is 1512-08-01 Thursday. The day, month and year are already right; only the name is wrong.

The cause is in runPart in src/runPart.ts: the weekday is (6 + date) % 7, where date is the truncated serial. For a negative serial that remainder is negative, so the lookup in the list of names finds nothing. And a negative serial with a time truncates to the day after the one toYMD gets, so the name is one day ahead.

The change takes the weekday from the same day that is passed to toYMD, and keeps the remainder from going negative. Dates from day 0 on are unchanged.

Tests: one new test in test/datebounds.spec.ts with eight cases, two of them the Google Sheets rows above. It fails on master and passes with the change. npm test, npm run test-all, npm run lint and npm run typecheck pass.

I found this with the help of Claude Fable (an AI model), which also drafted the fix. I have reviewed and tested the change myself and take responsibility for it. Happy to adjust anything.

With `dateSpanLarge`, `format('dddd', -7, { leap1900: false })` gave an empty string, and `format('dddd', -0.25, { leap1900: false })` gave `Saturday` for a time on Friday 29 December 1899.

The weekday was the remainder of the truncated serial. For a negative serial that remainder is negative, so the lookup in the list of names found nothing, and a serial with a time truncates to the day after the one `toYMD` gets.

The weekday is now taken from the day passed to `toYMD`, with a remainder that is never negative. Dates from day 0 on are unchanged.
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