Skip to content

Commit

Permalink
Fix fr-CA culture time formatting and parsing (#56443)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarekgh authored Jul 28, 2021
1 parent 5d03d42 commit cd1b4cf
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,5 +210,17 @@ public void JapaneseAbbreviatedEnglishEraNamesTest()
Assert.Equal(i + 1, ci.DateTimeFormat.GetEra(eraNames[i]));
}
}

[Fact]
public void TestFrenchCanadaTimeFormat()
{
CultureInfo ci = CultureInfo.GetCultureInfo("fr-CA");
Assert.Equal(":", ci.DateTimeFormat.TimeSeparator);

DateTime time = new DateTime(2021, 10, 1, 5, 36, 50);
string formattedTime = time.ToString("HH 'h' mm 'min' ss 's'", ci);
DateTime dt = DateTime.Parse(formattedTime, ci);
Assert.Equal(time.TimeOfDay, dt.TimeOfDay);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1936,14 +1936,23 @@ internal string TimeSeparator
{
if (_sTimeSeparator == null && !GlobalizationMode.Invariant)
{
string? longTimeFormat = ShouldUseUserOverrideNlsData ? NlsGetTimeFormatString() : IcuGetTimeFormatString();
if (string.IsNullOrEmpty(longTimeFormat))
// fr-CA culture uses time format as "HH 'h' mm 'min' ss 's'" which we cannot derive the time separator from such pattern.
// We special case such culture and force ':' as time separator.
if (_sName == "fr-CA")
{
longTimeFormat = LongTimes[0];
_sTimeSeparator = ":";
}
else
{
string? longTimeFormat = ShouldUseUserOverrideNlsData ? NlsGetTimeFormatString() : IcuGetTimeFormatString();
if (string.IsNullOrEmpty(longTimeFormat))
{
longTimeFormat = LongTimes[0];
}

// Compute STIME from time format
_sTimeSeparator = GetTimeSeparator(longTimeFormat);
// Compute STIME from time format
_sTimeSeparator = GetTimeSeparator(longTimeFormat);
}
}
return _sTimeSeparator!;
}
Expand All @@ -1966,7 +1975,7 @@ internal string DateSeparator(CalendarId calendarId)
// changing the default pattern is likely will happen in the near future which can easily break formatting
// and parsing.
// We are forcing here the date separator to '/' to ensure the parsing is not going to break when changing
// the default short date pattern. The application still can override this in the code by DateTimeFormatInfo.DateSeparartor.
// the default short date pattern. The application still can override this in the code by DateTimeFormatInfo.DateSeparator.
return "/";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ private static CultureInfo GetCultureByName(string name)
}

/// <summary>
/// Return a specific culture. A tad irrelevent now since we always
/// Return a specific culture. A tad irrelevant now since we always
/// return valid data for neutral locales.
///
/// Note that there's interesting behavior that tries to find a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2054,6 +2054,16 @@ internal TokenHashValue[] CreateTokenHashTable()
InsertHash(temp, TimeSeparator, TokenType.SEP_Time, 0);
}

if (_name == "fr-CA")
{
InsertHash(temp, " h", TokenType.SEP_HourSuff, 0);
InsertHash(temp, " h ", TokenType.SEP_HourSuff, 0);
InsertHash(temp, " min", TokenType.SEP_MinuteSuff, 0);
InsertHash(temp, " min ", TokenType.SEP_MinuteSuff, 0);
InsertHash(temp, " s", TokenType.SEP_SecondSuff, 0);
InsertHash(temp, " s ", TokenType.SEP_SecondSuff, 0);
}

InsertHash(temp, AMDesignator, TokenType.SEP_Am | TokenType.Am, 0);
InsertHash(temp, PMDesignator, TokenType.SEP_Pm | TokenType.Pm, 1);

Expand Down

0 comments on commit cd1b4cf

Please sign in to comment.