DateTime.Parse 的奇怪行为

2024-04-20

我们有一个大型应用程序(不幸的是)大量使用 DateTime.Parse(text) 。目前还不能用更好的东西替换 DateTime.Parse 。

最近我升级了 Windows(到 Windows 10)并开始遇到问题。根据文档https://msdn.microsoft.com/en-us/library/system.datetime.parse(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.datetime.parse(v=vs.110).aspx该方法应使用 CultureInfo.CurrentCulture 作为默认格式提供程序。在我的电脑上似乎并非如此,除非我遗漏了一些明显的东西。

以下代码调用函数两次,首先不更改任何区域性设置,然后在区域性和 ui-culture 上设置挪威区域性。 由于某种原因,在这两种情况下,它都能够解析以 en-US 格式 (M/d/yyyy) 给出的文本,但无法解析 CultureInfo 期望的格式的日期。

这里发生了什么?我希望我错过了一些明显的东西..

class Program
{
    static void Main(string[] args)
    {
        var norwegianCulture = new CultureInfo("nb-NO");

        Console.WriteLine("First");
        ParseDates();

        Console.WriteLine("\nSecond");
        Thread.CurrentThread.CurrentUICulture = norwegianCulture;
        ParseDates();

        Console.ReadKey();
    }

    private static void ParseDates()
    {
        Console.WriteLine($"CultureInfo.CurrentCulture: {CultureInfo.CurrentCulture}");
        Console.WriteLine($"CultureInfo.CurrentUICulture: {CultureInfo.CurrentUICulture}");
        Console.WriteLine($"{Thread.CurrentThread.CurrentCulture} - CurrentCulture - {Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern}");
        Console.WriteLine($"{Thread.CurrentThread.CurrentUICulture} - CurrentUICulture - {Thread.CurrentThread.CurrentUICulture.DateTimeFormat.ShortDatePattern}");
        var shortDateString = new DateTime(2015, 9, 5).ToShortDateString();
        WriteParsedDate(shortDateString);
        WriteParsedDate("1/2/2015");
        Console.WriteLine();
    }

    private static void WriteParsedDate(string shortDateString)
    {
        try
        {
            var d = DateTime.Parse(shortDateString, CultureInfo.CurrentCulture);
            Console.WriteLine($"The text {shortDateString} parsed to Year: {d.Year}, Month: {d.Month}, Day: {d.Day}");
        }
        catch (Exception e)
        {
            Console.WriteLine($"The text {shortDateString} could not be parsed.");
            Console.WriteLine($"Error: {e.Message}");
        }
    }
}

将其写入控制台:

First
CultureInfo.CurrentCulture: nb-NO
CultureInfo.CurrentUICulture: en-US
nb-NO - CurrentCulture - dd.MM.yyyy
en-US - CurrentUICulture - M/d/yyyy
The text 05.09.2015 could not be parsed.
Error: String was not recognized as a valid DateTime.
The text 1/2/2015 parsed to Year: 2015, Month: 2, Day: 1


Second
CultureInfo.CurrentCulture: nb-NO
CultureInfo.CurrentUICulture: nb-NO
nb-NO - CurrentCulture - dd.MM.yyyy
nb-NO - CurrentUICulture - dd.MM.yyyy
The text 05.09.2015 could not be parsed.
Error: Strengen ble ikke gjenkjent som en gyldig DateTime.
The text 1/2/2015 parsed to Year: 2015, Month: 2, Day: 1

部分解决方法

正如 shf301 所写,这是由于 Windows 10 中的一个错误造成的。

我设法通过更改时间分隔符“。”来使其正常工作。到“:” - 与旧版本的 Windows 中一样。我想它正在处理该更改,因为我们仅使用用户设置,而不是手动加载/指定它们。


这是列出的一个未解决的错误Connect https://connect.microsoft.com/VisualStudio/Feedback/Details/1621722. It appears https://www.reddit.com/r/csharp/comments/3f5z5w/datetime_parsing_fails_in_windows_10/对于任何具有相同日期和时间分隔符的区域设置,日期解析都会失败。

Estonian (Estonia)
Finnish (Finland)
Norwegian Bokmål (Norway)
Norwegian Bokmål (Svalbard and Jan Mayen)
Serbian (Cyrillic, Kosovo)
Serbian (Latin, Montenegro)
Serbian (Latin, Serbia)
Serbian (Latin, Kosovo)

您的示例从不使用美国设置解析或格式化日期。首先DateTime.Parse没有IFormatProvider uses CurrentCulture并不是CurrentUICulture其次你明确地通过CurrentCulture into DateTime.Parse。这就是为什么两种情况下的输出完全相同。

Had "1/2/2015"已解析使用en-US规则,它将有输出:

The text 1/2/2015 parsed to Year: 2015, Month: 1, Day: 2

DateTime.Parse只是聪明地假设你使用了/作为日期分隔符而不是.并将日期解析为d/M/yyyy.

不幸的是,在这个问题得到解决之前,您将不得不将您的电话更改为DateTime.Parse。或者切换到不受影响的.Net 3.5 或更早版本。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

DateTime.Parse 的奇怪行为 的相关文章

随机推荐