中断性变更:日期和时间转换器使用区域性参数

您所在的位置:网站首页 公历农历转换器时间 中断性变更:日期和时间转换器使用区域性参数

中断性变更:日期和时间转换器使用区域性参数

2023-06-09 05:55| 来源: 网络整理| 查看: 265

日期和时间转换器使用区域性参数 项目 06/05/2023

以下类中的 ConvertTo 方法现在使用 culture 参数中的区域性作为日期和时间的格式提供程序,而不是 CultureInfo.CurrentCulture:

DateOnlyConverter DateTimeConverter DateTimeOffsetConverter TimeOnlyConverter 旧行为

以前,受影响的 API 使用 CultureInfo.CurrentCulture 作为日期和时间的格式提供程序,即使调用方在 culture 参数中指定了区域性也是如此。

请考虑以下代码片段,它将当前区域性设置为西班牙语(西班牙),但将自定义的法语区域性传递给 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)));

在 .NET 7 及更早版本中,此代码以正确的格式打印日期,但以西班牙语而不是法语显示月份名称:

Current culture: es-ES 01 agosto 2022 新行为

从 .NET 8 开始,受影响的 API 使用 culture 参数将区域性指定为格式提供程序。

上一行为中显示的代码片段会以法语正确输出月份名称:

Current culture: es-ES 01 août 2022 引入的版本

.NET 8 预览版 4

中断性变更的类型

此更改为行为更改。

更改原因

此更改修复了 ConvertTo 与 ConvertFrom 不一致的 bug。 它使用来自输入区域性的日期和时间格式字符串,但使用 CurrentCulture 格式化日期和时间。

建议的操作

如果你依赖于之前的行为,请为 culture 参数传入 CultureInfo.CurrentCulture、null 或自定义区域性。

受影响的 API System.ComponentModel.DateOnlyConverter.ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type) System.ComponentModel.DateTimeConverter.ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type) System.ComponentModel.DateTimeOffsetConverter.ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type) System.ComponentModel.TimeOnlyConverter.ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type)


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3