Skip to content

fix: add A → a replacement in dateFns localeParse for use12Hours#965

Open
daedalus28 wants to merge 2 commits intoreact-component:masterfrom
daedalus28:fix/datefns-localeParse-ampm
Open

fix: add A → a replacement in dateFns localeParse for use12Hours#965
daedalus28 wants to merge 2 commits intoreact-component:masterfrom
daedalus28:fix/datefns-localeParse-ampm

Conversation

@daedalus28
Copy link
Copy Markdown

@daedalus28 daedalus28 commented Apr 17, 2026

Summary

  • Add missing .replace(/A/g, 'a') to localeParse in the date-fns generate config
  • Fixes use12Hours crashing with RangeError: Format string contains an unescaped latin alphabet character A

localeParse already converts moment.js format tokens to date-fns equivalents (Y→y, D→d, etc.) but was missing A→a (AM/PM). When use12Hours is enabled, rc-picker injects uppercase A into the format string internally, which date-fns rejects.

Fixes #964

Summary by CodeRabbit

  • Bug Fixes

    • 优化了日期格式规范化,对 AM/PM 大写标记的处理进行了兼容性修正,提升了日期解析与格式化的准确性与稳定性。
  • Tests

    • 新增单元测试覆盖 AM/PM 标记(大写 A)在格式化与解析中的行为,确保输出和解析结果一致且正确。

When use12Hours is enabled, rc-picker internally injects uppercase A
(moment.js AM/PM token) into the format string. date-fns uses lowercase
a for AM/PM, so passing A through causes:

  RangeError: Format string contains an unescaped latin alphabet character `A`

localeParse already converts other moment tokens (Y→y, D→d, etc.) but
was missing A→a. This adds it.

Fixes react-component#964
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 17, 2026

@daedalus28 is attempting to deploy a commit to the React Component Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 17, 2026

Walkthrough

localeParse 的格式预处理新增对 AM/PM 令牌的正规化:把所有大写 A 替换为小写 a,并新增对应单元测试以验证格式化与解析在含 A 的 12 小时制场景下正常工作。

Changes

Cohort / File(s) Summary
日期格式转换修复
src/generate/dateFns.ts
localeParse 的链式替换中添加 .replace(/A/g, 'a'),将 moment 风格的大写 AM/PM 令牌转换为 date-fns 期望的小写 a。注意影响到使用 use12Hours 的格式化/解析流程。
新增/更新测试
tests/generate.spec.tsx
新增测试覆盖 YYYY-MM-DD hh:mm:ss A 格式:验证格式化输出含 PM,并能成功解析回正确的小时(14)和分钟(30)。

Sequence Diagram(s)

(本次变更属于小幅格式化修复与测试,交互组件不足以生成序列图,故省略。)

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • afc163

诗句

🐰 我在格式里轻轻跳,
把大写的 A 换成小写的 a,
时针笑着回到正轨,
午后依旧温柔如常,
咕噜咕噜,解析不再慌。

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题准确反映了主要变化——在 dateFns localeParse 中添加 A → a 替换以支持 use12Hours。
Linked Issues check ✅ Passed 代码变更完全符合 issue #964 的要求:添加了缺失的 A → a 替换来修复 use12Hours 导致的 date-fns 崩溃,并添加了测试用例。
Out of Scope Changes check ✅ Passed 所有变更都在 issue #964 的范围内,包括修复 dateFns.ts 和添加相关测试,没有超出范围的变更。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the localeParse function in src/generate/dateFns.ts to replace the 'A' format token with 'a', ensuring compatibility with date-fns for 12-hour time formats. I have no feedback to provide.

@afc163
Copy link
Copy Markdown
Member

afc163 commented Apr 19, 2026

Could you add a test case?

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 19, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.81%. Comparing base (738bcac) to head (582a66b).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #965   +/-   ##
=======================================
  Coverage   98.81%   98.81%           
=======================================
  Files          65       65           
  Lines        2695     2695           
  Branches      724      749   +25     
=======================================
  Hits         2663     2663           
  Misses         29       29           
  Partials        3        3           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Verifies that localeParse converts uppercase A (moment-style AM/PM)
to lowercase a (date-fns) so that both format() and parse() work
correctly with 12-hour time formats.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@daedalus28
Copy link
Copy Markdown
Author

Could you add a test case?

Done 👍

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@tests/generate.spec.tsx`:
- Around line 342-344: The comment incorrectly states the output will be
lowercase am/pm while the assertion expects uppercase "PM"; update the comment
in tests/generate.spec.tsx near the dateFnsGenerateConfig.locale.format('en_US',
date, 'YYYY-MM-DD hh:mm:ss A') test to explain that the test verifies token
mapping/format-token behavior (that the 'A' token is handled) rather than
asserting a case change of the am/pm output, so it doesn't claim lowercase
output.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 82e058b4-cb2d-4306-bedd-a042b8039e07

📥 Commits

Reviewing files that changed from the base of the PR and between cb2f157 and 582a66b.

📒 Files selected for processing (1)
  • tests/generate.spec.tsx

Comment thread tests/generate.spec.tsx
Comment on lines +342 to +344
// Format with uppercase A (moment-style) should produce lowercase am/pm output
const formatted = dateFnsGenerateConfig.locale.format('en_US', date, 'YYYY-MM-DD hh:mm:ss A');
expect(formatted).toEqual('2000-01-01 02:30:00 PM');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

注释里的预期大小写与断言不一致。

Line 342 说会输出小写 am/pm,但 Line 344 断言的是大写 PM。建议把注释改成说明转换的是 token,而不是输出大小写,避免后续维护时误读。

建议修改
-    // Format with uppercase A (moment-style) should produce lowercase am/pm output
+    // Format with uppercase A (moment-style) should be normalized to date-fns `a`
     const formatted = dateFnsGenerateConfig.locale.format('en_US', date, 'YYYY-MM-DD hh:mm:ss A');
     expect(formatted).toEqual('2000-01-01 02:30:00 PM');
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Format with uppercase A (moment-style) should produce lowercase am/pm output
const formatted = dateFnsGenerateConfig.locale.format('en_US', date, 'YYYY-MM-DD hh:mm:ss A');
expect(formatted).toEqual('2000-01-01 02:30:00 PM');
// Format with uppercase A (moment-style) should be normalized to date-fns `a`
const formatted = dateFnsGenerateConfig.locale.format('en_US', date, 'YYYY-MM-DD hh:mm:ss A');
expect(formatted).toEqual('2000-01-01 02:30:00 PM');
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/generate.spec.tsx` around lines 342 - 344, The comment incorrectly
states the output will be lowercase am/pm while the assertion expects uppercase
"PM"; update the comment in tests/generate.spec.tsx near the
dateFnsGenerateConfig.locale.format('en_US', date, 'YYYY-MM-DD hh:mm:ss A') test
to explain that the test verifies token mapping/format-token behavior (that the
'A' token is handled) rather than asserting a case change of the am/pm output,
so it doesn't claim lowercase output.

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.

localeParse missing A → a replacement causes date-fns crash with use12Hours

2 participants