Skip to content

Commit 930d381

Browse files
kubaflosheiksyedm
authored andcommitted
Android TimePicker ignores 24 hour system setting when using Format Property - fix (#28797)
### Description of Change More generic condition for detecting the 24 hour date format ### Issues Fixed Fixes #28784
1 parent 79436ec commit 930d381

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

src/Core/src/Handlers/TimePicker/TimePickerHandler.Android.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,23 @@ void OnDialogDismiss(object? sender, EventArgs e)
156156
HidePickerDialog();
157157
}
158158

159-
bool Use24HourView => VirtualView != null && (DateFormat.Is24HourFormat(PlatformView?.Context)
160-
&& VirtualView.Format == "t" || VirtualView.Format == "HH:mm");
159+
// "HH" (uppercase) is the .NET 24-hour specifier; "hh" (lowercase) is 12-hour.
160+
// Case-sensitive Ordinal comparison is required to distinguish between them.
161+
internal static bool IsCustom24HourFormat(string? format) =>
162+
!string.IsNullOrEmpty(format) && format.Contains("HH", StringComparison.Ordinal);
163+
164+
bool Use24HourView
165+
{
166+
get
167+
{
168+
if (VirtualView is null || string.IsNullOrEmpty(VirtualView.Format))
169+
return false;
170+
171+
if (VirtualView.Format == "t")
172+
return DateFormat.Is24HourFormat(PlatformView?.Context);
173+
174+
return IsCustom24HourFormat(VirtualView.Format);
175+
}
176+
}
161177
}
162178
}

src/Core/tests/DeviceTests/Handlers/TimePicker/TimePickerHandlerTests.Android.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@ namespace Microsoft.Maui.DeviceTests
1111
{
1212
public partial class TimePickerHandlerTests
1313
{
14+
[Theory(DisplayName = "IsCustom24HourFormat detects HH patterns correctly")]
15+
[InlineData("HH:mm", true)]
16+
[InlineData("HH:mm:ss", true)]
17+
[InlineData("HH.mm", true)]
18+
[InlineData("HH-mm-ss", true)]
19+
[InlineData("hh:mm", false)]
20+
[InlineData("hh:mm tt", false)]
21+
[InlineData("h:mm", false)]
22+
[InlineData("H:mm", false)]
23+
[InlineData("t", false)]
24+
[InlineData("T", false)]
25+
[InlineData("", false)]
26+
[InlineData(null, false)]
27+
public void IsCustom24HourFormatDetectsCorrectly(string format, bool expected)
28+
{
29+
Assert.Equal(expected, TimePickerHandler.IsCustom24HourFormat(format));
30+
}
31+
1432
[Fact(DisplayName = "CharacterSpacing Initializes Correctly")]
1533
public async Task CharacterSpacingInitializesCorrectly()
1634
{

0 commit comments

Comments
 (0)