Skip to content

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

Merged
afc163 merged 3 commits intoreact-component:masterfrom
daedalus28:fix/datefns-localeParse-ampm
Apr 29, 2026
Merged

fix: add A → a replacement in dateFns localeParse for use12Hours#965
afc163 merged 3 commits intoreact-component:masterfrom
daedalus28:fix/datefns-localeParse-ampm

Conversation

@daedalus28
Copy link
Copy Markdown
Contributor

@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 标记大写 A 视为小写 a 进行处理,提升日期解析与格式化的一致性与稳定性。
  • Tests

    • 新增针对 date-fns 的单元测试,覆盖大写 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
Contributor

coderabbitai Bot commented Apr 17, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5fb8ede1-2135-4b94-aae8-76c25816e7b7

📥 Commits

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

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

Walkthrough

localeParse 的格式预处理链中新增对 AM/PM 令牌的正规化:将所有大写 A 替换为小写 a;同时新增 date-fns 专用单元测试,验证在含大写 A 的 12 小时制格式下格式化与解析正常工作(验证输出 PM 且能解析回 14:30)。

Changes

Cohort / File(s) Summary
日期格式转换修复
src/generate/dateFns.ts
localeParse 的链式替换中添加 .replace(/A/g, 'a'),将 moment 风格的大写 AM/PM 令牌转换为 date-fns 期望的小写 a,避免未转义字母导致的解析错误。
新增/更新测试
tests/generate.spec.tsx
新增 date-fns 专用测试,使用 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 The title accurately describes the main change: adding A → a replacement in dateFns localeParse for use12Hours support.
Linked Issues check ✅ Passed The code changes fully implement the fix specified in issue #964: adding .replace(/A/g, 'a') to localeParse and including a test case verifying 12-hour format functionality.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the A → a token replacement issue in dateFns localeParse; no out-of-scope modifications detected.
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
Review rate limit: 0/1 reviews remaining, refill in 60 minutes.

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 (935095a).
⚠️ Report is 1 commits behind head on master.

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
Contributor Author

Could you add a test case?

Done 👍

Copy link
Copy Markdown
Contributor

@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 Outdated
@daedalus28
Copy link
Copy Markdown
Contributor Author

@afc163 Is there anything you need for this? This is a real issue affecting everyone using date-fns with a 12 hour picker.

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@afc163 afc163 merged commit 30c896c into react-component:master Apr 29, 2026
5 of 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.

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

2 participants