Skip to content

fix(shared): derive the day period from the hour, not a localized string - #2958

Merged
zernonia merged 1 commit into
v2from
fix/day-period-locale-2956
Sep 21, 2026
Merged

zernonia merged 1 commit into
v2from
fix/day-period-locale-2956

Conversation

@zernonia

@zernonia zernonia commented Sep 21, 2026 •

Copy link
Copy Markdown
Member

🔗 Linked issue

Resolves #2956

❓ Type of change

  • 📖 Documentation (updates to the documentation, readme or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

useDateFormatter().dayPeriod() matched the formatted day period against PM / pm / p.m. and fell back to AM for anything else. Locales that render it differently (p. m. in es-ES, 午後 in ja-JP, 下午 in zh-CN, 오후 in ko-KR, …) therefore always resolved to AM. With hour-cycle="12", an afternoon value displayed as AM, and retyping the hour silently moved the time back twelve hours (15:30 → 03:30).

Fix: read the hour on a 24-hour clock and compare it against 12, the same way handleHourSegmentKeydown and handleSegmentFocusOut already derive the token. This was the only place in src that string-matched the day period.

  • hourCycle: 'h23' and numberingSystem: 'latn' are pinned so the hour always parses as a Latin-digit 0–23 value (covers ar-EG, fa-IR, …).
  • hour12 is cleared explicitly. It takes precedence over hourCycle in Intl, so a caller passing hour12: true to useDateFormatter would otherwise get every afternoon back as AM.
  • The public Formatter.dayPeriod signature is unchanged.

Because the day period flows through the shared syncTimeSegmentValues, this fixes DateField, DateRangeField, TimeField and TimeRangeField together.

Tests

  • New useDateFormatter.test.ts: 15 locales × 5 hours, every hourCycle / hour12 option, a non-Gregorian calendar, opts.timeZone, and setLocale. Everything is pinned to UTC, so results don't depend on the runner's zone.
  • DateField and TimeField: render and hour-retype cases across the affected locales.
  • 49 of the new tests fail on v2 and all pass with the fix. The full suite passes (2252 tests), along with type-check (test files included), build and lint.

Relation to #2953

#2953 has since merged. It added a timeZone parameter to the same function for foreign-zone ZonedDateTime values. This branch is rebased on it and keeps that parameter, with a unit test for it, so both fixes stay in place.

Not in scope

The segment still shows the AM / PM token in every locale rather than a localized string such as 午後. That comes from the public DayPeriod type and the placeholder design, not from this bug, and changing it would alter output for every non-English locale.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Fixed 12-hour time formatting across multiple locales so afternoon times are correctly identified as PM, including locales with localized day-period text.
    • Fixed Date and Time fields so editing afternoon hours preserves the PM indicator and the correct underlying time.
    • Improved consistency when using custom time zones, locale changes, and different hour-formatting options.

@coderabbitai

coderabbitai Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

Understand this PR’s impact

Explore downstream dependencies and potential security impact with Blast Radius.

View blast radius →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository: unovue/reka-ui/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: 8f428390-b53a-4833-8176-f68bff33ef6b

📥 Commits

Reviewing files that changed from the base of the PR and between 72a3a1d and ad2495f.

📒 Files selected for processing (4)
  • packages/core/src/DateField/DateField.test.ts
  • packages/core/src/TimeField/TimeField.test.ts
  • packages/core/src/shared/useDateFormatter.test.ts
  • packages/core/src/shared/useDateFormatter.ts

Included review availability: Your plan provides up to 8 included reviews per hour; 4 remain after this review.


📝 Walkthrough

Walkthrough

The formatter now derives AM/PM from a fixed 24-hour value. Tests cover localized formats, formatter options, time zones, locale changes, DateField, and TimeField hour editing.

Changes

Day-period handling

Layer / File(s) Summary
Numeric day-period derivation
packages/core/src/shared/useDateFormatter.ts, packages/core/src/shared/useDateFormatter.test.ts
dayPeriod uses an h23 Latin-digit formatter and returns PM for hours from 12 through 23. Tests cover locales, options, time zones, and locale changes.
Date and time field regression coverage
packages/core/src/DateField/DateField.test.ts, packages/core/src/TimeField/TimeField.test.ts
Parameterized tests verify afternoon and morning day periods across locales. Retyping an afternoon hour preserves the PM period and the 15:30 value.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 4 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Issue #2956 requires locale-independent AM/PM resolution and correct PM preservation when the user retypes the hour. useDateFormatter().dayPeriod() now reads a pinned h23 hour with hour12 cleare…
Out of Scope Changes check ✅ Passed The changed source code fixes the day-period derivation for issue #2956. The added tests verify the same behavior across the formatter and affected field paths. No unrelated production behavior or unr…
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: deriving the day period from the numeric hour instead of localized strings.
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR

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.

`useDateFormatter`'s `dayPeriod` matched the formatted day period against
`PM`/`pm`/`p.m.` and fell back to `AM` for anything else. Locales that render
it in another form always resolved to `AM`, so an afternoon value displayed as
AM and retyping the hour silently moved the time back twelve hours.

Read the hour on a pinned 24-hour, Latin-digit clock and compare it against 12
instead, which is how `handleHourSegmentKeydown` and `handleSegmentFocusOut`
already derive the token. `hour12` is cleared explicitly because it takes
precedence over `hourCycle` in `Intl`.

This covers DateField, DateRangeField, TimeField and TimeRangeField, which all
resolve the day period through the shared `syncTimeSegmentValues`.

Closes #2956

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@zernonia
zernonia force-pushed the fix/day-period-locale-2956 branch from 3e23a3e to ad2495f Compare September 21, 2026 12:28
@pkg-pr-new

pkg-pr-new Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/reka-ui@2958

commit: ad2495f

@zernonia
zernonia merged commit a32396e into v2 Sep 21, 2026
8 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.

[Bug]: Day period resolves to AM for locales whose day period isn't "PM"/"pm"/"p.m."

1 participant