Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/generate/dateFns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ const localeParse = (format: string) => {
.replace(/D/g, 'd')
.replace(/gggg/, 'yyyy')
.replace(/g/g, 'G')
.replace(/([Ww])o/g, 'wo');
.replace(/([Ww])o/g, 'wo')
.replace(/A/g, 'a');
};

const parse = (text: string, format: string, locale: string) => {
Expand Down
15 changes: 15 additions & 0 deletions tests/generate.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,19 @@ describe('Generate:date-fns', () => {
expect(dateFnsGenerateConfig.locale.getWeekFirstDay('it_IT')).toEqual(1);
expect(dateFnsGenerateConfig.locale.getWeekFirstDay('fr_FR')).toEqual(1);
});

it('format and parse with AM/PM (uppercase A)', () => {
const date = new Date(2000, 0, 1, 14, 30, 0);

// 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');
Comment on lines +342 to +344
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot Apr 19, 2026

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please check this one.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@afc163, the inconsistency is confirmed. The comment on Line 342 says "should produce lowercase am/pm output," but the assertion on Line 344 expects uppercase 'PM'. This is misleading because:

  • The actual fix maps the moment-style token A → date-fns token a
  • date-fns's a token renders uppercase AM/PM in the en_US locale

The comment incorrectly conflates token-level normalization (uppercase A → lowercase a) with the formatted output casing (which remains uppercase). The suggested fix makes the intent clearer:

-    // 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');


// Parse with uppercase A should also work without throwing
const parsed = dateFnsGenerateConfig.locale.parse('en_US', '2000-01-01 02:30:00 PM', [
'YYYY-MM-DD hh:mm:ss A',
]);
expect(dateFnsGenerateConfig.getHour(parsed)).toEqual(14);
expect(dateFnsGenerateConfig.getMinute(parsed)).toEqual(30);
});
});
Loading