Skip to content

Pass Locale.US to Clock date parsing and formatting in Dates.java#3988

Open
apexpdl wants to merge 1 commit into
mit-cml:ucrfrom
apexpdl:fix/clock-date-locale
Open

Pass Locale.US to Clock date parsing and formatting in Dates.java#3988
apexpdl wants to merge 1 commit into
mit-cml:ucrfrom
apexpdl:fix/clock-date-locale

Conversation

@apexpdl

@apexpdl apexpdl commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

What does this PR accomplish?

Description

Android half of #3984. The iOS half is #3989.

Dates.java creates SimpleDateFormat instances without a Locale in three places: tryParseDate (which backs Clock.MakeInstant), FormatDateTime, and FormatDate. On a Thai-locale device, SimpleDateFormat resolves against the Buddhist calendar, so Clock.MakeInstant("06/10/2026") reads 2026 as a Buddhist Era year and returns Gregorian year 1483. Any app that parses a saved or hardcoded date string silently gets an instant about 543 years in the past. Formatting has the mirror problem, which also breaks round-trips (format a date, store it, parse it later).

This PR passes Locale.US to all three, the same approach approved and merged in #3891 for the Chart components. The documented format strings (MM/dd/yyyy, HH:mm, etc.) fully specify component order, so the locale only pins the calendar and digit script. It does not change date layout for anyone.

It also fixes a debuggability problem in the same function: tryParseDate swallowed ParseException with an empty catch, so a failed parse threw IllegalArgumentException with no detail. The exception now includes the input value and carries the last parse error as its cause.

WeekdayName/MonthName style functions are intentionally untouched. They use locale display names by design and should keep doing so.

Testing Guidelines

  1. Set an Android device or emulator to Thai (Settings > System > Languages > Thai).
  2. In a test app, call Clock.MakeInstant("06/10/2026") then Clock.Year(...) on the result.
  3. Before this PR: 1483. After this PR: 2026.
  4. Round-trip check: Clock.FormatDate an instant, parse it back with Clock.MakeInstant, confirm the same instant on any locale.
  5. Regression on a US-locale device: all Clock blocks behave identically to before.

Addresses the Android portion of #3984.

Context for the changes

This is Android Companion runtime code, so it targets the ucr branch per the contributing guidelines.

If your code changes how something works on the device (i.e., it affects the companion):

  • I have made no changes that affect the companion

  • I branched from ucr

  • My pull request has ucr as the base

For all other changes:

  • I have made no changes that affect the master branch

  • I branched from master

  • My pull request has master as the base

General items:

  • I have updated the relevant documentation files under docs/
  • My code follows the:
  • ant tests passes on my machine

Dates.java created SimpleDateFormat instances without a Locale in
three places: tryParseDate (backs Clock.MakeInstant), FormatDateTime,
and FormatDate. On devices set to a Thai locale, SimpleDateFormat
resolves against the Buddhist calendar, so MakeInstant("06/10/2026")
reads 2026 as a Buddhist Era year and returns Gregorian year 1483.
Apps that parse stored or hardcoded date strings silently get
instants ~543 years in the past. Formatting has the mirror problem.

This passes Locale.US to all three, following the approach approved
and merged in mit-cml#3891 for the Chart components. The documented format
strings fully specify component order, so the locale only pins the
calendar and digit script.

Also surfaces the underlying ParseException: tryParseDate previously
swallowed it with an empty catch, so a failed parse threw an
IllegalArgumentException with no detail. The exception now includes
the input value and the last parse error as its cause.

WeekdayName/MonthName style functions are intentionally untouched;
they use locale display names by design.

Addresses the Android half of mit-cml#3984.
@AppInventorWorkerBee

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

1 similar comment
@AppInventorWorkerBee

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

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.

2 participants