Repository navigation
Fix getDateTimeLocalValue() for epoch numbers and Date values - #5397
Conversation
heath-freenome
left a comment
There was a problem hiding this comment.
Thanks for picking this up! The date-time path is right. antd, mantine and daisyui now receive a UTC ISO string, which they parse as an instant and display in local time. The invalid-input cases (NaN, Infinity, out-of-range numbers, invalid Dates) are covered, and the CHANGELOG entry is in the right place.
One note on the issue itself: core's DateTimeWidget only reads localValue for iso-date-time. For date-time it passes the raw value to utcToLocal(), which already handled numbers and Dates. So in core only iso-date-time fields were rendering empty.
The inline comment is the one change needed. For iso-date-time, an epoch or Date is currently shown at its UTC time as if it were local, so editing the field saves a time that is off by the user's UTC offset.
Also:
- CI hasn't run on this PR yet, because the workflows need a maintainer's approval for a first-time contributor.
- There's a trivial conflict in
CHANGELOG-v7.mdwithv7.
| stringValue = value; | ||
| } else if (typeof value === 'number' || value instanceof Date) { | ||
| const date = new Date(value); | ||
| stringValue = Number.isNaN(date.getTime()) ? undefined : date.toISOString(); |
There was a problem hiding this comment.
This is right for date-time, but not for iso-date-time. An epoch number or a Date is an exact moment in time, and toISOString() gives that moment's UTC wall-clock time. offsetTimeToLocalTime() then only strips the trailing Z; it doesn't convert anything. Every theme treats an iso-date-time localValue as local time (daisyui's commitValue calls it a "naive local date-time string"). So the field shows the UTC time as if it were local. In America/Los_Angeles:
stored instant (UTC) : 2020-05-03T14:30:00.000Z
this PR (iso-date-time) : 2020-05-03T14:30:00.000
...read back as local time: 2020-05-03T21:30:00.000Z <- 7h later than stored
utcToLocal() would give : 2020-05-03T07:30:00.000
...read back as local time: 2020-05-03T14:30:00.000Z
The field shows 14:30 for a moment that is 07:30 locally. If the user edits just the day, the value saved back is 7 hours off. That's the same silent replacement of a stored instant that #5393 describes, just harder to notice than an empty field.
For iso-date-time, a number or Date needs the local wall-clock time instead, which utcToLocal() already produces in the same yyyy-MM-ddThh:mm:ss.SSS shape. For example:
} else if (typeof value === 'number' || value instanceof Date) {
const date = new Date(value);
if (!Number.isNaN(date.getTime())) {
const iso = date.toISOString();
return { isIsoDateTime, localValue: isIsoDateTime ? utcToLocal(iso) : iso };
}
}The two new iso-date-time tests currently expect the UTC time, and they pass in any time zone only because nothing in them depends on the local zone. With this change they'd depend on it, so they'd need a pinned zone, like the helper in packages/daisyui/test/helpers/pinTimeZone.ts. Ideally pin a zone that isn't UTC, so the test actually catches this.
(A string value that includes a Z or an offset also has its offset stripped without conversion under iso-date-time. That behavior predates this PR and is out of scope here.)
There was a problem hiding this comment.
Done in 79a94a0: iso-date-time now shows an epoch or Date at local wall-clock time via utcToLocal(), the tests pin America/Los_Angeles so they catch it, and the CHANGELOG conflict with v7 is resolved.
Size limit reportSizes are minified + brotli, measured by size-limit at A package listed twice is measured both as installed and, on the second row, as its own code with its dependencies excluded. Peer dependencies are always excluded.
|
da186f9 to
79a94a0
Compare
heath-freenome
left a comment
There was a problem hiding this comment.
Thanks for the quick turnaround. The iso-date-time fix is exactly what I was after, and pinning America/Los_Angeles in the tests means they'll catch it if it regresses.
A few things are still open:
- The CHANGELOG conflicts with
v7again. It was resolved when you pushed, but since then #5381, #5386, #5400 and #5364 have added bullets at the end of the## @rjsf/utilssection, andgit merge-treereports a conflict there. Please rebase and put the #5393 bullet after them. - The comment in
packages/daisyui/src/widgets/DateTimeWidget/DateTimeWidget.tsx(around line 80) is now wrong. It's outside this diff, so I'm noting it here. It says a non-string value givesgetDateTimeLocalValue()"nothing … to return", but after this PR it returns a string for every finite number and validDate. On that path thelocalValue ?? valuefallback, andreadInstant's handling of numbers andDates, now only ever get values that are rejected anyway. Please update the comment, and drop the fallback if nothing else needs it.
79a94a0 to
7e90b81
Compare
|
Rebased onto current v7 with the #5393 changelog bullet after the four new ones; updated the DateTimeWidget comment and dropped the |
7e90b81 to
64f0217
Compare
heath-freenome
left a comment
There was a problem hiding this comment.
Thanks for the rebase and the daisyui cleanup. The corrected comment reads right, and dropping the localValue ?? value fallback is safe. readInstant still needs its number and Date handling for daisyui's DateWidget, and the existing epoch and Date cases in DateTimeWidget.test.tsx now go through getDateTimeLocalValue() and pass.
The three inline comments from my last review are still open, and I've replied on each:
- the
requiresOffsetchange, including a correction to how the bug shows up in Mantine - the JSDoc
- the CHANGELOG wording, which also conflicts with
v7again
Two smaller things:
- The PR description still says a number or
Dategoes "through the same offset handling as a string value". Foriso-date-timeit's now converted to local time instead, and therequiresOffsetchange will extend that to every format exceptdate-time/datetime. - Optional:
getDateTimeLocalValue()could return early for a string, then handle a number orDate, then returnundefined, instead of carryingstringValuethrough to a second check.
…teTimeLocalValue() getDateTimeLocalValue() returned an undefined localValue for anything but a string, so a date-time field holding an epoch number or a Date rendered empty. A finite number or a valid Date now becomes its ISO string, then goes through the same iso-date-time offset strip as a string value. An invalid Date, a non-finite or out-of-range number, and any other non-string value still give undefined.
…me; pin the test zone
…lt only; correct the comment
64f0217 to
1fbbf8b
Compare
|
Thanks, all four are done. |
heath-freenome
left a comment
There was a problem hiding this comment.
Thanks, this round covers everything I asked for:
- the
requiresOffsetsplit - the tests for a field with no format and one with
format: 'date' - the JSDoc and the CHANGELOG
- the early return and the updated description
The conflict with v7 is gone too.
Re-reviewing the new head turned up a few more things. Two of them come from changes I asked for, so sorry for the extra round:
- Inline comments:
format: 'date'now shows different days in different themes- the hour when clocks go back in daisyui
- the daisyui comment
- years outside 0–9999 (minor)
- Docs:
packages/docs/docs/api-reference/utility-functions.md(line 762) repeats the function's JSDoc, and still describes only the offset stripping. Please add the new paragraph about epoch numbers andDates there too. - Optional: Mantine's
DateTimeInput.tsx(line 79) repeats the samedate-time/datetimecheck. Returning it as arequiresOffsetfield onDateTimeLocalValueResultwould keep the two from drifting apart. This is fine as a follow-up.
… requiresOffset - format: date reads an epoch or Date at UTC midnight as the day it names, matching daisyui - an epoch or Date whose local year is outside 0-9999 gives undefined - daisyui reads a non-text value as an instant, so the repeated 01:30 is kept - DateTimeLocalValueResult returns requiresOffset, which mantine now uses - document epoch numbers and Dates in the utility-functions reference
|
Thanks, all in the new commit. Under |
heath-freenome
left a comment
There was a problem hiding this comment.
Thanks, this round covers everything from my last review:
- the UTC-midnight rule for
format: 'date' - the 0–9999 year guard
- reading an instant exactly in daisyui, with a test for the night clocks go back
- the rewritten comment
- the docs paragraph
- sharing
requiresOffsetwith Mantine
The code is nearly there. Two small requests and two optional nits are inline.
The PR description needs another pass too:
- It still says every format except
date-time/datetimegets local wall-clock time, which leaves out theformat: 'date'UTC-midnight rule. - It says the daisyui
DateTimeWidget"now reads only thegetDateTimeLocalValue()result". That's no longer true, since it reads a number orDateas the instant it is.
…fsetValueParser, update CHANGELOG
|
Done: the CHANGELOG bullet uses your text, the |
heath-freenome
left a comment
There was a problem hiding this comment.
Thanks, the CHANGELOG, the year guard and its tests, and offsetValueParser(localValue) all look right. The useMemo reason makes sense too, so please keep it.
Two things outside the inline comments:
- The
sizecheck fails.@rjsf/utils (without dependencies)is now 21.01 kB, over its 21 kB budget. Please bumpownfor@rjsf/utilsto'22 kB'in.github/size-limit/.size-limit.js. The base was already at 20.88 kB, so this is the natural point to raise it. - The PR description still needs updating. It says every format except
date-time/datetimegets local wall-clock time, which leaves out theformat: 'date'UTC-midnight rule. It also says daisyui "reads only thegetDateTimeLocalValue()result".
Inline:
- You were right about the hour when clocks go back. That was my mistake, so I'm suggesting you revert that part.
- One more gap: Mantine fields with a custom
valueFormat.
|
@drakeo338 still a few comments awaiting responses |
…n Mantine without valueFormat
heath-freenome
left a comment
There was a problem hiding this comment.
Thanks, this round looks good. The daisyui revert, dropping the test, and the Mantine valueFormat fix with its test are exactly what I was after, and the code is ready from my side.
One thing left before I approve:
- The PR description: it still says "every other format, including
iso-date-time" gets local wall-clock time. That leaves out theformat: 'date'rule, where an epoch orDateat UTC midnight comes back as the day it names. (The line about daisyui reading only thegetDateTimeLocalValue()result is accurate again after the revert.)
Two optional nits, fine to skip:
- The new Mantine test passes in any zone, so it covers the
valueFormatfix. Pinning a zone west of UTC, such asAmerica/Los_Angeles, would also make it catch a UTC-midnight day shift in Mantine. - Mantine now has its own change for non-string values, so a short bullet under
## @rjsf/mantineinCHANGELOG-v7.mdwouldn't hurt, though the utils bullet arguably covers it.
I'll approve once the description is updated and the remaining build jobs pass.
|
Updated the description with the |
|
@drakeo338 Thanks for working with me to get this PR over the line |
Reasons for making this change
Fixes #5393.
getDateTimeLocalValue()returned an undefinedlocalValuefor anything that was not a string, so a date-time field holding an epoch number or aDaterendered empty. A finite number or a validDateis now converted as follows:date-time/datetime: the UTC ISO string, which the themes parse as an instant.date: a UTC midnight comes back as the day it names (YYYY-MM-DD); any other instant gets local wall-clock time.iso-date-time: local wall-clock time viautcToLocal().Invalid dates, non-finite numbers and other types still give
undefined. The daisyuiDateTimeWidgetnow reads only thegetDateTimeLocalValue()result.Checklist
pnpm exec nx run-many --target=build --exclude=@rjsf/docs && pnpm run test:updateto update snapshots, if needed.Unticked items do not apply (no docs or feature change). I ran the utils, core, antd, mantine and daisyui tests plus lint; I did not run the snapshot build.