[英]DateTime.TryParse() and DateTime.TryParseExact() doesn't parse date on linux/macos and work properly on windows
I try to parse a date that has format "Вс, 1 дек 2019 20:40:00 +0300" (Russian language) dotnet core 3.1 and both TryParse() and TryParseExact() work properly on windows but don't at linux and macos.我尝试解析格式为“Вс, 1 дек 2019 20:40:00 +0300”(俄语)dotnet core 3.1 的日期,并且 TryParse() 和 TryParseExact() 在 Windows 上都可以正常工作,但在 linux 和苹果系统。 I tried different formats and different DateTimeStyles, current culture and used System.Globalization.CultureInfo.GetCultureInfo("ru-RU") for getting russian language culture object.我尝试了不同的格式和不同的 DateTimeStyles、当前文化,并使用 System.Globalization.CultureInfo.GetCultureInfo("ru-RU") 来获取俄语文化对象。 Tried it on ubuntu on several machines - it doesnt' parse date.在多台机器上的 ubuntu 上尝试过 - 它不解析日期。 This is the code I use with some formats:这是我使用某些格式的代码:
string dateStr = "Вс, 1 дек 2019 20:40:00 +0300";
string dateStr2 = "Вс, 1 дек 2019 20:50:00";
DateTime result = default(DateTime);
DateTime result2 = default(DateTime);
string[] dateFormats = new string[]{
"ddd, d MMM yyyy HH:mm:ss",
"ddd, d MMM yyyy HH:mm:ss K",
"ddd, d MMM yyyy H:mm:ss",
"ddd, d MMM yyyy H:mm:ss K"
};
var ruRu = System.Globalization.CultureInfo.GetCultureInfo("ru-RU");
if(!DateTime.TryParse(dateStr, ruRu, DateTimeStyles.None, out result)){
DateTime.TryParseExact(dateStr, dateFormats,
//System.Globalization.CultureInfo.CurrentCulture,
ruRu,
System.Globalization.DateTimeStyles.AssumeUniversal, out result);
}
if (!DateTime.TryParse(dateStr2, ruRu, DateTimeStyles.None, out result2)){
DateTime.TryParseExact(dateStr2, dateFormats,
//System.Globalization.CultureInfo.CurrentCulture,
ruRu,
System.Globalization.DateTimeStyles.AssumeUniversal, out result2);
}
Console.WriteLine(result);
Console.WriteLine(result2);
In both cases under linux and macos I got '1/1/0001 12:00:00 AM'在 linux 和 macos 下的两种情况下,我都得到了“1/1/0001 12:00:00 AM”
well, seems that Linux .Net Core 2.2 has different month names defined comparing with the Windows one:好吧,与 Windows 相比,Linux .Net Core 2.2 似乎定义了不同的月份名称:
TestContext.WriteLine("month:{0}", string.Join(";", ruRu.DateTimeFormat.AbbreviatedMonthNames));
output is month:янв;фев;мар;апр;май;июн;июл;авг;сен;окт;ноя;дек;
输出是month:янв;фев;мар;апр;май;июн;июл;авг;сен;окт;ноя;дек;
on Windows在 Windows 上
but month:янв.;февр.;март;апр.;май;июнь;июль;авг.;сент.;окт.;нояб.;дек.;
但month:янв.;февр.;март;апр.;май;июнь;июль;авг.;сент.;окт.;нояб.;дек.;
on Ubuntu (do not ask me why).在 Ubuntu 上(不要问我为什么)。 Dot is added at the end of months abbreviations.在月份缩写的末尾添加点。
Also you have to take into account that июнь;июль;март
are not shortened and февр
is not like on Windows фев
此外,您还必须考虑到июнь;июль;март
没有缩短,并且февр
不像 Windows 上的фев
Mac OS is build on some Unix kernel actually so it has the same .net framework as on Ubuntu I suppose. Mac OS 实际上是建立在一些 Unix 内核上的,所以我想它具有与 Ubuntu 相同的 .net 框架。
Your case can be solved with new format string like below:您的情况可以使用新的格式字符串解决,如下所示:
string[] dateFormats = new string[]{
"ddd, d MMM yyyy HH:mm:ss",
"ddd, d MMM yyyy HH:mm:ss K",
"ddd, d MMM. yyyy HH:mm:ss K", // this is added for linux
"ddd, d MMM yyyy H:mm:ss",
"ddd, d MMM yyyy H:mm:ss K"
};
However it looks like a bug in the .net since will not parse March month from the Пн, 4 мар 2019 00:00:00
for example.然而,它看起来像是 .net 中的一个错误,因为不会从Пн, 4 мар 2019 00:00:00
解析三月月份。
UPD: I've checked and had not found the way to parse Пн, 4 мар 2019 00:00:00
on Linux using the DateTime.TryParseExact
because of the март
on Linux will not be parsed from the мар
UPD:我已经检查并没有找到在 Linux 上使用DateTime.TryParseExact
解析Пн, 4 мар 2019 00:00:00
方法,因为 Linux 上的март
不会从мар
解析
Seems that you should register new issue for .Net Core.似乎您应该为 .Net Core 注册新问题。 Or parse the string manually but it sucks或者手动解析字符串但它很糟糕
Code below works :下面的代码有效:
string[] dateStrs = {"Вс, 1 дек 2019 20:40:00 +0300","Вс, 1 дек 2019 20:50:00"};
var ruRu = System.Globalization.CultureInfo.GetCultureInfo("ru-RU");
string[] dateFormats = new string[]{
"ddd, d MMM yyyy HH:mm:ss",
"ddd, d MMM yyyy HH:mm:ss K",
"ddd, d MMM yyyy H:mm:ss",
"ddd, d MMM yyyy H:mm:ss K"
};
DateTime date;
Boolean match = false;
foreach (string dateStr in dateStrs)
{
match = DateTime.TryParseExact(dateStr,dateFormats, ruRu, System.Globalization.DateTimeStyles.None, out date);
Console.WriteLine(date.ToString());
}
Console.ReadLine();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.