C#解析到日期时间,与区域性信息无关 [英] C# parsing to datetime regardless of culture info

查看:113
本文介绍了C#解析到日期时间,与区域性信息无关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在解析某些非英语的日期字符串时遇到问题。
示例日期字符串为 8 avril 2016 vendredi,其英文为 2016年4月8日星期五。

I have problem parsing some date string where language is not english. The sample date string is "8 avril 2016 vendredi" which is "8 april 2016 friday" in english.

我已经尝试过了,但是没有运气。 / p>

I have tried this but no luck.

DateTime dateTime;
DateTime.TryParse("8 avril 2016 vendredi", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime);

在我的情况下,日期字符串可以使用任何语言,因此我无法在解析中指定区域性。 br>
感谢您的帮助。谢谢。

In my case, date string can be in any language so I cannot specify the culture in parsing.
I appreciate your help. Thanks.

推荐答案

任何解析器都可怕!

CultureInfo.GetCultures(CultureTypes.AllCultures).Select(culture => {
    DateTime result;
    return DateTime.TryParse(
        "8 avril 2016 vendredi", 
        culture, 
        DateTimeStyles.None, 
        out result
    ) ? result : default(DateTime?);
})
.Where(d => d != null)
.GroupBy(d => d)
.OrderByDescending(g => g.Count())
.FirstOrDefault()
.Key

这要求系统上的每个区域性解析日期,并选择最频繁出现的日期作为赢家。如果没有文化可以解析日期,则它返回 null

This asks every culture on the system to parse the date, and picks the date that emerges most frequently as the "winner". It returns null if no culture could parse the date.

不难想到这种方法无法提供正确的结果,因为最常见的结果不一定是正确的结果,并且某些日期确实是模棱两可的。是 04-05-2016是5月4日还是4月5日?任何分析器都认为五月四号更有可能只是因为更多的文化以这种方式对其进行了分析。至少在我的机器上。但这并不能取悦美国作者(在互联网上人数过多的作者),因此也许需要考虑文化的可能性。

It isn't hard to think of ways this can fail to provide the correct result, because the most common result isn't necessarily the correct one and some dates are truly ambiguous. Is "04-05-2016" the fourth of May or the fifth of April? The any parser thinks the fourth of May is more likely simply because more cultures parse it that way. On my machine, at least. But that will not please American authors (who are overrepresented on the Internet), so maybe the probability of cultures needs to be taken into account.

不应使用此代码解析任意用户输入,更不用说所有输入,甚至在真正缺乏关于该语言的所有其他线索的刮板的情况下,这也不是最好的方法。还要注意,这很慢。一台普通机器上有数百种文化。首先猜测整个页面的整个文化,然后根据该页面进行持续解析绝对是一个更好的主意。

This code should not be used to parse arbitrary user input, let alone all input, and even in the context of a scraper that truly lacks all other clues about the language, this is probably not the best approach. Also beware that this is slow; there are hundreds of cultures on an average machine. Guessing the whole culture for a page first and then consistently parsing based on that is absolutely a better idea.

这篇关于C#解析到日期时间,与区域性信息无关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆