Date and time converters honor culture argument

The ConvertTo methods on the following classes now use the culture from the culture parameter as the format provider for the date and time instead of CultureInfo.CurrentCulture:

Previous behavior

Previously, the affected APIs used CultureInfo.CurrentCulture as the format provider for the date and time even though the caller specified a culture in the culture parameter.

Consider the following code snippet that sets the current culture to Spanish (Spain) but passes a customized French culture to DateTimeConverter.ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type).

CultureInfo.CurrentCulture = new CultureInfo("es-ES");
Console.WriteLine($"Current culture: {CultureInfo.CurrentCulture}");

var dt1 = new DateTime(2022, 8, 1);

var frCulture = new CultureInfo("fr-FR");
frCulture.DateTimeFormat.ShortDatePattern = "dd MMMM yyyy";

Console.WriteLine(TypeDescriptor.GetConverter(dt1).ConvertTo(null, frCulture, dt1, typeof(string)));

In .NET 7 and earlier versions, this code prints the date in the correct format but with the name of the month in Spanish instead of French:

Current culture: es-ES
01 agosto 2022

New behavior

Starting in .NET 8, the affected APIs use the culture specified by the culture parameter as the format provider.

The code snippet shown in the Previous behavior correctly prints the name of the month in French:

Current culture: es-ES
01 août 2022

Version introduced

.NET 8 Preview 4

Type of breaking change

This change is a behavioral change.

Reason for change

This change fixes a bug where ConvertTo was not consistent with ConvertFrom. It used the date and time format strings from the input culture but formatted the date and time with CurrentCulture.

If you relied on the previous behavior, pass in CultureInfo.CurrentCulture, null, or a custom culture for the culture parameter.

Affected APIs